diff --git a/package.json b/package.json index 3613f6f..b1791de 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "dependencies": { "@BridgemanAccessible/ba-auth": "^1.0.52", "@BridgemanAccessible/ba-logging": "^1.0.2", - "@BridgemanAccessible/ba-web-components": "^1.0.0", + "@BridgemanAccessible/ba-web-components": "^1.0.1", "express": "^4.19.2", "fs-extra": "^11.2.0", "jsdom": "^24.1.0", diff --git a/src/App.ts b/src/App.ts index ff03a7a..3012fe7 100644 --- a/src/App.ts +++ b/src/App.ts @@ -40,13 +40,12 @@ export class App { this.initializer = new Initializer(); } - await this.initializer.init(); - if(includeComponentLibrary) { - const componentsLibraryMiddleware = await useComponentsLibrary({ expressApp: this.getExpressApp() }) as RequestHandler; - this.getExpressApp().use(componentsLibraryMiddleware); + this.initializer.addMiddleware(async () => await useComponentsLibrary({ expressApp: this.getExpressApp() })); } + await this.initializer.init(); + // Start the server const port = process.env.PORT || this.DEFAULT_PORT; this.getExpressApp().listen(port, async () => { diff --git a/src/Initializer.ts b/src/Initializer.ts index a7cb04b..374d82b 100644 --- a/src/Initializer.ts +++ b/src/Initializer.ts @@ -24,7 +24,7 @@ export class Initializer { private view?: { filesPath: string, engine?: string }; /** The middlewares to use */ - private middlewares: ((...args: any[]) => RequestHandler)[]; + private middlewares: ((...args: any[]) => RequestHandler | Promise)[]; private app?: Application; private router?: Router; @@ -42,7 +42,7 @@ export class Initializer { * @param inputs.view.filesPath The path to the view files * @param middlewares Th middlewares to use */ - constructor(inputs?: { controllersPath?: string, staticFilesPath?: string, view?: { filesPath: string, engine?: string } }, ...middlewares: ((...args: any[]) => RequestHandler)[]) { + constructor(inputs?: { controllersPath?: string, staticFilesPath?: string, view?: { filesPath: string, engine?: string } }, ...middlewares: ((...args: any[]) => RequestHandler | Promise)[]) { this.controllersPath = typeof inputs !== 'undefined' && inputs.controllersPath !== 'undefined' ? inputs.controllersPath : undefined; this.staticFilesPath = typeof inputs !== 'undefined' && inputs.staticFilesPath !== 'undefined' ? inputs.staticFilesPath : undefined; this.view = typeof inputs !== 'undefined' && typeof inputs.view !== 'undefined' ? inputs.view : undefined; @@ -66,6 +66,10 @@ export class Initializer { return this.router; } + addMiddleware(...middlewares: ((...args: any[]) => RequestHandler | Promise)[]) { + this.middlewares.push(...middlewares); + } + /** * Create the Express app * @@ -84,7 +88,9 @@ export class Initializer { * @param app The Express app to setup the middleware on */ private async setupMiddleware(app: Application) { - this.middlewares.forEach(middleware => app.use(middleware())); + for (const middleware of this.middlewares) { + app.use(await middleware()); + } } /** diff --git a/yarn.lock b/yarn.lock index 42c5ef5..b8ed9eb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -44,10 +44,10 @@ axios "^1.8.4" express "^4.21.2" -"@BridgemanAccessible/ba-web-components@^1.0.0": - version "1.0.0" - resolved "https://npm.pkg.bridgemanaccessible.ca/@BridgemanAccessible/ba-web-components/-/ba-web-components-1.0.0.tgz#66d7f2ff68983bbdc2ed7d2252c232d768d6c928" - integrity sha512-wJ/nEh0J+wDQsYZkZFM58D74tmsJ5AYvaqVX4V6hBBPSbPu8mmkLH2fdb2Qn6mQi8lAV1xmuc/olSg0S82tujQ== +"@BridgemanAccessible/ba-web-components@^1.0.1": + version "1.0.1" + resolved "https://npm.pkg.bridgemanaccessible.ca/@BridgemanAccessible/ba-web-components/-/ba-web-components-1.0.1.tgz#d6ca2ea4a77df9c790592815c39d6d805b2db40e" + integrity sha512-KgcdCcNJ70vN2CixFJzx7jdGSoRtW4cEFpJcQrELcjrT4Uxhewwr2nmXKy1QzM4lDfo5njfRSIuDgB/HHropkQ== dependencies: ejs "^5.0.2" open-props "2.0.0-beta.5"