Skip to content

Commit

Permalink
feat: supports async IoC container
Browse files Browse the repository at this point in the history
  • Loading branch information
agrumason committed Jul 27, 2021
1 parent 7c05b4d commit 801c697
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/cli/src/routeGeneration/templates/express.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export function RegisterRoutes(app: express.Router) {
{{#if uploadFiles}}
upload.array('{{uploadFilesName}}'),
{{/if}}
function {{../name}}_{{name}}(request: any, response: any, next: any) {

{{#if ../../iocModule}}async {{/if}}function {{../name}}_{{name}}(request: any, response: any, next: any) {
const args = {
{{#each parameters}}
{{@key}}: {{{json this}}},
Expand All @@ -87,7 +88,7 @@ export function RegisterRoutes(app: express.Router) {
{{#if ../../iocModule}}
const container: IocContainer = typeof iocContainer === 'function' ? (iocContainer as IocContainerFactory)(request) : iocContainer;

const controller: any = container.get<{{../name}}>({{../name}});
const controller: any = await container.get<{{../name}}>({{../name}});
if (typeof controller['setStatus'] === 'function') {
controller.setStatus(undefined);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/routeGeneration/templates/hapi.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function RegisterRoutes(server: any) {
allow: 'multipart/form-data'
},
{{/if}}
handler: function {{../name}}_{{name}}(request: any, h: any) {
handler: {{#if ../../iocModule}}async {{/if}}function {{../name}}_{{name}}(request: any, h: any) {
const args = {
{{#each parameters}}
{{@key}}: {{{json this}}},
Expand Down Expand Up @@ -119,7 +119,7 @@ export function RegisterRoutes(server: any) {
{{#if ../../iocModule}}
const container: IocContainer = typeof iocContainer === 'function' ? (iocContainer as IocContainerFactory)(request) : iocContainer;

const controller: any = container.get<{{../name}}>({{../name}});
const controller: any = await container.get<{{../name}}>({{../name}});
if (typeof controller['setStatus'] === 'function') {
controller.setStatus(undefined);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/routeGeneration/templates/koa.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function RegisterRoutes(router: KoaRouter) {
{{#if ../../iocModule}}
const container: IocContainer = typeof iocContainer === 'function' ? (iocContainer as IocContainerFactory)(context.request) : iocContainer;

const controller: any = container.get<{{../name}}>({{../name}});
const controller: any = await container.get<{{../name}}>({{../name}});
if (typeof controller['setStatus'] === 'function') {
controller.setStatus(undefined);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime/src/interfaces/iocModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface IocContainer {
get<T>(controller: { prototype: T }): T;

get<T>(controller: { prototype: T }): Promise<T>;
}

export type IocContainerFactory = (request: unknown) => IocContainer;

0 comments on commit 801c697

Please sign in to comment.