Skip to content

Commit c56cf7b

Browse files
authored
fix: remove ContextDelegation (#283)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Dependencies** - Updated `@eggjs/koa` to version 2.20.3 - Updated `@eggjs/bin` to version 7 - Updated `@types/node` to version 22 - Updated `pedding` to version 2 - Added new dependency `@eggjs/supertest` with version 8.1.1 - **Type Improvements** - Enhanced type flexibility for `BaseContextClass` - Streamlined context-related type definitions - Improved type safety across multiple components - **Testing** - Added new assertions to verify `EggCore` module properties - Updated import statements for `request` and `mm` modules across multiple test files <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent e5204a5 commit c56cf7b

28 files changed

+77
-58
lines changed

package.json

+6-7
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,22 +55,21 @@
5555
},
5656
"devDependencies": {
5757
"@arethetypeswrong/cli": "^0.17.1",
58-
"@eggjs/bin": "^7.0.0",
58+
"@eggjs/bin": "7",
59+
"@eggjs/supertest": "^8.1.1",
5960
"@eggjs/tsconfig": "1",
6061
"@types/js-yaml": "4",
6162
"@types/mocha": "10",
62-
"@types/node": "20",
63-
"@types/supertest": "6",
63+
"@types/node": "22",
6464
"await-event": "2",
6565
"coffee": "5",
6666
"eslint": "8",
6767
"eslint-config-egg": "14",
6868
"gals": "1",
6969
"js-yaml": "3",
70-
"mm": "3",
71-
"pedding": "^2.0.0",
70+
"mm": "^4.0.2",
71+
"pedding": "2",
7272
"rimraf": "6",
73-
"supertest": "7",
7473
"ts-node": "10",
7574
"tshy": "3",
7675
"tshy-after": "1",

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/asyncLocalStorage.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { strict as assert } from 'node:assert';
22
import { AsyncLocalStorage } from 'node:async_hooks';
3-
import request from 'supertest';
3+
import { request } from '@eggjs/supertest';
44
import { getAsyncLocalStorage, kGALS } from 'gals';
55
import { getFilepath } from './helper.js';
66
import { Application } from './fixtures/egg-esm/index.js';

test/egg-ts.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { strict as assert } from 'node:assert';
2-
import mm from 'mm';
3-
import request from 'supertest';
2+
import { mm } from 'mm';
3+
import { request } from '@eggjs/supertest';
44
import coffee from 'coffee';
55
import { utils } from '../src/index.js';
66
import { Application, createApp, getFilepath } from './helper.js';

test/egg.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import path from 'node:path';
33
import { strict as assert } from 'node:assert';
44
import fs from 'node:fs/promises';
55
import { setTimeout as sleep } from 'node:timers/promises';
6-
import mm from 'mm';
7-
import request from 'supertest';
6+
import { mm } from 'mm';
7+
import { request } from '@eggjs/supertest';
88
import { pending } from 'pedding';
99
import coffee from 'coffee';
1010
import { createApp, getFilepath, Application } from './helper.js';

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
});

test/loader/context_loader.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import request from 'supertest';
1+
import { request } from '@eggjs/supertest';
22
import { getFilepath, createApp, Application } from '../helper.js';
33

