Skip to content

Commit

Permalink
fix: ensure ContextInitiator be called after ctx ready
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Aug 11, 2023
1 parent f733441 commit ff1fc89
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import AppService from '../../modules/multi-module-service/AppService';
import { countMw } from '../middleware/count_mw';
import { logMwFactory } from '../middleware/log_mw';
import { callModuleCtx } from '../middleware/call_module';

@HTTPController({
path: '/middleware',
Expand All @@ -33,4 +34,13 @@ export class MiddlewareController {
async middleware() {
return {};
}

@HTTPMethod({
method: HTTPMethodEnum.GET,
path: '/methodCallModule',
})
@Middleware(callModuleCtx)
async middlewareCallModule() {
return {};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Context } from 'egg';
import { Next } from '@eggjs/tegg';

export async function callModuleCtx(ctx: Context, next: Next) {
await (ctx.module as any).multiModuleService.appService.findApp('foo');
await next();
}
7 changes: 7 additions & 0 deletions plugin/controller/test/http/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ describe('test/middleware.test.ts', () => {
assert(res.body.log === 'use middleware');
});
});

it('method middleware call module should work', async () => {
app.mockCsrf();
await app.httpRequest()
.get('/middleware/methodCallModule')
.expect(200);
});
});
8 changes: 8 additions & 0 deletions plugin/tegg/lib/EggContextCompatibleHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ export class EggContextCompatibleHook implements LifecycleHook<EggContextLifecyc
await EggContainerFactory.getOrCreateEggObject(protoObj as EggPrototype);
}
}

async postCreate(_, ctx: EggContext): Promise<void> {
const rootProto = ctx.get(ROOT_PROTO);
if (rootProto) {
// Ensure ContextInitiator be called.
await EggContainerFactory.getOrCreateEggObject(rootProto as EggPrototype);
}
}
}

0 comments on commit ff1fc89

Please sign in to comment.