Skip to content

Commit

Permalink
Emit correct middleware precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
skonves committed Oct 9, 2024
1 parent 323fb0a commit 665c064
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/router-factory-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ function getMiddleware<
if (!middleware) return [];
const middlewareArray = Array.isArray(middleware) ? middleware : [middleware];
return middlewareArray.flatMap((m) => (m[name] ? m[name] : []));
return middlewareArray.flatMap((m) => {
const _exceptSwaggerUI =
m['_exceptSwaggerUI'] && name !== '_onlySwaggerUI'
? m['_exceptSwaggerUI']
: [];
const base = m[name] ? m[name] : [];
const baseArray = Array.isArray(base) ? base : [base];
return [...baseArray, ..._exceptSwaggerUI];
});
}
/**
Expand Down Expand Up @@ -116,7 +125,6 @@ function getHandlers<TRequestHandler extends RequestHandler>(
>(methodName: TName, defaultHandler: RequestHandler): RequestHandler[] {
return [
...getMiddleware(middleware, methodName),
...getMiddleware(middleware, '_exceptSwaggerUI'),
...getHandlers(handlerOverrides?.[methodName], defaultHandler),
];
}`;
Expand Down
12 changes: 10 additions & 2 deletions src/snapshot/v1/express/router-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export function getRouter({
>(methodName: TName, defaultHandler: RequestHandler): RequestHandler[] {
return [
...getMiddleware(middleware, methodName),
...getMiddleware(middleware, '_exceptSwaggerUI'),
...getHandlers(handlerOverrides?.[methodName], defaultHandler),
];
}
Expand Down Expand Up @@ -195,7 +194,16 @@ function getMiddleware<
if (!middleware) return [];
const middlewareArray = Array.isArray(middleware) ? middleware : [middleware];

return middlewareArray.flatMap((m) => (m[name] ? m[name] : []));
return middlewareArray.flatMap((m) => {
const _exceptSwaggerUI =
m['_exceptSwaggerUI'] && name !== '_onlySwaggerUI'
? m['_exceptSwaggerUI']
: [];

const base = m[name] ? m[name] : [];
const baseArray = Array.isArray(base) ? base : [base];
return [...baseArray, ..._exceptSwaggerUI];
});
}

/**
Expand Down

0 comments on commit 665c064

Please sign in to comment.