Skip to content

Commit

Permalink
fix: fix ctx body as any (eggjs#4613)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu authored Feb 19, 2021
1 parent e25584d commit e9fba1b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ declare module 'egg' {
body: any;
}

export interface Response extends KoaApplication.Response { // tslint:disable-line
export interface Response<ResponseBodyT = any> extends KoaApplication.Response { // tslint:disable-line
/**
* read response real status code.
*
Expand All @@ -181,6 +181,7 @@ declare module 'egg' {
* @member {Number} Context#realStatus
*/
realStatus: number;
body: ResponseBodyT;
}

export type LoggerLevel = EggLoggerLevel;
Expand Down Expand Up @@ -729,8 +730,9 @@ declare module 'egg' {
* rewrite all the properties to be compatible with types in Koa.
* @see https://github.com/eggjs/egg/pull/3329
*/
export interface Context extends KoaApplication.BaseContext {
export interface Context<ResponseBodyT = any> extends KoaApplication.BaseContext {
[key: string]: any;
body: ResponseBodyT;

app: Application;

Expand All @@ -744,7 +746,7 @@ declare module 'egg' {

request: Request;

response: Response;
response: Response<ResponseBodyT>;

// The new 'cookies' instead of Koa's.
cookies: EggCookies;
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/apps/app-ts/app/middleware/default_ctx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Context } from 'egg';

export default () => {
return async (ctx: Context, next: () => Promise<any>) => {
ctx.locals.url = ctx.url;
await next();
console.log(ctx.body.foo);
};
}
13 changes: 13 additions & 0 deletions test/fixtures/apps/app-ts/app/middleware/generic_ctx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Context } from 'egg';

export interface CustomBody {
bar: string;
}

export default () => {
return async (ctx: Context<CustomBody>, next: () => Promise<any>) => {
ctx.locals.url = ctx.url;
await next();
console.log(ctx.body.bar);
};
}

0 comments on commit e9fba1b

Please sign in to comment.