unpkg/modules/actions/removeFromBlacklist.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-09-01 13:37:48 +00:00
const validateNpmPackageName = require("validate-npm-package-name");
2018-02-18 02:00:56 +00:00
const BlacklistAPI = require("../BlacklistAPI");
2017-11-11 20:18:13 +00:00
function removeFromBlacklist(req, res) {
2018-09-01 13:37:48 +00:00
// TODO: Remove req.packageName when DELETE
// /_blacklist/:packageName API is removed
const packageName = req.body.packageName || req.packageName;
if (!packageName) {
return res
.status(403)
.send({ error: 'Missing "packageName" body parameter' });
}
const nameErrors = validateNpmPackageName(packageName).errors;
// Disallow invalid package names.
if (nameErrors) {
const reason = nameErrors.join(", ");
return res.status(403).send({
error: `Invalid package name "${packageName}" (${reason})`
});
}
2017-11-11 20:18:13 +00:00
BlacklistAPI.removePackage(packageName).then(
removed => {
if (removed) {
2018-02-18 02:00:56 +00:00
const userId = req.user.jti;
console.log(
`Package "${packageName}" was removed from the blacklist by ${userId}`
);
2017-11-11 20:18:13 +00:00
}
res.send({
ok: true,
2018-02-18 02:00:56 +00:00
message: `Package "${packageName}" was ${
removed ? "removed from" : "not in"
} the blacklist`
});
2017-11-11 20:18:13 +00:00
},
error => {
2018-02-18 02:00:56 +00:00
console.error(error);
2017-11-11 20:18:13 +00:00
res.status(500).send({
error: `Unable to remove "${packageName}" from the blacklist`
2018-02-18 02:00:56 +00:00
});
2017-11-11 20:18:13 +00:00
}
2018-02-18 02:00:56 +00:00
);
2017-11-11 20:18:13 +00:00
}
2018-02-18 02:00:56 +00:00
module.exports = removeFromBlacklist;