Skip to content

Commit

Permalink
fix: Wrapped middleware passed to medusa
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Feb 14, 2022
1 parent cebba50 commit 9a61b5c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
16 changes: 7 additions & 9 deletions src/loaders/middlewares.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
2 changes: 0 additions & 2 deletions src/loaders/tests/middlewares.loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
consume(req: MedusaAuthenticatedRequest | Request, res: Response, next: NextFunction): void | Promise<void>;
}

/**
Expand Down

0 comments on commit 9a61b5c

Please sign in to comment.