Skip to content

Commit 36c3bd0

Browse files
committed
fix: ensure ContextInitiator be called after ctx ready
1 parent f733441 commit 36c3bd0

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

plugin/controller/test/fixtures/apps/controller-app/app/controller/MiddlewareController.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import AppService from '../../modules/multi-module-service/AppService';
99
import { countMw } from '../middleware/count_mw';
1010
import { logMwFactory } from '../middleware/log_mw';
11+
import { callModuleCtx } from '../middleware/call_module';
1112

1213
@HTTPController({
1314
path: '/middleware',
@@ -33,4 +34,13 @@ export class MiddlewareController {
3334
async middleware() {
3435
return {};
3536
}
37+
38+
@HTTPMethod({
39+
method: HTTPMethodEnum.GET,
40+
path: '/methodCallModule',
41+
})
42+
@Middleware(callModuleCtx)
43+
async middlewareCallModule() {
44+
return {};
45+
}
3646
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Context } from 'egg';
2+
import { Next } from '@eggjs/tegg';
3+
4+
export async function callModuleCtx(ctx: Context, next: Next) {
5+
await (ctx.module as any).multiModuleService.appService.findApp('foo');
6+
await next();
7+
}

plugin/controller/test/http/middleware.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ describe('test/middleware.test.ts', () => {
4848
assert(res.body.log === 'use middleware');
4949
});
5050
});
51+
52+
it('method middleware call module should work', async () => {
53+
app.mockCsrf();
54+
await app.httpRequest()
55+
.get('/middleware/methodCallModule')
56+
.expect(200);
57+
});
5158
});

plugin/tegg/lib/EggContextCompatibleHook.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ export class EggContextCompatibleHook implements LifecycleHook<EggContextLifecyc
3535
await EggContainerFactory.getOrCreateEggObject(protoObj as EggPrototype);
3636
}
3737
}
38+
39+
async postCreate(_, ctx: EggContext): Promise<void> {
40+
const rootProto = ctx.get(ROOT_PROTO);
41+
if (rootProto) {
42+
// Ensure ContextInitiator be called.
43+
await EggContainerFactory.getOrCreateEggObject(rootProto as EggPrototype);
44+
}
45+
}
3846
}

0 commit comments

Comments
 (0)