Skip to content

Commit

Permalink
Merge pull request #33 from arfedulov/fix-apply-middleware-every-route
Browse files Browse the repository at this point in the history
fix applying middleware only for routes listed in urlPrefixes
  • Loading branch information
octoape authored Aug 20, 2024
2 parents 07b0762 + d8d9726 commit 9ccc096
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ export default (options?: MockOptions): Plugin => {
})
if (options.middlewares) {
for (const [, layer] of options.middlewares.entries()) {
server.middlewares.use(layer);
server.middlewares.use((req, res, next) => {
const hasMatch = options.urlPrefixes.some((prefix) => req.url.startsWith(prefix))
if (hasMatch) {
layer(req, res, next)
} else {
next()
}
});
}
}
server.middlewares.use((
Expand Down

0 comments on commit 9ccc096

Please sign in to comment.