-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middleware for any http method as per documentation #140
Comments
To clarify, this mock definition won't work:
This does work but wouldn't work across all methods:
|
Thank you, the example is wrong. As you have noticed, the route variants has to have defined a Supporting wildcards, or at least an array of methods in the For the moment, as a workaround you could define a base route variant in a variable and use the spread operator to reuse it in many routes, as in: const baseMiddleware = {
url: "*",
variants: [
{
id: "enabled",
response: (req, res, next) => {
res.set('Content-Type', 'application/json; charset=utf-8');
next();
}
},
{
id: "disabled",
response: (req, res, next) => next()
}
]
} ;
module.exports = [
{
...baseMiddleware,
method: "GET",
id: "add-headers-get",
} ,
{
...baseMiddleware,
method: "POST",
id: "add-headers-post",
},
{
...baseMiddleware,
method: "DELETE",
id: "add-headers-delete",
}
]; I know that this is an ugly workaround, so I will try to release a new version supporting arrays in the |
Hi again @phil-armstrong-itv , the version v2.1.0, which adds support for multiple methods in routes is already released and the docs are also updated: https://www.mocks-server.org/docs/guides-using-middlewares#adding-common-headers-to-all-requests I hope it might help you. Thanks again for your feedback! 🙂 |
Hey awesome news thank you! |
…tier-7.x chore(deps): update dependency eslint-config-prettier to v7
chore(deps): update dependency fs-extra to v10.0.1
…-resolve-13.x chore(deps): update dependency @rollup/plugin-node-resolve to v13.0.4
chore(deps): update dependency rollup to v2.52.3
chore(deps): update dependency rollup to v2.52.2
Describe the bug
When trying the middleware example presented in the docs there is an error when starting up the server:
TypeError: this._router[METHODS[routeVariant.method]] is not a function at _routesVariants.forEach (.../node_modules/@mocks-server/core/src/mocks/Mock.js:34:49) at Array.forEach (<anonymous>) at Mock._initRouter (.../node_modules/@mocks-server/core/src/mocks/Mock.js:33:26) at new Mock (.../node_modules/@mocks-server/core/src/mocks/Mock.js:28:10) at _mocks._mocksDefinitions.map (.../node_modules/@mocks-server/core/src/mocks/Mocks.js:83:18) at Array.map (<anonymous>) at Mocks._processMocks (.../node_modules/@mocks-server/core/src/mocks/Mocks.js:81:8) at Mocks.load (.../node_modules/@mocks-server/core/src/mocks/Mocks.js:147:10) at Orchestrator._onLoadMocks (.../node_modules/@mocks-server/core/src/Orchestrator.js:54:19) at EventEmitter.emit (events.js:198:13)
To Reproduce
Use the content of the middlewares.js file in the documentation and add either of the routes to the 'base' mock setup.
When you start the server the above error will occur.
If you add
method: "GET"
to the route, it works, however it looks like a wildcard doesn't work for that field (*)Expected behavior
The server loads and the middleware route fires on all requests
** Operating system, Node.js an npm versions, or browser version (please complete the following information):**
The text was updated successfully, but these errors were encountered: