Skip to content

Commit

Permalink
fix: remove template on Application (#16)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Simplified type handling in core application classes by removing
generic type parameters
- Updated `Application`, `Request`, and `Response` classes to use a
standardized `Context` type
	- Streamlined context management across the application framework

- **Tests**
- Updated test cases to reflect new type handling in application context

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jan 2, 2025
1 parent f4b2c22 commit 0070bcf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
26 changes: 13 additions & 13 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type MiddlewareFunc<T extends Context = Context> = _MiddlewareFunc<T> & {
* Expose `Application` class.
* Inherits from `Emitter.prototype`.
*/
export class Application<T extends Context = Context> extends Emitter {
export class Application extends Emitter {
[key: symbol]: unknown;
/**
* Make HttpError available to consumers of the library so that consumers don't
Expand All @@ -41,14 +41,14 @@ export class Application<T extends Context = Context> extends Emitter {
proxyIpHeader: string;
maxIpsCount: number;
protected _keys?: string[];
middleware: MiddlewareFunc<T>[];
ctxStorage: AsyncLocalStorage<T>;
middleware: MiddlewareFunc<Context>[];
ctxStorage: AsyncLocalStorage<Context>;
silent: boolean;
ContextClass: ProtoImplClass<T>;
ContextClass: ProtoImplClass<Context>;
context: AnyProto;
RequestClass: ProtoImplClass<Request<T>>;
RequestClass: ProtoImplClass<Request>;
request: AnyProto;
ResponseClass: ProtoImplClass<Response<T>>;
ResponseClass: ProtoImplClass<Response>;
response: AnyProto;

/**
Expand Down Expand Up @@ -84,11 +84,11 @@ export class Application<T extends Context = Context> extends Emitter {
this.middleware = [];
this.ctxStorage = getAsyncLocalStorage();
this.silent = false;
this.ContextClass = class ApplicationContext extends Context {} as ProtoImplClass<T>;
this.ContextClass = class ApplicationContext extends Context {} as ProtoImplClass<Context>;
this.context = this.ContextClass.prototype;
this.RequestClass = class ApplicationRequest extends Request {} as ProtoImplClass<Request<T>>;
this.RequestClass = class ApplicationRequest extends Request {} as ProtoImplClass<Request>;
this.request = this.RequestClass.prototype;
this.ResponseClass = class ApplicationResponse extends Response {} as ProtoImplClass<Response<T>>;
this.ResponseClass = class ApplicationResponse extends Response {} as ProtoImplClass<Response>;
this.response = this.ResponseClass.prototype;
}

Expand Down Expand Up @@ -151,7 +151,7 @@ export class Application<T extends Context = Context> extends Emitter {
/**
* Use the given middleware `fn`.
*/
use(fn: MiddlewareFunc<T>) {
use<T extends Context = Context>(fn: MiddlewareFunc<T>) {
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
const name = fn._name || fn.name || '-';
if (isGeneratorFunction(fn)) {
Expand All @@ -160,7 +160,7 @@ export class Application<T extends Context = Context> extends Emitter {
'https://github.com/koajs/koa/blob/master/docs/migration.md');
}
debug('use %o #%d', name, this.middleware.length);
this.middleware.push(fn);
this.middleware.push(fn as MiddlewareFunc<Context>);
return this;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ export class Application<T extends Context = Context> extends Emitter {
* Handle request in callback.
* @private
*/
protected async handleRequest(ctx: T, fnMiddleware: (ctx: T) => Promise<void>) {
protected async handleRequest(ctx: Context, fnMiddleware: (ctx: Context) => Promise<void>) {
this.emit('request', ctx);
const res = ctx.res;
res.statusCode = 404;
Expand Down Expand Up @@ -246,7 +246,7 @@ export class Application<T extends Context = Context> extends Emitter {
/**
* Response helper.
*/
protected _respond(ctx: T) {
protected _respond(ctx: Context) {
// allow bypassing koa
if (ctx.respond === false) return;

Expand Down
6 changes: 3 additions & 3 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ export interface RequestSocket extends Socket {
encrypted: boolean;
}

export class Request<T extends Context = Context> {
export class Request {
[key: symbol]: unknown;
app: Application;
req: IncomingMessage;
res: ServerResponse;
ctx: T;
ctx: Context;
response: Response;
originalUrl: string;

constructor(app: Application, ctx: T, req: IncomingMessage, res: ServerResponse) {
constructor(app: Application, ctx: Context, req: IncomingMessage, res: ServerResponse) {
this.app = app;
this.req = req;
this.res = res;
Expand Down
6 changes: 3 additions & 3 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import type { Application } from './application.js';
import type { Context } from './context.js';
import type { Request } from './request.js';

export class Response<T extends Context = Context> {
export class Response {
[key: symbol]: unknown;
app: Application;
req: IncomingMessage;
res: ServerResponse;
ctx: T;
ctx: Context;
request: Request;

constructor(app: Application, ctx: T, req: IncomingMessage, res: ServerResponse) {
constructor(app: Application, ctx: Context, req: IncomingMessage, res: ServerResponse) {
this.app = app;
this.req = req;
this.res = res;
Expand Down
4 changes: 2 additions & 2 deletions test/application/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ describe('app.context', () => {
}
}

class MyApp extends Application<MyContext> {
class MyApp extends Application {
constructor() {
super();
this.ContextClass = MyContext;
}
}

const app = new MyApp();
app.use(ctx => {
app.use((ctx: MyContext) => {
ctx.body = `hello, ${ctx.getMsg()}`;
});

Expand Down

0 comments on commit 0070bcf

Please sign in to comment.