Skip to content

Commit

Permalink
fix: let MiddlewareFunc can extend by custom Context interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 19, 2024
1 parent 78558ce commit f5408f3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
16 changes: 16 additions & 0 deletions example/extend/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MiddlewareFunc, Context, type ContextDelegation } from '../../src/index.js';

class CustomContext extends Context {
// Add your custom properties and methods here
get hello() {
return 'world';
}
}

type ICustomContext = CustomContext & ContextDelegation;

export const middleware: MiddlewareFunc<ICustomContext> = async (ctx, next) => {
console.log('middleware start, %s', ctx.hello, ctx.writable);
await next();
console.log('middleware end');
};
9 changes: 4 additions & 5 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ import onFinished from 'on-finished';
import statuses from 'statuses';
import compose from 'koa-compose';
import { HttpError } from 'http-errors';
import { Context } from './context.js';
import { Context, type ContextDelegation } from './context.js';
import { Request } from './request.js';
import { Response } from './response.js';
import type { ContextDelegation } from './context.js';
import type { CustomError, AnyProto } from './types.js';

const debug = debuglog('@eggjs/koa/application');

export type ProtoImplClass<T = object> = new(...args: any[]) => T;
export type Next = () => Promise<void>;
type _MiddlewareFunc = (ctx: ContextDelegation, next: Next) => Promise<void> | void;
export type MiddlewareFunc = _MiddlewareFunc & { _name?: string };
type _MiddlewareFunc<T> = (ctx: T, next: Next) => Promise<void> | void;
export type MiddlewareFunc<T = ContextDelegation> = _MiddlewareFunc<T> & { _name?: string };

/**
* Expose `Application` class.
Expand Down Expand Up @@ -222,7 +221,7 @@ export class Application extends Emitter {
*/
protected createContext(req: IncomingMessage, res: ServerResponse) {
const context = new this.ContextClass(this, req, res);
return context as ContextDelegation;
return context;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Response } from './response.js';
import type { CustomError, AnyProto } from './types.js';

export class Context {
[key: symbol]: unknown;
[key: symbol | string]: unknown;
app: Application;
req: IncomingMessage;
res: ServerResponse;
Expand Down Expand Up @@ -158,7 +158,9 @@ export class Context {
// We can probably remove it once jest fixes https://github.com/facebook/jest/issues/2549.
const isNativeError = err instanceof Error ||
Object.prototype.toString.call(err) === '[object Error]';
if (!isNativeError) err = new Error(util.format('non-error thrown: %j', err));
if (!isNativeError) {
err = new Error(util.format('non-error thrown: %j', err));
}

let headerSent = false;
if (this.response.headerSent || !this.response.writable) {
Expand Down Expand Up @@ -282,5 +284,4 @@ export type ContextDelegation = Context & Pick<Request, 'acceptsLanguages' | 'ac
| 'path' | 'url' | 'accept' | 'origin' | 'href' | 'subdomains' | 'protocol' | 'host' | 'hostname'
| 'URL' | 'header' | 'headers' | 'secure' | 'stale' | 'fresh' | 'ips' | 'ip'>
& Pick<Response, 'attachment' | 'redirect' | 'remove' | 'vary' | 'has' | 'set' | 'append' | 'flushHeaders'
| 'status' | 'message' | 'body' | 'length' | 'type' | 'lastModified' | 'etag' | 'headerSent' | 'writable'>
& AnyProto;
| 'status' | 'message' | 'body' | 'length' | 'type' | 'lastModified' | 'etag' | 'headerSent' | 'writable'>;
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export type CustomError = Error & {
};

export type AnyProto = {
[key: string]: any;
[key: string | symbol]: any;
};

0 comments on commit f5408f3

Please sign in to comment.