README
Restrict middleware
Usage
Application level
app.use(
restrict({
allowedMethods: "POST,PUT,PATCH"
})
);
Router level
router.use(
restrict({
allowedMethods: "POST,PUT,PATCH",
defaultFields: ["name"],
throwError: true,
})
);
Endpoint level
router.post(
restrict({
allowedMethods: "POST",
defaultFields: ["name"],
overrideDefaultFields: true,
throwError: true,
onError: (err, req, res, next) => {
res.json({ name: 'My Error', err })
}
}, "birthday", "sex"),
(req, res, next) => {
...
}
);
Configuration
allowedMethods
- string
: Method names divided by ,
eg. POST,PUT,PATCH
. It doesn't check the input, but non-existent HTTP methods won't trigger this middleware.
defaultFields
- string[]
(optional): This array will be merged with the defaul value unless overrideDefaultFields
is set to true. Default value is ['id', 'createdAt', 'updatedAt']
. This shouldn't be set on application level.
overrideDefaultFields
- boolean
(optional): If set to true
, it overrides the defaultFields
default value with the given array. Only works if defaultFields
was set.
throwError
- boolean
(optional): If set to true
an error will be thrown on that level. Only works if onError
wasn't set.
onError
- Express Error handler
(optional): Custom error handler. Syntax: (err, req, res, next) => { ... }
. Can be set on any level.
Changelog
No changes yet.
License
Copyright © 2020, Nagy Viktor
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.