Always send JSON responses
This commit is contained in:
parent
7d0f3898de
commit
4ab944b13c
|
|
@ -14,7 +14,8 @@
|
|||
"build:dev": "NODE_ENV=development parcel build --no-cache",
|
||||
"dev": "rm -rf .parcel-cache dist && NODE_ENV=development nodemon --exec \"yarn build:dev && yarn start\" --watch routes --watch res --watch index.js",
|
||||
"build": "parcel build",
|
||||
"start": "node ./dist/index.js"
|
||||
"start": "node ./dist/index.js",
|
||||
"lint": "eslint index.js ./routes"
|
||||
},
|
||||
"dependencies": {
|
||||
"@google-cloud/datastore": "^7.0.0",
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ const createEvent = async (req, res) => {
|
|||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
res.sendStatus(400)
|
||||
res.status(400).send({ error: 'An error occurred while creating the event' })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ const createPerson = async (req, res) => {
|
|||
|
||||
await req.datastore.insert(entity)
|
||||
|
||||
res.sendStatus(201)
|
||||
res.status(201).send({ success: 'Created' })
|
||||
|
||||
// Update stats
|
||||
const personCountResult = (await req.datastore.get(req.datastore.key([req.types.stats, 'personCount'])))[0] || null
|
||||
|
|
@ -51,14 +51,14 @@ const createPerson = async (req, res) => {
|
|||
})
|
||||
}
|
||||
} else {
|
||||
res.sendStatus(400)
|
||||
res.status(400).send({ error: 'Unable to create person' })
|
||||
}
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
res.status(404).send({ error: 'Event does not exist' })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
res.sendStatus(400)
|
||||
res.status(400).send({ error: 'An error occurred while creating the person' })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@ const getEvent = async (req, res) => {
|
|||
visited: dayjs().unix()
|
||||
})
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
res.status(404).send({ error: 'Event not found' })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
res.sendStatus(404)
|
||||
res.status(404).send({ error: 'Event not found' })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ const getPeople = async (req, res) => {
|
|||
res.send({ people })
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
res.sendStatus(404)
|
||||
res.status(404).send({ error: 'Person not found' })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ const login = async (req, res) => {
|
|||
if (personResult.password) {
|
||||
const passwordsMatch = person && person.password && await bcrypt.compare(person.password, personResult.password)
|
||||
if (!passwordsMatch) {
|
||||
return res.status(401).send('Incorrect password')
|
||||
return res.status(401).send({ error: 'Incorrect password' })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -24,11 +24,11 @@ const login = async (req, res) => {
|
|||
created: personResult.created,
|
||||
})
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
res.status(404).send({ error: 'Person does not exist' })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
res.sendStatus(404)
|
||||
res.status(400).send({ error: 'An error occurred' })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import dayjs from 'dayjs'
|
|||
|
||||
const taskCleanup = async (req, res) => {
|
||||
if (req.header('X-Appengine-Cron') === undefined) {
|
||||
return res.status(400).send('This task can only be run from a cron job')
|
||||
return res.status(400).send({ error: 'This task can only be run from a cron job' })
|
||||
}
|
||||
|
||||
const threeMonthsAgo = dayjs().subtract(3, 'month').unix()
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import dayjs from 'dayjs'
|
|||
|
||||
const taskRemoveOrphans = async (req, res) => {
|
||||
if (req.header('X-Appengine-Cron') === undefined) {
|
||||
return res.status(400).send('This task can only be run from a cron job')
|
||||
return res.status(400).send({ error: 'This task can only be run from a cron job' })
|
||||
}
|
||||
|
||||
const threeMonthsAgo = dayjs().subtract(3, 'month').unix()
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ const updatePerson = async (req, res) => {
|
|||
if (personResult.password) {
|
||||
const passwordsMatch = person.password && await bcrypt.compare(person.password, personResult.password)
|
||||
if (!passwordsMatch) {
|
||||
return res.status(401).send('Incorrect password')
|
||||
return res.status(401).send({ error: 'Incorrect password' })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -24,16 +24,16 @@ const updatePerson = async (req, res) => {
|
|||
availability: person.availability,
|
||||
})
|
||||
|
||||
res.sendStatus(200)
|
||||
res.status(200).send({ success: 'Updated' })
|
||||
} else {
|
||||
res.sendStatus(400)
|
||||
res.status(400).send({ error: 'Availability must be set' })
|
||||
}
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
res.status(404).send({ error: 'Person not found' })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
res.sendStatus(400)
|
||||
res.status(400).send('An error occurred')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue