Skip to content

Commit

Permalink
feat: allow to set symbol property on request, response, context and …
Browse files Browse the repository at this point in the history
…app (#12)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## 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.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
fengmk2 and coderabbitai[bot] authored Dec 18, 2024
1 parent 80fcbf7 commit 5e18ed2
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": [
"eslint-config-egg/typescript"
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
23 changes: 23 additions & 0 deletions example/extend/Request.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion example/helloworld.cjs
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
2 changes: 1 addition & 1 deletion example/helloworld.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Application } from '../dist/esm/index.js';
import { Application } from '..';

const app = new Application();

Expand Down
3 changes: 2 additions & 1 deletion src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = object> = new(...args: any[]) => T;
export type Next = () => Promise<void>;
Expand All @@ -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`
Expand Down
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5e18ed2

Please sign in to comment.