From 3b8aea4c443920690ddadd88c98350acfd583073 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Fri, 20 Sep 2024 14:50:04 +0200 Subject: [PATCH] fix(framework): throw when middleware fails to register --- .../framework/framework/src/http/router.ts | 55 ++++++++----------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/packages/framework/framework/src/http/router.ts b/packages/framework/framework/src/http/router.ts index ff83dc0f39315..5761c5c585d54 100644 --- a/packages/framework/framework/src/http/router.ts +++ b/packages/framework/framework/src/http/router.ts @@ -10,6 +10,9 @@ import { } from "express" import { readdir } from "fs/promises" import { extname, join, parse, sep } from "path" +import { configManager } from "../config" +import { logger } from "../logger" +import { authenticate, AuthType, errorHandler } from "./middlewares" import { GlobalMiddlewareDescriptor, HTTP_METHODS, @@ -25,9 +28,6 @@ import { RouteHandler, RouteVerb, } from "./types" -import { authenticate, AuthType, errorHandler } from "./middlewares" -import { configManager } from "../config" -import { logger } from "../logger" const log = ({ activityId, @@ -486,43 +486,32 @@ class ApiRoutesLoader { const absolutePath = join(this.#sourceDir, middlewareFilePath) - try { - await import(absolutePath).then((import_) => { - const middlewaresConfig = import_.default as - | MiddlewaresConfig - | undefined - - if (!middlewaresConfig) { - log({ - activityId: this.#activityId, - message: `No middleware configuration found in ${absolutePath}. Skipping middleware configuration.`, - }) - return - } + await import(absolutePath).then((import_) => { + const middlewaresConfig = import_.default as MiddlewaresConfig | undefined - middlewaresConfig.routes = middlewaresConfig.routes?.map((route) => { - return { - ...route, - method: route.method ?? "USE", - } + if (!middlewaresConfig) { + log({ + activityId: this.#activityId, + message: `No middleware configuration found in ${absolutePath}. Skipping middleware configuration.`, }) + return + } - const descriptor: GlobalMiddlewareDescriptor = { - config: middlewaresConfig, + middlewaresConfig.routes = middlewaresConfig.routes?.map((route) => { + return { + ...route, + method: route.method ?? "USE", } + }) - this.validateMiddlewaresConfig(descriptor) + const descriptor: GlobalMiddlewareDescriptor = { + config: middlewaresConfig, + } - this.#globalMiddlewaresDescriptor = descriptor - }) - } catch (error) { - log({ - activityId: this.#activityId, - message: `Failed to load middleware configuration in ${absolutePath}. Skipping middleware configuration.`, - }) + this.validateMiddlewaresConfig(descriptor) - return - } + this.#globalMiddlewaresDescriptor = descriptor + }) } protected async createRoutesMap(): Promise {