Skip to content

Commit 8b0765e

Browse files
committed
fix: remove ContextDelegation
1 parent e5204a5 commit 8b0765e

File tree

6 files changed

+48
-28
lines changed

6 files changed

+48
-28
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"homepage": "https://github.com/eggjs/core#readme",
4040
"dependencies": {
41-
"@eggjs/koa": "^2.20.2",
41+
"@eggjs/koa": "^2.20.3",
4242
"@eggjs/router": "^3.0.5",
4343
"@eggjs/utils": "^4.1.5",
4444
"egg-logger": "^3.5.0",
@@ -55,11 +55,11 @@
5555
},
5656
"devDependencies": {
5757
"@arethetypeswrong/cli": "^0.17.1",
58-
"@eggjs/bin": "^7.0.0",
58+
"@eggjs/bin": "7",
5959
"@eggjs/tsconfig": "1",
6060
"@types/js-yaml": "4",
6161
"@types/mocha": "10",
62-
"@types/node": "20",
62+
"@types/node": "22",
6363
"@types/supertest": "6",
6464
"await-event": "2",
6565
"coffee": "5",
@@ -68,7 +68,7 @@
6868
"gals": "1",
6969
"js-yaml": "3",
7070
"mm": "3",
71-
"pedding": "^2.0.0",
71+
"pedding": "2",
7272
"rimraf": "6",
7373
"supertest": "7",
7474
"ts-node": "10",

src/base_context_class.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import type { EggCore, ContextDelegation } from './egg.js';
1+
import type { EggCore, Context } from './egg.js';
22

33
/**
44
* BaseContextClass is a base class that can be extended,
55
* it's instantiated in context level,
66
* {@link Helper}, {@link Service} is extending it.
77
*/
8-
export class BaseContextClass {
9-
ctx: ContextDelegation;
8+
export class BaseContextClass<T extends Context = Context> {
9+
ctx: T;
1010
app: EggCore;
1111
config: Record<string, any>;
12-
service: BaseContextClass;
12+
service: BaseContextClass<T>;
1313

1414
/**
1515
* @since 1.0.0
1616
*/
17-
constructor(ctx: ContextDelegation) {
17+
constructor(ctx: T) {
1818
/**
1919
* @member {Context} BaseContextClass#ctx
2020
* @since 1.0.0
@@ -34,6 +34,6 @@ export class BaseContextClass {
3434
* @member {Service} BaseContextClass#service
3535
* @since 1.0.0
3636
*/
37-
this.service = ctx.service;
37+
this.service = ctx.service as BaseContextClass<T>;
3838
}
3939
}

src/egg.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Request as KoaRequest, Response as KoaResponse,
77
} from '@eggjs/koa';
88
import type {
9-
ContextDelegation as KoaContextDelegation,
109
MiddlewareFunc as KoaMiddlewareFunc,
1110
Next,
1211
} from '@eggjs/koa';
@@ -42,20 +41,18 @@ export {
4241

4342
// export @eggjs/koa types
4443
export type {
45-
Next, KoaMiddlewareFunc, KoaContextDelegation,
44+
Next, KoaMiddlewareFunc,
4645
};
4746

4847
// export @eggjs/core classes
4948
export class Request extends KoaRequest {
5049
declare app: EggCore;
5150
declare response: Response;
52-
declare ctx: ContextDelegation;
5351
}
5452

55-
export class Response extends KoaResponse {
53+
export class Response extends KoaResponse<Context> {
5654
declare app: EggCore;
5755
declare request: Request;
58-
declare ctx: ContextDelegation;
5956
}
6057

6158
export class Context extends KoaContext {
@@ -66,8 +63,7 @@ export class Context extends KoaContext {
6663
}
6764

6865
// export @eggjs/core types
69-
export type ContextDelegation = KoaContextDelegation & Context;
70-
export type MiddlewareFunc<T extends ContextDelegation = ContextDelegation> = KoaMiddlewareFunc<T>;
66+
export type MiddlewareFunc<T extends Context = Context> = KoaMiddlewareFunc<T>;
7167

7268
export class EggCore extends KoaApplication {
7369
options: EggCoreOptions;

src/loader/context_loader.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import assert from 'node:assert';
22
import { isClass, isPrimitive } from 'is-type-of';
33
import { FileLoader, EXPORTS, type FileLoaderOptions } from './file_loader.js';
4-
import type { ContextDelegation } from '../egg.js';
4+
import type { Context } from '../egg.js';
55

66
const CLASS_LOADER = Symbol('classLoader');
77

88
export interface ClassLoaderOptions {
9-
ctx: ContextDelegation;
9+
ctx: Context;
1010
properties: any;
1111
}
1212

1313
export class ClassLoader {
1414
readonly _cache = new Map();
15-
_ctx: ContextDelegation;
15+
_ctx: Context;
1616

1717
constructor(options: ClassLoaderOptions) {
1818
assert(options.ctx, 'options.ctx is required');
@@ -98,7 +98,7 @@ export class ContextLoader extends FileLoader {
9898
}
9999
}
100100

101-
function getInstance(values: any, ctx: ContextDelegation) {
101+
function getInstance(values: any, ctx: Context) {
102102
// it's a directory when it has no exports
103103
// then use ClassLoader
104104
const Class = values[EXPORTS] ? values : null;

src/loader/egg_loader.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ import { isAsyncFunction, isClass, isGeneratorFunction, isObject, isPromise } fr
77
import type { Logger } from 'egg-logger';
88
import { getParamNames, readJSONSync, readJSON } from 'utility';
99
import { extend } from 'extend2';
10-
import { Request, Response, Context, Application } from '@eggjs/koa';
10+
import { Request, Response, Application, Context as KoaContext } from '@eggjs/koa';
1111
import { pathMatching, type PathMatchingOptions } from 'egg-path-matching';
1212
import { now, diff } from 'performance-ms';
1313
import { FULLPATH, FileLoader, FileLoaderOptions } from './file_loader.js';
1414
import { ContextLoader, ContextLoaderOptions } from './context_loader.js';
1515
import utils, { Fun } from '../utils/index.js';
1616
import sequencify from '../utils/sequencify.js';
1717
import { Timing } from '../utils/timing.js';
18-
import type { ContextDelegation, EggCore, MiddlewareFunc } from '../egg.js';
19-
import { BaseContextClass } from '../base_context_class.js';
18+
import type {
19+
Context, EggCore, MiddlewareFunc,
20+
} from '../egg.js';
21+
import type { BaseContextClass } from '../base_context_class.js';
2022

2123
const debug = debuglog('@eggjs/core/loader/egg_loader');
2224

2325
const originalPrototypes: Record<string, any> = {
2426
request: Request.prototype,
2527
response: Response.prototype,
26-
context: Context.prototype,
28+
context: KoaContext.prototype,
2729
application: Application.prototype,
2830
};
2931

@@ -1691,7 +1693,7 @@ function wrapControllerClass(Controller: typeof BaseContextClass, fullPath: stri
16911693
}
16921694

16931695
function controllerMethodToMiddleware(Controller: typeof BaseContextClass, key: string) {
1694-
return function classControllerMiddleware(this: ContextDelegation, ...args: any[]) {
1696+
return function classControllerMiddleware(this: Context, ...args: any[]) {
16951697
const controller: any = new Controller(this);
16961698
if (!this.app.config.controller?.supportParams) {
16971699
args = [ this ];
@@ -1727,7 +1729,7 @@ function wrapObject(obj: Record<string, any>, fullPath: string, prefix?: string)
17271729
}
17281730

17291731
function objectFunctionToMiddleware(func: Fun) {
1730-
async function objectControllerMiddleware(this: ContextDelegation, ...args: any[]) {
1732+
async function objectControllerMiddleware(this: Context, ...args: any[]) {
17311733
if (!this.app.config.controller?.supportParams) {
17321734
args = [ this ];
17331735
}

test/index.test.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,32 @@ import * as EggCore from '../src/index.js';
33

44
describe('test/index.test.ts', () => {
55
it('should expose properties', () => {
6-
// console.log(EggCore);
76
assert(EggCore.EggCore);
87
assert(EggCore.EggLoader);
98
assert(EggCore.BaseContextClass);
109
assert(EggCore.utils);
10+
console.log(Object.keys(EggCore));
11+
assert.deepEqual(Object.keys(EggCore), [
12+
'BaseContextClass',
13+
'ClassLoader',
14+
'Context',
15+
'ContextLoader',
16+
'EGG_LOADER',
17+
'EXPORTS',
18+
'EggCore',
19+
'EggLoader',
20+
'FULLPATH',
21+
'FileLoader',
22+
'KoaApplication',
23+
'KoaContext',
24+
'KoaRequest',
25+
'KoaResponse',
26+
'Lifecycle',
27+
'Request',
28+
'Response',
29+
'Router',
30+
'Timing',
31+
'utils',
32+
]);
1133
});
1234
});

0 commit comments

Comments
 (0)