Files
archived-next-auth/src/server/lib/extend-req.js
Balázs Orbán f6d6c4344c refactor: code base improvements 3 (#1072)
* refactor: extend res.{end,send,json}, redirect

* refactor: chain res methods, remove unnecessary ones

* refactor: simplify oauth callback signature

* refactor: code simplifications

* refactor: re-export everything from routes in one

* refactor: split up main index.js to multiple files

* refactor: simplify passing of provider(s) around

* refactor: extend req with callbackUrl inline

* refactor: simplify page rendering

* refactor: move error page redirects to main file, simplify renderer

* refactor: inline req.options definition

* refactor: simplify error fallbacks

* refactor: remove else branches and unnecessary try..catch

* refactor: add docs, and simplify jwt functions

* refactor: prefer errors object over switch..case in signin page

* feat: log all params sent to logger instead of only first

* refactor: fewer lines input validation

* refactor: remove even more unnecessary else branches
2021-02-01 10:01:10 +01:00

36 lines
875 B
JavaScript

/**
* Extends res.{end,json,send} with `done()`,
* and redirect to support sending url as json.
*
* When a response is complete, it will call the `done` method,
* so that the serverless function knows when it is
* safe to return and that no more data will be sent.
*/
export default function extendRes (req, res, done) {
const originalResEnd = res.end.bind(res)
res.end = (...args) => {
done()
return originalResEnd(...args)
}
const originalResJson = res.json.bind(res)
res.json = (...args) => {
done()
return originalResJson(...args)
}
const originalResSend = res.send.bind(res)
res.send = (...args) => {
done()
return originalResSend(...args)
}
res.redirect = (url) => {
if (req.body?.json === 'true') {
return res.json({ url })
}
res.status(302).setHeader('Location', url)
return res.end()
}
}