Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 9ccc096

Browse files
authored
Merge pull request #33 from arfedulov/fix-apply-middleware-every-route
fix applying middleware only for routes listed in urlPrefixes
2 parents 07b0762 + d8d9726 commit 9ccc096

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ export default (options?: MockOptions): Plugin => {
7777
})
7878
if (options.middlewares) {
7979
for (const [, layer] of options.middlewares.entries()) {
80-
server.middlewares.use(layer);
80+
server.middlewares.use((req, res, next) => {
81+
const hasMatch = options.urlPrefixes.some((prefix) => req.url.startsWith(prefix))
82+
if (hasMatch) {
83+
layer(req, res, next)
84+
} else {
85+
next()
86+
}
87+
});
8188
}
8289
}
8390
server.middlewares.use((

0 commit comments

Comments
 (0)