nss-json-server
Version:
JSON Server with other useful mixins
55 lines (54 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const bodyParser = require("body-parser");
/**
* Use same body-parser options as json-server
*/
exports.bodyParsingHandler = [
bodyParser.json({ limit: '50mb' }),
bodyParser.urlencoded({ extended: false }),
];
/**
* Json error handler
*/
exports.errorHandler = (err, req, res, next) => {
console.error(err);
res.status(500).jsonp(err.message);
};
/**
* Just executes the next middleware,
* to pass directly the request to the json-server router
*/
exports.goNext = (req, res, next) => {
next();
};
/**
* Look for a property in the request body and reject the request if found
*/
function forbidUpdateOn(...forbiddenBodyParams) {
return (req, res, next) => {
const bodyParams = Object.keys(req.body);
const hasForbiddenParam = bodyParams.some(forbiddenBodyParams.includes);
if (hasForbiddenParam) {
res.status(403).jsonp(`Forbidden update on: ${forbiddenBodyParams.join(', ')}`);
}
else {
next();
}
};
}
exports.forbidUpdateOn = forbidUpdateOn;
/**
* Reject the request for a given method
*/
function forbidMethod(method) {
return (req, res, next) => {
if (req.method === method) {
res.sendStatus(405);
}
else {
next();
}
};
}
exports.forbidMethod = forbidMethod;