unpkg/modules/actions/addToBlacklist.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-12-17 17:38:05 +00:00
const validateNpmPackageName = require('validate-npm-package-name');
2018-09-01 13:37:48 +00:00
2018-12-17 17:38:05 +00:00
const BlacklistAPI = require('../BlacklistAPI');
2017-11-11 20:18:13 +00:00
function addToBlacklist(req, res) {
2018-02-18 02:00:56 +00:00
const packageName = req.body.packageName;
2017-11-11 20:18:13 +00:00
if (!packageName) {
2018-02-18 02:00:56 +00:00
return res
.status(403)
.send({ error: 'Missing "packageName" body parameter' });
2017-11-11 20:18:13 +00:00
}
2018-02-18 02:00:56 +00:00
const nameErrors = validateNpmPackageName(packageName).errors;
2017-11-11 20:18:13 +00:00
// Disallow invalid package names.
if (nameErrors) {
2018-12-17 17:38:05 +00:00
const reason = nameErrors.join(', ');
2017-11-11 20:18:13 +00:00
return res.status(403).send({
error: `Invalid package name "${packageName}" (${reason})`
2018-02-18 02:00:56 +00:00
});
2017-11-11 20:18:13 +00:00
}
BlacklistAPI.addPackage(packageName).then(
added => {
if (added) {
2018-02-18 02:00:56 +00:00
const userId = req.user.jti;
console.log(
`Package "${packageName}" was added to the blacklist by ${userId}`
);
2017-11-11 20:18:13 +00:00
}
2018-09-01 13:37:48 +00:00
res.send({
2017-11-11 20:18:13 +00:00
ok: true,
2018-02-18 02:00:56 +00:00
message: `Package "${packageName}" was ${
2018-12-17 17:38:05 +00:00
added ? 'added to' : 'already in'
2018-02-18 02:00:56 +00:00
} 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 add "${packageName}" to 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 = addToBlacklist;