Skip to content

Commit 8e3551c

Browse files
committed
remove ContextDelegation
1 parent f058c21 commit 8e3551c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+456
-302
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121
"dependencies": {
2222
"@eggjs/cluster": "^3.0.0",
2323
"@eggjs/cookies": "^3.0.0",
24-
"@eggjs/core": "^6.2.5",
24+
"@eggjs/core": "^6.2.11",
2525
"@eggjs/schedule": "^5.0.2",
2626
"@eggjs/utils": "^4.1.5",
2727
"@eggjs/watcher": "^4.0.1",
2828
"circular-json-for-egg": "^1.0.0",
2929
"cluster-client": "^3.7.0",
30-
"delegates": "^1.0.0",
3130
"egg-development": "^3.0.0",
3231
"egg-errors": "^2.3.1",
3332
"egg-i18n": "^2.1.1",
@@ -57,10 +56,9 @@
5756
"@arethetypeswrong/cli": "^0.17.1",
5857
"@eggjs/bin": "^7.0.0",
5958
"@eggjs/koa": "^2.19.1",
60-
"@eggjs/mock": "^6.0.3",
59+
"@eggjs/mock": "^6.0.5",
6160
"@eggjs/supertest": "^8.1.1",
6261
"@eggjs/tsconfig": "1",
63-
"@types/delegates": "^1.0.3",
6462
"@types/koa-bodyparser": "^4.3.12",
6563
"@types/mocha": "^10.0.7",
6664
"@types/ms": "^0.7.34",

src/app/extend/context.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import delegate from 'delegates';
21
import { assign } from 'utility';
32
import { now, diff } from 'performance-ms';
43
import {
54
utils, Context as EggCoreContext, Router,
6-
type ContextDelegation as EggCoreContextDelegation,
75
} from '@eggjs/core';
86
import type { Cookies as ContextCookies } from '@eggjs/cookies';
97
import { EggLogger } from 'egg-logger';
@@ -12,8 +10,8 @@ import type {
1210
HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
1311
} from '../../lib/core/httpclient.js';
1412
import type { BaseContextClass } from '../../lib//core/base_context_class.js';
15-
import Request from './request.js';
16-
import Response from './response.js';
13+
import type Request from './request.js';
14+
import type Response from './response.js';
1715
import type Helper from './helper.js';
1816

1917
import './context.types.js';
@@ -33,7 +31,9 @@ interface Cookies extends ContextCookies {
3331
export default class Context extends EggCoreContext {
3432
declare app: Application;
3533
declare request: Request;
34+
declare response: Response;
3635
declare service: BaseContextClass;
36+
declare proxy: any;
3737

3838
/**
3939
* Request start time
@@ -230,7 +230,7 @@ export default class Context extends EggCoreContext {
230230
* });
231231
* ```
232232
*/
233-
runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, taskName?: string): void {
233+
runInBackground(scope: (ctx: Context) => Promise<void>, taskName?: string): void {
234234
// try to use custom function name first
235235
if (!taskName) {
236236
taskName = Reflect.get(scope, '_name') || scope.name || utils.getCalleeFromStack(true);
@@ -243,7 +243,7 @@ export default class Context extends EggCoreContext {
243243

244244
// let plugins or frameworks to reuse _runInBackground in some cases.
245245
// e.g.: https://github.com/eggjs/egg-mock/pull/78
246-
async _runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, taskName: string) {
246+
async _runInBackground(scope: (ctx: Context) => Promise<void>, taskName: string) {
247247
const startTime = now();
248248
try {
249249
await scope(this as any);
@@ -257,46 +257,52 @@ export default class Context extends EggCoreContext {
257257
this.app.emit('error', err, this);
258258
}
259259
}
260-
}
261-
262-
/**
263-
* Context delegation.
264-
*/
265260

266-
delegate(Context.prototype, 'request')
267261
/**
268262
* @member {Boolean} Context#acceptJSON
269263
* @see Request#acceptJSON
270264
* @since 1.0.0
271265
*/
272-
.getter('acceptJSON')
266+
get acceptJSON(): boolean {
267+
return this.request.acceptJSON;
268+
}
269+
270+
get query(): Record<string, string> {
271+
return this.request.query;
272+
}
273+
273274
/**
274275
* @member {Array} Context#queries
275276
* @see Request#queries
276277
* @since 1.0.0
277278
*/
278-
.getter('queries')
279-
/**
280-
* @member {Boolean} Context#accept
281-
* @see Request#accept
282-
* @since 1.0.0
283-
*/
284-
.getter('accept')
279+
get queries(): Record<string, string[]> {
280+
return this.request.queries;
281+
}
282+
285283
/**
286284
* @member {string} Context#ip
287285
* @see Request#ip
288286
* @since 1.0.0
289287
*/
290-
.access('ip');
288+
get ip(): string {
289+
return this.request.ip;
290+
}
291+
292+
set ip(val: string) {
293+
this.request.ip = val;
294+
}
291295

292-
delegate(Context.prototype, 'response')
293296
/**
294297
* @member {Number} Context#realStatus
295298
* @see Response#realStatus
296299
* @since 1.0.0
297300
*/
298-
.access('realStatus');
301+
get realStatus(): number {
302+
return this.response.realStatus;
303+
}
299304

300-
export type ContextDelegation = EggCoreContextDelegation & Context
301-
& Pick<Request, 'acceptJSON' | 'queries' | 'accept' | 'ip'>
302-
& Pick<Response, 'realStatus'>;
305+
set realStatus(val: number) {
306+
this.response.realStatus = val;
307+
}
308+
}

src/app/extend/context.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type {
22
Router,
33
} from '@eggjs/core';
4+
import type { EggLogger } from 'egg-logger';
45
import type {
56
HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
67
} from '../../lib/core/httpclient.js';
78
import type Helper from './helper.js';
8-
import type { EggLogger } from 'egg-logger';
99

1010
declare module '@eggjs/core' {
1111
// add Context overrides types

src/app/extend/request.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import querystring from 'node:querystring';
22
import { Request as EggCoreRequest } from '@eggjs/core';
33
import type { Application } from '../../lib/application.js';
4-
import type { ContextDelegation } from './context.js';
4+
import type Context from './context.js';
55
import Response from './response.js';
66

77
const QUERY_CACHE = Symbol('request query cache');
@@ -13,9 +13,14 @@ const RE_ARRAY_KEY = /[^\[\]]+\[\]$/;
1313

1414
export default class Request extends EggCoreRequest {
1515
declare app: Application;
16-
declare ctx: ContextDelegation;
16+
declare ctx: Context;
1717
declare response: Response;
1818

19+
/**
20+
* Request body, parsed from koa-bodyparser or egg-multipart
21+
*/
22+
declare body: any;
23+
1924
/**
2025
* Parse the "Host" header field host
2126
* and support X-Forwarded-Host when a

src/app/extend/request.types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module '@eggjs/core' {
2+
// add Request overrides types
3+
interface Request {
4+
body: any;
5+
get acceptJSON(): boolean;
6+
get query(): Record<string, string>;
7+
set query(obj: Record<string, string>);
8+
get queries(): Record<string, string[]>;
9+
}
10+
}

src/app/extend/response.types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare module '@eggjs/core' {
2+
// add Response overrides types
3+
interface Response {
4+
get realStatus(): number;
5+
set realStatus(status: number);
6+
}
7+
}

src/app/middleware/meta.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
*/
44

55
import { performance } from 'node:perf_hooks';
6-
import type { ContextDelegation, Next } from '../../lib/egg.js';
6+
import type { Context, Next } from '../../lib/egg.js';
77

88
export interface MetaMiddlewareOptions {
99
enable: boolean;
1010
logging: boolean;
1111
}
1212

1313
export default (options: MetaMiddlewareOptions) => {
14-
return async function meta(ctx: ContextDelegation, next: Next) {
14+
return async function meta(ctx: Context, next: Next) {
1515
if (options.logging) {
1616
ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s',
1717
ctx.host, ctx.header['user-agent']);

src/app/middleware/notfound.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Next, ContextDelegation } from '../../lib/egg.js';
1+
import type { Next, Context } from '../../lib/egg.js';
22

33
export interface NotFoundMiddlewareOptions {
44
enable: boolean;
55
pageUrl: string;
66
}
77

88
export default (options: NotFoundMiddlewareOptions) => {
9-
return async function notfound(ctx: ContextDelegation, next: Next) {
9+
return async function notfound(ctx: Context, next: Next) {
1010
await next();
1111

1212
if (ctx.status !== 404 || ctx.body) {

src/app/middleware/site_file.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from 'node:path';
22
import { fileURLToPath } from 'node:url';
33
import { readFile } from 'node:fs/promises';
4-
import type { Next, ContextDelegation } from '../../lib/egg.js';
4+
import type { Next, Context } from '../../lib/egg.js';
55

6-
export type SiteFileContentFun = (ctx: ContextDelegation) => Promise<Buffer | string>;
6+
export type SiteFileContentFun = (ctx: Context) => Promise<Buffer | string>;
77

88
export interface SiteFileMiddlewareOptions {
99
enable: boolean;
@@ -14,7 +14,7 @@ export interface SiteFileMiddlewareOptions {
1414
const BUFFER_CACHE = Symbol('siteFile URL buffer cache');
1515

1616
export default (options: SiteFileMiddlewareOptions) => {
17-
return async function siteFile(ctx: ContextDelegation, next: Next) {
17+
return async function siteFile(ctx: Context, next: Next) {
1818
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') {
1919
return next();
2020
}

src/index.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { startEgg } from './lib/start.js';
77
import Helper from './app/extend/helper.js';
88

99
// export extends
10-
export { Helper };
10+
export {
11+
Helper,
12+
// keep compatible with egg v3
13+
Helper as IHelper,
14+
};
1115

1216
// export types
1317
export * from './lib/egg.js';
@@ -17,6 +21,15 @@ export * from './lib/start.js';
1721
// export errors
1822
export * from './lib/error/index.js';
1923

24+
// export loggers
25+
export type {
26+
LoggerLevel,
27+
} from 'egg-logger';
28+
29+
// export httpClients
30+
export * from './lib/core/httpclient.js';
31+
export * from './lib/core/context_httpclient.js';
32+
2033
/**
2134
* Start egg application with cluster mode
2235
* @since 1.0.0
@@ -27,7 +40,9 @@ export * from '@eggjs/cluster';
2740
* Start egg application with single process mode
2841
* @since 1.0.0
2942
*/
30-
export const start = startEgg;
43+
export {
44+
startEgg as start,
45+
};
3146

3247
/**
3348
* @member {Application} Egg#Application
@@ -57,19 +72,25 @@ export { AppWorkerLoader, AgentWorkerLoader } from './lib/loader/index.js';
5772
* @member {Controller} Egg#Controller
5873
* @since 1.1.0
5974
*/
60-
export const Controller = BaseContextClass;
75+
export {
76+
BaseContextClass as Controller,
77+
};
6178

6279
/**
6380
* @member {Service} Egg#Service
6481
* @since 1.1.0
6582
*/
66-
export const Service = BaseContextClass;
83+
export {
84+
BaseContextClass as Service,
85+
};
6786

6887
/**
6988
* @member {Subscription} Egg#Subscription
7089
* @since 1.10.0
7190
*/
72-
export const Subscription = BaseContextClass;
91+
export {
92+
BaseContextClass as Subscription,
93+
};
7394

7495
/**
7596
* @member {BaseContextClass} Egg#BaseContextClass

src/lib/application.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isGeneratorFunction } from 'is-type-of';
99
import {
1010
EggApplicationCore,
1111
type EggApplicationCoreOptions,
12-
type ContextDelegation,
12+
type Context,
1313
} from './egg.js';
1414
import { AppWorkerLoader } from './loader/index.js';
1515
import Helper from '../app/extend/helper.js';
@@ -223,7 +223,7 @@ export class Application extends EggApplicationCore {
223223
* @see Context#runInBackground
224224
* @param {Function} scope - the first args is an anonymous ctx
225225
*/
226-
runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, req?: unknown) {
226+
runInBackground(scope: (ctx: Context) => Promise<void>, req?: unknown) {
227227
const ctx = this.createAnonymousContext(req);
228228
if (!scope.name) {
229229
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
@@ -239,7 +239,7 @@ export class Application extends EggApplicationCore {
239239
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
240240
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
241241
*/
242-
async runInAnonymousContextScope(scope: (ctx: ContextDelegation) => Promise<void>, req?: unknown) {
242+
async runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown) {
243243
const ctx = this.createAnonymousContext(req);
244244
if (!scope.name) {
245245
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));

src/lib/core/base_context_class.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseContextClass as EggCoreBaseContextClass } from '@eggjs/core';
2-
import type { ContextDelegation } from '../egg.js';
2+
import type { Context, EggApplicationCore } from '../egg.js';
33
import { BaseContextLogger } from './base_context_logger.js';
44

55
/**
@@ -8,8 +8,11 @@ import { BaseContextLogger } from './base_context_logger.js';
88
* {@link Helper}, {@link Service} is extending it.
99
*/
1010
export class BaseContextClass extends EggCoreBaseContextClass {
11-
declare ctx: ContextDelegation;
11+
[key: string | symbol]: any;
12+
declare ctx: Context;
1213
declare pathName?: string;
14+
declare app: EggApplicationCore;
15+
declare service: BaseContextClass;
1316
#logger?: BaseContextLogger;
1417

1518
get logger() {

src/lib/core/context_httpclient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { ContextDelegation, EggApplicationCore } from '../egg.js';
1+
import type { Context, EggApplicationCore } from '../egg.js';
22
import type {
33
HttpClientRequestURL, HttpClientRequestOptions,
44
} from './httpclient.js';
55

66
export class ContextHttpClient {
7-
ctx: ContextDelegation;
7+
ctx: Context;
88
app: EggApplicationCore;
99

10-
constructor(ctx: ContextDelegation) {
10+
constructor(ctx: Context) {
1111
this.ctx = ctx;
1212
this.app = ctx.app;
1313
}

0 commit comments

Comments
 (0)