Skip to content

Commit

Permalink
fix: remove ContextDelegation (#283)
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

- **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 -->
  • Loading branch information
fengmk2 authored Jan 2, 2025
1 parent e5204a5 commit c56cf7b
Show file tree
Hide file tree
Showing 28 changed files with 77 additions and 58 deletions.
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"homepage": "https://github.com/eggjs/core#readme",
"dependencies": {
"@eggjs/koa": "^2.20.2",
"@eggjs/koa": "^2.20.3",
"@eggjs/router": "^3.0.5",
"@eggjs/utils": "^4.1.5",
"egg-logger": "^3.5.0",
Expand All @@ -55,22 +55,21 @@
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.1",
"@eggjs/bin": "^7.0.0",
"@eggjs/bin": "7",
"@eggjs/supertest": "^8.1.1",
"@eggjs/tsconfig": "1",
"@types/js-yaml": "4",
"@types/mocha": "10",
"@types/node": "20",
"@types/supertest": "6",
"@types/node": "22",
"await-event": "2",
"coffee": "5",
"eslint": "8",
"eslint-config-egg": "14",
"gals": "1",
"js-yaml": "3",
"mm": "3",
"pedding": "^2.0.0",
"mm": "^4.0.2",
"pedding": "2",
"rimraf": "6",
"supertest": "7",
"ts-node": "10",
"tshy": "3",
"tshy-after": "1",
Expand Down
12 changes: 6 additions & 6 deletions src/base_context_class.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type { EggCore, ContextDelegation } from './egg.js';
import type { EggCore, Context } from './egg.js';

/**
* BaseContextClass is a base class that can be extended,
* it's instantiated in context level,
* {@link Helper}, {@link Service} is extending it.
*/
export class BaseContextClass {
ctx: ContextDelegation;
export class BaseContextClass<T extends Context = Context> {
ctx: T;
app: EggCore;
config: Record<string, any>;
service: BaseContextClass;
service: BaseContextClass<T>;

/**
* @since 1.0.0
*/
constructor(ctx: ContextDelegation) {
constructor(ctx: T) {
/**
* @member {Context} BaseContextClass#ctx
* @since 1.0.0
Expand All @@ -34,6 +34,6 @@ export class BaseContextClass {
* @member {Service} BaseContextClass#service
* @since 1.0.0
*/
this.service = ctx.service;
this.service = ctx.service as BaseContextClass<T>;
}
}
10 changes: 3 additions & 7 deletions src/egg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Request as KoaRequest, Response as KoaResponse,
} from '@eggjs/koa';
import type {
ContextDelegation as KoaContextDelegation,
MiddlewareFunc as KoaMiddlewareFunc,
Next,
} from '@eggjs/koa';
Expand Down Expand Up @@ -42,20 +41,18 @@ export {

// export @eggjs/koa types
export type {
Next, KoaMiddlewareFunc, KoaContextDelegation,
Next, KoaMiddlewareFunc,
};

// export @eggjs/core classes
export class Request extends KoaRequest {
declare app: EggCore;
declare response: Response;
declare ctx: ContextDelegation;
}

export class Response extends KoaResponse {
export class Response extends KoaResponse<Context> {
declare app: EggCore;
declare request: Request;
declare ctx: ContextDelegation;
}

export class Context extends KoaContext {
Expand All @@ -66,8 +63,7 @@ export class Context extends KoaContext {
}

// export @eggjs/core types
export type ContextDelegation = KoaContextDelegation & Context;
export type MiddlewareFunc<T extends ContextDelegation = ContextDelegation> = KoaMiddlewareFunc<T>;
export type MiddlewareFunc<T extends Context = Context> = KoaMiddlewareFunc<T>;

export class EggCore extends KoaApplication {
options: EggCoreOptions;
Expand Down
8 changes: 4 additions & 4 deletions src/loader/context_loader.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import assert from 'node:assert';
import { isClass, isPrimitive } from 'is-type-of';
import { FileLoader, EXPORTS, type FileLoaderOptions } from './file_loader.js';
import type { ContextDelegation } from '../egg.js';
import type { Context } from '../egg.js';

const CLASS_LOADER = Symbol('classLoader');

export interface ClassLoaderOptions {
ctx: ContextDelegation;
ctx: Context;
properties: any;
}

export class ClassLoader {
readonly _cache = new Map();
_ctx: ContextDelegation;
_ctx: Context;

constructor(options: ClassLoaderOptions) {
assert(options.ctx, 'options.ctx is required');
Expand Down Expand Up @@ -98,7 +98,7 @@ export class ContextLoader extends FileLoader {
}
}

function getInstance(values: any, ctx: ContextDelegation) {
function getInstance(values: any, ctx: Context) {
// it's a directory when it has no exports
// then use ClassLoader
const Class = values[EXPORTS] ? values : null;
Expand Down
14 changes: 8 additions & 6 deletions src/loader/egg_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import { isAsyncFunction, isClass, isGeneratorFunction, isObject, isPromise } fr
import type { Logger } from 'egg-logger';
import { getParamNames, readJSONSync, readJSON } from 'utility';
import { extend } from 'extend2';
import { Request, Response, Context, Application } from '@eggjs/koa';
import { Request, Response, Application, Context as KoaContext } from '@eggjs/koa';
import { pathMatching, type PathMatchingOptions } from 'egg-path-matching';
import { now, diff } from 'performance-ms';
import { FULLPATH, FileLoader, FileLoaderOptions } from './file_loader.js';
import { ContextLoader, ContextLoaderOptions } from './context_loader.js';
import utils, { Fun } from '../utils/index.js';
import sequencify from '../utils/sequencify.js';
import { Timing } from '../utils/timing.js';
import type { ContextDelegation, EggCore, MiddlewareFunc } from '../egg.js';
import { BaseContextClass } from '../base_context_class.js';
import type {
Context, EggCore, MiddlewareFunc,
} from '../egg.js';
import type { BaseContextClass } from '../base_context_class.js';

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

const originalPrototypes: Record<string, any> = {
request: Request.prototype,
response: Response.prototype,
context: Context.prototype,
context: KoaContext.prototype,
application: Application.prototype,
};

Expand Down Expand Up @@ -1691,7 +1693,7 @@ function wrapControllerClass(Controller: typeof BaseContextClass, fullPath: stri
}

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

function objectFunctionToMiddleware(func: Fun) {
async function objectControllerMiddleware(this: ContextDelegation, ...args: any[]) {
async function objectControllerMiddleware(this: Context, ...args: any[]) {
if (!this.app.config.controller?.supportParams) {
args = [ this ];
}
Expand Down
2 changes: 1 addition & 1 deletion test/asyncLocalStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'node:assert';
import { AsyncLocalStorage } from 'node:async_hooks';
import request from 'supertest';
import { request } from '@eggjs/supertest';
import { getAsyncLocalStorage, kGALS } from 'gals';
import { getFilepath } from './helper.js';
import { Application } from './fixtures/egg-esm/index.js';
Expand Down
4 changes: 2 additions & 2 deletions test/egg-ts.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'node:assert';
import mm from 'mm';
import request from 'supertest';
import { mm } from 'mm';
import { request } from '@eggjs/supertest';
import coffee from 'coffee';
import { utils } from '../src/index.js';
import { Application, createApp, getFilepath } from './helper.js';
Expand Down
4 changes: 2 additions & 2 deletions test/egg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import path from 'node:path';
import { strict as assert } from 'node:assert';
import fs from 'node:fs/promises';
import { setTimeout as sleep } from 'node:timers/promises';
import mm from 'mm';
import request from 'supertest';
import { mm } from 'mm';
import { request } from '@eggjs/supertest';
import { pending } from 'pedding';
import coffee from 'coffee';
import { createApp, getFilepath, Application } from './helper.js';
Expand Down
24 changes: 23 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@ import * as EggCore from '../src/index.js';

describe('test/index.test.ts', () => {
it('should expose properties', () => {
// console.log(EggCore);
assert(EggCore.EggCore);
assert(EggCore.EggLoader);
assert(EggCore.BaseContextClass);
assert(EggCore.utils);
console.log(Object.keys(EggCore));
assert.deepEqual(Object.keys(EggCore), [
'BaseContextClass',
'ClassLoader',
'Context',
'ContextLoader',
'EGG_LOADER',
'EXPORTS',
'EggCore',
'EggLoader',
'FULLPATH',
'FileLoader',
'KoaApplication',
'KoaContext',
'KoaRequest',
'KoaResponse',
'Lifecycle',
'Request',
'Response',
'Router',
'Timing',
'utils',
]);
});
});
2 changes: 1 addition & 1 deletion test/loader/context_loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import request from 'supertest';
import { request } from '@eggjs/supertest';
import { getFilepath, createApp, Application } from '../helper.js';

describe('test/loader/context_loader.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/loader/egg_loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { strict as assert } from 'node:assert';
import os from 'node:os';
import path from 'node:path';
import mm from 'mm';
import { mm } from 'mm';
import { getPlugins } from '@eggjs/utils';
import { Application, createApp, getFilepath } from '../helper.js';
import { EggLoader } from '../../src/index.js';
Expand Down
2 changes: 1 addition & 1 deletion test/loader/get_app_info.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import mm from 'mm';
import { mm } from 'mm';
import { Application, createApp, getFilepath } from '../helper.js';

describe('test/loader/get_app_info.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/loader/get_framework_paths.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import mm from 'mm';
import { mm } from 'mm';
import { importModule } from '@eggjs/utils';
import { Application, createApp, getFilepath } from '../helper.js';
import { EggLoader, EggCore } from '../../src/index.js';
Expand Down
2 changes: 1 addition & 1 deletion test/loader/get_load_units.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import mm from 'mm';
import { mm } from 'mm';
import { Application, createApp, getFilepath } from '../helper.js';

describe('test/loader/get_load_units.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/loader/get_server_env.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import mm from 'mm';
import { mm } from 'mm';
import { Application, createApp } from '../helper.js';

describe('test/loader/get_server_env.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/loader/load_file.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import mm from 'mm';
import { mm } from 'mm';
import { createApp, Application, getFilepath } from '../helper.js';

describe('test/loader/load_file.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/loader/mixin/load_config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { strict as assert } from 'node:assert';
import mm from 'mm';
import { mm } from 'mm';
import { EggCore } from '../../../src/index.js';
import { Application, createApp, getFilepath } from '../../helper.js';

Expand Down
2 changes: 1 addition & 1 deletion test/loader/mixin/load_controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'node:assert';
import path from 'node:path';
import request from 'supertest';
import { request } from '@eggjs/supertest';
import is from 'is-type-of';
import { Application, createApp, getFilepath } from '../../helper.js';

Expand Down
2 changes: 1 addition & 1 deletion test/loader/mixin/load_custom_loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import request from 'supertest';
import { request } from '@eggjs/supertest';
import { Application, createApp } from '../../helper.js';

describe('test/loader/mixin/load_custom_loader.test.ts', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/loader/mixin/load_extend.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'node:assert';
import request from 'supertest';
import mm from 'mm';
import { request } from '@eggjs/supertest';
import { mm } from 'mm';
import { Application, createApp } from '../../helper.js';

describe('test/loader/mixin/load_extend.test.ts', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/loader/mixin/load_extend_class.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'node:assert';
import request from 'supertest';
import mm from 'mm';
import { request } from '@eggjs/supertest';
import { mm } from 'mm';
import { Application, createApp } from '../../helper.js';

describe('test/loader/mixin/load_extend_class.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/loader/mixin/load_helper_extend.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import request from 'supertest';
import { request } from '@eggjs/supertest';
import { Application, createApp } from '../../helper.js';

describe('test/loader/mixin/load_helper_extend.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/loader/mixin/load_middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { strict as assert } from 'node:assert';
import request from 'supertest';
import { request } from '@eggjs/supertest';
import { Application, createApp, getFilepath } from '../../helper.js';

describe('test/loader/mixin/load_middleware.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/loader/mixin/load_plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import fs from 'node:fs';
import { strict as assert } from 'node:assert';
import mm from 'mm';
import { mm } from 'mm';
import { Application, createApp, getFilepath } from '../../helper.js';
import { EggCore, EggLoader } from '../../../src/index.js';

Expand Down
4 changes: 2 additions & 2 deletions test/loader/mixin/load_service.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import { strict as assert } from 'node:assert';
import request from 'supertest';
import mm from 'mm';
import { request } from '@eggjs/supertest';
import { mm } from 'mm';
import { Application, createApp, getFilepath } from '../../helper.js';

describe('test/loader/mixin/load_service.test.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/support-typescript.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import request from 'supertest';
import { request } from '@eggjs/supertest';
import { getFilepath } from './helper.js';
import { Application } from './fixtures/egg-esm/index.js';

Expand Down
2 changes: 1 addition & 1 deletion test/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { strict as assert } from 'node:assert';
import mm from 'mm';
import { mm } from 'mm';
import utils from '../../src/utils/index.js';
import { getFilepath } from '../helper.js';

Expand Down
2 changes: 1 addition & 1 deletion test/utils/router.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import request from 'supertest';
import { request } from '@eggjs/supertest';
import { Application, createApp } from '../helper.js';

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

0 comments on commit c56cf7b

Please sign in to comment.