2019-01-22 17:29:44 +03:00
|
|
|
function getData(...props) {
|
|
|
|
return function (req, res, next) {
|
|
|
|
if (!req.body) {
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = props.concat('origin').reduce((data, prop) => {
|
2019-04-17 14:41:27 +03:00
|
|
|
if (!data) {
|
2019-01-22 17:29:44 +03:00
|
|
|
return null;
|
|
|
|
}
|
2019-04-17 14:41:27 +03:00
|
|
|
|
|
|
|
let propObj = typeof prop === 'string' ? {
|
|
|
|
name: prop,
|
|
|
|
required: true
|
|
|
|
} : prop;
|
|
|
|
|
|
|
|
const value = req.body[propObj.name];
|
|
|
|
if (propObj.required && !value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:29:44 +03:00
|
|
|
return Object.assign(data, {
|
2019-04-17 14:41:27 +03:00
|
|
|
[propObj.name]: value
|
2019-01-22 17:29:44 +03:00
|
|
|
});
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end(`Expected {${props.join(', ')}}`);
|
|
|
|
}
|
|
|
|
req.data = data || {};
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(status, res) {
|
|
|
|
return function () {
|
|
|
|
res.writeHead(status);
|
|
|
|
res.end();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getData,
|
|
|
|
handleError
|
|
|
|
};
|