From 5e18ed261d62186b7c23ec97cb41c20e9d9d89e7 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 18 Dec 2024 23:33:47 +0800 Subject: [PATCH] feat: allow to set symbol property on request, response, context and app (#12) ## Summary by CodeRabbit - **New Features** - Introduced a `CustomRequest` class for enhanced host value retrieval. - Added support for additional Node.js versions in CI testing. - **Improvements** - Updated ESLint configuration to include more linting rules. - Enhanced error handling and middleware management in the `Application` class. - Added symbol key properties in `Context`, `Request`, and `Response` classes for increased flexibility. - **Refactor** - Simplified import paths in example files for better maintainability. --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .eslintrc | 3 ++- .github/workflows/node.js.yml | 2 +- example/extend/Request.ts | 23 +++++++++++++++++++++++ example/helloworld.cjs | 2 +- example/helloworld.mjs | 2 +- src/application.ts | 3 ++- src/context.ts | 1 + src/request.ts | 1 + src/response.ts | 1 + 9 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 example/extend/Request.ts diff --git a/.eslintrc b/.eslintrc index 98f9236ae..9bcdb4688 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,5 +1,6 @@ { "extends": [ - "eslint-config-egg/typescript" + "eslint-config-egg/typescript", + "eslint-config-egg/lib/rules/enforce-node-prefix" ] } diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 60edbce24..ae12488c4 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -12,6 +12,6 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest, macos-latest, windows-latest' - version: '18.19.0, 18, 20, 22' + version: '18.19.0, 18, 20, 22, 23' secrets: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/example/extend/Request.ts b/example/extend/Request.ts new file mode 100644 index 000000000..653175d3f --- /dev/null +++ b/example/extend/Request.ts @@ -0,0 +1,23 @@ +import { Request } from '../../src/index.js'; + +const HOST = Symbol('request host'); + +export class CustomRequest extends Request { + get host(): string { + let host = this[HOST] as string; + if (host) { + return host; + } + + if (this.app.proxy) { + host = '127.0.0.1'; + } + const rawHost = host || this.get('host'); + if (!rawHost) { + this[HOST] = ''; + return ''; + } + this[HOST] = rawHost.split(/\s*,\s*/)[0]; + return host; + } +} diff --git a/example/helloworld.cjs b/example/helloworld.cjs index 7eb5bd126..e20c998cb 100644 --- a/example/helloworld.cjs +++ b/example/helloworld.cjs @@ -1,5 +1,5 @@ // eslint-disable-next-line @typescript-eslint/no-var-requires -const { Application } = require('../dist/commonjs/index.js'); +const { Application } = require('..'); const app = new Application(); diff --git a/example/helloworld.mjs b/example/helloworld.mjs index d805b3373..2efc9a3e9 100644 --- a/example/helloworld.mjs +++ b/example/helloworld.mjs @@ -1,4 +1,4 @@ -import { Application } from '../dist/esm/index.js'; +import { Application } from '..'; const app = new Application(); diff --git a/src/application.ts b/src/application.ts index d069bdb9b..12b770c26 100644 --- a/src/application.ts +++ b/src/application.ts @@ -17,7 +17,7 @@ import { Response } from './response.js'; import type { ContextDelegation } from './context.js'; import type { CustomError, AnyProto } from './types.js'; -const debug = debuglog('@eggjs/koa:application'); +const debug = debuglog('@eggjs/koa/application'); export type ProtoImplClass = new(...args: any[]) => T; export type Next = () => Promise; @@ -29,6 +29,7 @@ export type MiddlewareFunc = _MiddlewareFunc & { _name?: string }; * Inherits from `Emitter.prototype`. */ export class Application extends Emitter { + [key: symbol]: unknown; /** * Make HttpError available to consumers of the library so that consumers don't * have a direct dependency upon `http-errors` diff --git a/src/context.ts b/src/context.ts index 7be730311..0d3175b1f 100644 --- a/src/context.ts +++ b/src/context.ts @@ -11,6 +11,7 @@ import type { Response } from './response.js'; import type { CustomError, AnyProto } from './types.js'; export class Context { + [key: symbol]: unknown; app: Application; req: IncomingMessage; res: ServerResponse; diff --git a/src/request.ts b/src/request.ts index 7bf09df1c..d8929fac5 100644 --- a/src/request.ts +++ b/src/request.ts @@ -13,6 +13,7 @@ import type { ContextDelegation } from './context.js'; import type { Response } from './response.js'; export class Request { + [key: symbol]: unknown; app: Application; req: IncomingMessage; res: ServerResponse; diff --git a/src/response.ts b/src/response.ts index 345fc35aa..c3581756a 100644 --- a/src/response.ts +++ b/src/response.ts @@ -17,6 +17,7 @@ import type { ContextDelegation } from './context.js'; import type { Request } from './request.js'; export class Response { + [key: symbol]: unknown; app: Application; req: IncomingMessage; res: ServerResponse;