From 9a61b5c5dde5d9e0295a95c2e7cd8701206e77bc Mon Sep 17 00:00:00 2001 From: adrien2p Date: Mon, 14 Feb 2022 14:16:43 +0100 Subject: [PATCH] fix: Wrapped middleware passed to medusa --- src/loaders/middlewares.loader.ts | 16 +++++++--------- src/loaders/tests/middlewares.loader.spec.ts | 2 -- src/types.ts | 7 +------ 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/loaders/middlewares.loader.ts b/src/loaders/middlewares.loader.ts index 0bb658fc..df3d5cbe 100644 --- a/src/loaders/middlewares.loader.ts +++ b/src/loaders/middlewares.loader.ts @@ -39,18 +39,16 @@ export function middlewaresLoader( export function wrapMiddleware( middleware: GetInjectableOption<'middleware'> ): (options: { app: Express }) => (...args: unknown[]) => void { - return (): (( - err: unknown, - req: MedusaRequest | MedusaAuthenticatedRequest, - res: Response, - next: NextFunction - ) => void) => { - return (err: unknown, req: MedusaRequest | MedusaAuthenticatedRequest, res: Response, next: NextFunction) => { + return (): ((req: MedusaRequest | MedusaAuthenticatedRequest, res: Response, next: NextFunction) => void) => { + return (req: MedusaRequest | MedusaAuthenticatedRequest, res: Response, next: NextFunction) => { const shouldHandle = middleware.routes.some((route) => { - return req.method.toLowerCase() === route.method.toLowerCase() && req.path === route.path; + return ( + (route.method === 'all' || req.method.toLowerCase() === route.method.toLowerCase()) && + (route.path === '*' || req.path === route.path) + ); }); if (shouldHandle) { - return new middleware.metatype().consume(err, req, res, next); + return new middleware.metatype().consume(req, res, next); } return next(); }; diff --git a/src/loaders/tests/middlewares.loader.spec.ts b/src/loaders/tests/middlewares.loader.spec.ts index 18b95aeb..790cd4e1 100644 --- a/src/loaders/tests/middlewares.loader.spec.ts +++ b/src/loaders/tests/middlewares.loader.spec.ts @@ -20,7 +20,6 @@ const MiddlewareServiceMock = { class PreAuthUserMiddleware implements MedusaMiddleware { /* eslint-disable @typescript-eslint/no-unused-vars */ consume( - err: unknown, req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction @@ -33,7 +32,6 @@ class PreAuthUserMiddleware implements MedusaMiddleware { class PostAuthUserMiddleware implements MedusaMiddleware { /* eslint-disable @typescript-eslint/no-unused-vars */ consume( - err: unknown, req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction diff --git a/src/types.ts b/src/types.ts index 91689b1b..afa36f2e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -149,12 +149,7 @@ export type MedusaRouteOptions = { * Describe a custom middleware instance. */ export interface MedusaMiddleware { - consume( - err: unknown, - req: MedusaAuthenticatedRequest | Request, - res: Response, - next: NextFunction - ): void | Promise; + consume(req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction): void | Promise; } /**