Ghost/ghost/members-api/util.js
Fabien O'Carroll ac9daac9f2 Fixed subscription issue with null coupons
no-issue

Coupons were being sent as null to the api, so we support non required fields
2019-05-07 17:35:17 +02:00

48 lines
1.0 KiB
JavaScript

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) => {
if (!data) {
return null;
}
let propObj = typeof prop === 'string' ? {
name: prop,
required: true
} : prop;
const value = req.body[propObj.name];
if (propObj.required && !value) {
return null;
}
return Object.assign(data, {
[propObj.name]: value
});
}, {});
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
};