Skip to content

Commit 85a9254

Browse files
authored
fix: add missing app property (#4422)
1 parent f8dda5f commit 85a9254

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

packages/web-koa/src/framework.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
IMidwayKoaApplication,
2020
IMidwayKoaConfigurationOptions,
2121
IMidwayKoaContext,
22-
IWebMiddleware,
2322
} from './interface';
2423
import * as Router from '@koa/router';
2524
import type { DefaultState, Middleware, Next } from 'koa';
@@ -234,7 +233,10 @@ export class MidwayKoaFramework extends BaseFramework<
234233
this.webRouterService
235234
);
236235

237-
this.defineApplicationProperties();
236+
this.defineApplicationProperties({
237+
generateController: this.generateController.bind(this),
238+
getPort: this.getPort.bind(this),
239+
});
238240

239241
// hack use method
240242
(this.app as any).originUse = this.app.use;
@@ -260,17 +262,6 @@ export class MidwayKoaFramework extends BaseFramework<
260262
return this.generator.generateKoaController(routeInfo);
261263
}
262264

263-
/**
264-
* @deprecated
265-
* @param middlewareId
266-
*/
267-
public async generateMiddleware(middlewareId: any) {
268-
const mwIns = await this.getApplicationContext().getAsync<IWebMiddleware>(
269-
middlewareId
270-
);
271-
return mwIns.resolve();
272-
}
273-
274265
public async run(): Promise<void> {
275266
// load controller
276267
await this.loadMidwayController();

packages/web-koa/src/interface.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ export type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext, koa<St
1414
routeArgsInfo?: RouterParamValue[],
1515
routerResponseData?: any []
1616
): Middleware<State, IMidwayKoaContext>;
17-
/**
18-
* @deprecated
19-
* @param middlewareId
20-
*/
21-
generateMiddleware(middlewareId: any): Promise<Middleware<State, IMidwayKoaContext>>;
2217
/**
2318
* Get the port that the application is listening on
2419
*/

packages/web-koa/test/index.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,5 +774,37 @@ describe('/test/feature.test.ts', () => {
774774

775775
await closeApp(app);
776776
});
777+
778+
it('should test app methods existence', async () => {
779+
@Controller()
780+
class HomeController {
781+
@Get('/')
782+
async query() {
783+
return 'hello world'
784+
}
785+
}
786+
const app = await createLightApp('', {
787+
imports: [
788+
require('../src'),
789+
],
790+
preloadModules: [
791+
HomeController
792+
],
793+
globalConfig: {
794+
keys: '123',
795+
koa: {
796+
port: null, // 不监听端口
797+
}
798+
}
799+
}) as IMidwayKoaApplication;
800+
801+
await sleep();
802+
803+
// 验证 app 实例上的方法是否存在
804+
expect(typeof app.generateController).toBe('function');
805+
expect(typeof app.getPort).toBe('function');
806+
807+
await closeApp(app);
808+
});
777809
});
778810
});

0 commit comments

Comments
 (0)