Prettify
This commit is contained in:
@ -1,20 +1,18 @@
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
// 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})`
|
||||
})
|
||||
@ -24,16 +22,12 @@ function addToBlacklist(req, res) {
|
||||
added => {
|
||||
if (added) {
|
||||
const userId = req.user.jti
|
||||
console.log(
|
||||
`Package "${packageName}" was added to the blacklist by ${userId}`
|
||||
)
|
||||
console.log(`Package "${packageName}" was added to the blacklist by ${userId}`)
|
||||
}
|
||||
|
||||
res.set({ 'Content-Location': `/_blacklist/${packageName}` }).send({
|
||||
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 => {
|
||||
|
@ -1,4 +1,4 @@
|
||||
const AuthAPI = require('../AuthAPI')
|
||||
const AuthAPI = require("../AuthAPI")
|
||||
|
||||
const defaultScopes = {
|
||||
blacklist: {
|
||||
@ -15,7 +15,7 @@ function createAuth(req, res) {
|
||||
console.error(error)
|
||||
|
||||
res.status(500).send({
|
||||
error: 'Unable to generate auth token'
|
||||
error: "Unable to generate auth token"
|
||||
})
|
||||
}
|
||||
)
|
||||
|
@ -1,5 +1,5 @@
|
||||
const validateNpmPackageName = require('validate-npm-package-name')
|
||||
const BlacklistAPI = require('../BlacklistAPI')
|
||||
const validateNpmPackageName = require("validate-npm-package-name")
|
||||
const BlacklistAPI = require("../BlacklistAPI")
|
||||
|
||||
function removeFromBlacklist(req, res) {
|
||||
const packageName = req.packageName
|
||||
@ -8,16 +8,12 @@ function removeFromBlacklist(req, res) {
|
||||
removed => {
|
||||
if (removed) {
|
||||
const userId = req.user.jti
|
||||
console.log(
|
||||
`Package "${packageName}" was removed from the blacklist by ${userId}`
|
||||
)
|
||||
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 => {
|
||||
|
@ -1,4 +1,4 @@
|
||||
const BlacklistAPI = require('../BlacklistAPI')
|
||||
const BlacklistAPI = require("../BlacklistAPI")
|
||||
|
||||
function showBlacklist(req, res) {
|
||||
BlacklistAPI.getPackages().then(
|
||||
@ -8,7 +8,7 @@ function showBlacklist(req, res) {
|
||||
error => {
|
||||
console.error(error)
|
||||
res.status(500).send({
|
||||
error: 'Unable to fetch blacklist'
|
||||
error: "Unable to fetch blacklist"
|
||||
})
|
||||
}
|
||||
)
|
||||
|
@ -1,4 +1,4 @@
|
||||
const AuthAPI = require('../AuthAPI')
|
||||
const AuthAPI = require("../AuthAPI")
|
||||
|
||||
function showPublicKey(req, res) {
|
||||
res.send({ publicKey: AuthAPI.getPublicKey() })
|
||||
|
@ -1,60 +1,56 @@
|
||||
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
|
||||
switch (req.query.period) {
|
||||
case 'last-day':
|
||||
case "last-day":
|
||||
until = startOfDay(new Date())
|
||||
since = subDays(until, 1)
|
||||
break
|
||||
case 'last-week':
|
||||
case "last-week":
|
||||
until = startOfDay(new Date())
|
||||
since = subDays(until, 7)
|
||||
break
|
||||
case 'last-month':
|
||||
case "last-month":
|
||||
until = startOfDay(new Date())
|
||||
since = subDays(until, 30)
|
||||
break
|
||||
default:
|
||||
until = req.query.until
|
||||
? new Date(req.query.until)
|
||||
: startOfSecond(new Date())
|
||||
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(
|
||||
stats => {
|
||||
res
|
||||
.set({
|
||||
'Cache-Control': 'public, max-age=60',
|
||||
'Cache-Tag': 'stats'
|
||||
"Cache-Control": "public, max-age=60",
|
||||
"Cache-Tag": "stats"
|
||||
})
|
||||
.send(stats)
|
||||
},
|
||||
error => {
|
||||
console.error(error)
|
||||
res.status(500).send({ error: 'Unable to fetch stats' })
|
||||
res.status(500).send({ error: "Unable to fetch stats" })
|
||||
}
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user