44
describe('test/loader/context_loader.test.ts', () => {

test/loader/egg_loader.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { strict as assert } from 'node:assert';
22
import os from 'node:os';
33
import path from 'node:path';
4-
import mm from 'mm';
4+
import { mm } from 'mm';
55
import { getPlugins } from '@eggjs/utils';
66
import { Application, createApp, getFilepath } from '../helper.js';
77
import { EggLoader } from '../../src/index.js';

test/loader/get_app_info.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'node:assert';
2-
import mm from 'mm';
2+
import { mm } from 'mm';
33
import { Application, createApp, getFilepath } from '../helper.js';
44

55
describe('test/loader/get_app_info.test.ts', () => {

test/loader/get_framework_paths.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'node:assert';
2-
import mm from 'mm';
2+
import { mm } from 'mm';
33
import { importModule } from '@eggjs/utils';
44
import { Application, createApp, getFilepath } from '../helper.js';
55
import { EggLoader, EggCore } from '../../src/index.js';

test/loader/get_load_units.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'node:assert';
2-
import mm from 'mm';
2+
import { mm } from 'mm';
33
import { Application, createApp, getFilepath } from '../helper.js';
44

55
describe('test/loader/get_load_units.test.ts', () => {

test/loader/get_server_env.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'node:assert';
2-
import mm from 'mm';
2+
import { mm } from 'mm';
33
import { Application, createApp } from '../helper.js';
44

55
describe('test/loader/get_server_env.test.ts', () => {

test/loader/load_file.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'node:assert';
2-
import mm from 'mm';
2+
import { mm } from 'mm';
33
import { createApp, Application, getFilepath } from '../helper.js';
44

55
describe('test/loader/load_file.test.ts', () => {

test/loader/mixin/load_config.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22
import { strict as assert } from 'node:assert';
3-
import mm from 'mm';
3+
import { mm } from 'mm';
44
import { EggCore } from '../../../src/index.js';
55
import { Application, createApp, getFilepath } from '../../helper.js';
66

test/loader/mixin/load_controller.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { strict as assert } from 'node:assert';
22
import path from 'node:path';
3-
import request from 'supertest';
3+
import { request } from '@eggjs/supertest';
44
import is from 'is-type-of';
55
import { Application, createApp, getFilepath } from '../../helper.js';
66

test/loader/mixin/load_custom_loader.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'node:assert';
2-
import request from 'supertest';
2+
import { request } from '@eggjs/supertest';
33
import { Application, createApp } from '../../helper.js';
44

55
describe('test/loader/mixin/load_custom_loader.test.ts', () => {

test/loader/mixin/load_extend.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { strict as assert } from 'node:assert';
2-
import request from 'supertest';
3-
import mm from 'mm';
2+
import { request } from '@eggjs/supertest';
3+
import { mm } from 'mm';
44
import { Application, createApp } from '../../helper.js';
55

66
describe('test/loader/mixin/load_extend.test.ts', () => {

test/loader/mixin/load_extend_class.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { strict as assert } from 'node:assert';
2-
import request from 'supertest';
3-
import mm from 'mm';
2+
import { request } from '@eggjs/supertest';
3+
import { mm } from 'mm';
44
import { Application, createApp } from '../../helper.js';
55

66
describe('test/loader/mixin/load_extend_class.test.ts', () => {

test/loader/mixin/load_helper_extend.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import request from 'supertest';
1+
import { request } from '@eggjs/supertest';
22
import { Application, createApp } from '../../helper.js';
33

44
describe('test/loader/mixin/load_helper_extend.test.ts', () => {

test/loader/mixin/load_middleware.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22
import { strict as assert } from 'node:assert';
3-
import request from 'supertest';
3+
import { request } from '@eggjs/supertest';
44
import { Application, createApp, getFilepath } from '../../helper.js';
55

66
describe('test/loader/mixin/load_middleware.test.ts', () => {

test/loader/mixin/load_plugin.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path';
22
import fs from 'node:fs';
33
import { strict as assert } from 'node:assert';
4-
import mm from 'mm';
4+
import { mm } from 'mm';
55
import { Application, createApp, getFilepath } from '../../helper.js';
66
import { EggCore, EggLoader } from '../../../src/index.js';
77

test/loader/mixin/load_service.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path';
22
import { strict as assert } from 'node:assert';
3-
import request from 'supertest';
4-
import mm from 'mm';
3+
import { request } from '@eggjs/supertest';
4+
import { mm } from 'mm';
55
import { Application, createApp, getFilepath } from '../../helper.js';
66

77
describe('test/loader/mixin/load_service.test.ts', () => {

test/support-typescript.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'node:assert';
2-
import request from 'supertest';
2+
import { request } from '@eggjs/supertest';
33
import { getFilepath } from './helper.js';
44
import { Application } from './fixtures/egg-esm/index.js';
55

test/utils/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22
import { strict as assert } from 'node:assert';
3-
import mm from 'mm';
3+
import { mm } from 'mm';
44
import utils from '../../src/utils/index.js';
55
import { getFilepath } from '../helper.js';
66

test/utils/router.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'node:assert';
2-
import request from 'supertest';
2+
import { request } from '@eggjs/supertest';
33
import { Application, createApp } from '../helper.js';
44

55
describe('test/utils/router.test.ts', () => {

0 commit comments

Comments
 (0)