Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/backend/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ const auth = require('@screeps/backend/lib/game/api/auth')
const authlib = require('@screeps/backend/lib/authlib')
const _ = require('lodash')

/**
* Like legacy `q-json-response`, envelopes success as `{ ok: 1, ...data }`.
* Unlike it, does NOT call next() after responding: our routes shadow vanilla
* backend routes at the same paths, and falling through would execute the
* vanilla handler on an already-answered request (double side effects, and
* ERR_HTTP_HEADERS_SENT crashes in its tokenAuth).
*/
function jsonResponse (fn) {
return function (request, response) {
Promise.resolve()
.then(() => fn(request, response))
.then((data) => {
response.json(_.extend({ ok: 1 }, data))
})
.catch((error) => {
if (error.stack) console.error(error.stack)
if (!response.headersSent) {
response.json({ error: String(error) })
}
})
}
}

module.exports = function (config) {
const { common } = config
const { db, env, pubsub } = common.storage
Expand Down
Loading