diff --git a/packages/core/src/util/webRouterCollector.ts b/packages/core/src/util/webRouterCollector.ts index e5ec910bae20..ba2f24f05bf3 100644 --- a/packages/core/src/util/webRouterCollector.ts +++ b/packages/core/src/util/webRouterCollector.ts @@ -19,6 +19,7 @@ import { } from '@midwayjs/decorator'; import { MidwayContainer } from '../context/midwayContainer'; import { joinURLPath } from './index'; +import { IMidwayContainer } from '../interface'; export interface RouterInfo { /** @@ -103,6 +104,7 @@ export class WebRouterCollector { protected routes = new Map(); private routesPriority: RouterPriority[] = []; protected options: RouterCollectorOptions; + private applicationContext: IMidwayContainer; constructor(baseDir = '', options: RouterCollectorOptions = {}) { this.baseDir = baseDir; @@ -115,6 +117,10 @@ export class WebRouterCollector { await framework.initialize({ baseDir: this.baseDir, }); + + this.applicationContext = framework.getApplicationContext(); + } else { + this.applicationContext = MidwayContainer.parentApplicationContext; } const controllerModules = listModule(CONTROLLER_KEY); @@ -142,6 +148,10 @@ export class WebRouterCollector { }); } + public getApplicationContext() { + return this.applicationContext; + } + protected collectRoute(module, functionMeta = false) { const controllerId = getProviderId(module); const controllerOption: ControllerOption = getClassMetadata( diff --git a/packages/core/test/util/triggerCollector.test.ts b/packages/core/test/util/triggerCollector.test.ts index 7c4eb56f9b2b..e6cc2195e373 100644 --- a/packages/core/test/util/triggerCollector.test.ts +++ b/packages/core/test/util/triggerCollector.test.ts @@ -11,6 +11,9 @@ describe('/test/util/triggerCollector.test.ts', function () { clearContainerCache(); const collector = new ServerlessTriggerCollector(join(__dirname, '../fixtures/base-app-func-router/src')); const result = await collector.getFunctionList(); + expect(collector.getApplicationContext()).not.toBeNull(); + expect(collector.getApplicationContext()).not.toBeUndefined(); + expect(matchObjectPropertyInArray(result, { "controllerId": "apiController", "controllerMiddleware": [