Prettify everything

This commit is contained in:
MICHAEL JACKSON
2018-02-17 18:00:56 -08:00
parent d6f2bc089a
commit 2e1f09e913
58 changed files with 1061 additions and 932 deletions

View File

@ -1,42 +1,48 @@
const validateNpmPackageName = require("validate-npm-package-name")
const BlacklistAPI = require("../BlacklistAPI")
const validateNpmPackageName = require("validate-npm-package-name");
const BlacklistAPI = require("../BlacklistAPI");
function addToBlacklist(req, res) {
const packageName = req.body.packageName
const packageName = req.body.packageName;
if (!packageName) {
return res.status(403).send({ error: 'Missing "packageName" body parameter' })
return res
.status(403)
.send({ error: 'Missing "packageName" body parameter' });
}
const nameErrors = validateNpmPackageName(packageName).errors
const nameErrors = validateNpmPackageName(packageName).errors;
// Disallow invalid package names.
if (nameErrors) {
const reason = nameErrors.join(", ")
const reason = nameErrors.join(", ");
return res.status(403).send({
error: `Invalid package name "${packageName}" (${reason})`
})
});
}
BlacklistAPI.addPackage(packageName).then(
added => {
if (added) {
const userId = req.user.jti
console.log(`Package "${packageName}" was added to the blacklist by ${userId}`)
const userId = req.user.jti;
console.log(
`Package "${packageName}" was added to the blacklist by ${userId}`
);
}
res.set({ "Content-Location": `/_blacklist/${packageName}` }).send({
ok: true,
message: `Package "${packageName}" was ${added ? "added to" : "already in"} the blacklist`
})
message: `Package "${packageName}" was ${
added ? "added to" : "already in"
} the blacklist`
});
},
error => {
console.error(error)
console.error(error);
res.status(500).send({
error: `Unable to add "${packageName}" to the blacklist`
})
});
}
)
);
}
module.exports = addToBlacklist
module.exports = addToBlacklist;

View File

@ -1,24 +1,24 @@
const AuthAPI = require("../AuthAPI")
const AuthAPI = require("../AuthAPI");
const defaultScopes = {
blacklist: {
read: true
}
}
};
function createAuth(req, res) {
AuthAPI.createToken(defaultScopes).then(
token => {
res.send({ token })
res.send({ token });
},
error => {
console.error(error)
console.error(error);
res.status(500).send({
error: "Unable to generate auth token"
})
});
}
)
);
}
module.exports = createAuth
module.exports = createAuth;

View File

@ -1,28 +1,32 @@
const BlacklistAPI = require("../BlacklistAPI")
const BlacklistAPI = require("../BlacklistAPI");
function removeFromBlacklist(req, res) {
const packageName = req.packageName
const packageName = req.packageName;
BlacklistAPI.removePackage(packageName).then(
removed => {
if (removed) {
const userId = req.user.jti
console.log(`Package "${packageName}" was removed from the blacklist by ${userId}`)
const userId = req.user.jti;
console.log(
`Package "${packageName}" was removed from the blacklist by ${userId}`
);
}
res.send({
ok: true,
message: `Package "${packageName}" was ${removed ? "removed from" : "not in"} the blacklist`
})
message: `Package "${packageName}" was ${
removed ? "removed from" : "not in"
} the blacklist`
});
},
error => {
console.error(error)
console.error(error);
res.status(500).send({
error: `Unable to remove "${packageName}" from the blacklist`
})
});
}
)
);
}
module.exports = removeFromBlacklist
module.exports = removeFromBlacklist;

View File

@ -1,5 +1,5 @@
function showAuth(req, res) {
res.send({ auth: req.user })
res.send({ auth: req.user });
}
module.exports = showAuth
module.exports = showAuth;

View File

@ -1,17 +1,17 @@
const BlacklistAPI = require("../BlacklistAPI")
const BlacklistAPI = require("../BlacklistAPI");
function showBlacklist(req, res) {
BlacklistAPI.getPackages().then(
blacklist => {
res.send({ blacklist })
res.send({ blacklist });
},
error => {
console.error(error)
console.error(error);
res.status(500).send({
error: "Unable to fetch blacklist"
})
});
}
)
);
}
module.exports = showBlacklist
module.exports = showBlacklist;

View File

@ -1,7 +1,7 @@
const AuthAPI = require("../AuthAPI")
const AuthAPI = require("../AuthAPI");
function showPublicKey(req, res) {
res.send({ publicKey: AuthAPI.getPublicKey() })
res.send({ publicKey: AuthAPI.getPublicKey() });
}
module.exports = showPublicKey
module.exports = showPublicKey;

View File

@ -1,42 +1,46 @@
const subDays = require("date-fns/sub_days")
const startOfDay = require("date-fns/start_of_day")
const startOfSecond = require("date-fns/start_of_second")
const StatsAPI = require("../StatsAPI")
const subDays = require("date-fns/sub_days");
const startOfDay = require("date-fns/start_of_day");
const startOfSecond = require("date-fns/start_of_second");
const StatsAPI = require("../StatsAPI");
function showStats(req, res) {
let since, until
let since, until;
switch (req.query.period) {
case "last-day":
until = startOfDay(new Date())
since = subDays(until, 1)
break
until = startOfDay(new Date());
since = subDays(until, 1);
break;
case "last-week":
until = startOfDay(new Date())
since = subDays(until, 7)
break
until = startOfDay(new Date());
since = subDays(until, 7);
break;
case "last-month":
until = startOfDay(new Date())
since = subDays(until, 30)
break
until = startOfDay(new Date());
since = subDays(until, 30);
break;
default:
until = req.query.until ? new Date(req.query.until) : startOfSecond(new Date())
since = new Date(req.query.since)
until = req.query.until
? new Date(req.query.until)
: startOfSecond(new Date());
since = new Date(req.query.since);
}
if (isNaN(since.getTime())) {
return res.status(403).send({ error: "?since is not a valid date" })
return res.status(403).send({ error: "?since is not a valid date" });
}
if (isNaN(until.getTime())) {
return res.status(403).send({ error: "?until is not a valid date" })
return res.status(403).send({ error: "?until is not a valid date" });
}
if (until <= since) {
return res.status(403).send({ error: "?until date must come after ?since date" })
return res
.status(403)
.send({ error: "?until date must come after ?since date" });
}
if (until >= new Date()) {
return res.status(403).send({ error: "?until must be a date in the past" })
return res.status(403).send({ error: "?until must be a date in the past" });
}
StatsAPI.getStats(since, until).then(
@ -46,13 +50,13 @@ function showStats(req, res) {
"Cache-Control": "public, max-age=60",
"Cache-Tag": "stats"
})
.send(stats)
.send(stats);
},
error => {
console.error(error)
res.status(500).send({ error: "Unable to fetch stats" })
console.error(error);
res.status(500).send({ error: "Unable to fetch stats" });
}
)
);
}
module.exports = showStats
module.exports = showStats;