From 98022010183fbe7f2d0a4ab4bc6fbfb825aaa5d1 Mon Sep 17 00:00:00 2001 From: Jacob Bare Date: Thu, 28 Jan 2021 21:10:56 -0600 Subject: [PATCH] Pass Express app object to redirect handler --- packages/marko-web/express/error-handlers.js | 4 +++- packages/marko-web/express/get-redirect.js | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/marko-web/express/error-handlers.js b/packages/marko-web/express/error-handlers.js index 03ea071fc..0eed3b3d4 100644 --- a/packages/marko-web/express/error-handlers.js +++ b/packages/marko-web/express/error-handlers.js @@ -34,6 +34,7 @@ const render = (res, { statusCode, err, template }) => { }; const redirectOrError = ({ + app, req, res, err, @@ -42,7 +43,7 @@ const redirectOrError = ({ template, fatalErrorHandler, }) => { - getRedirect(req, redirectHandler).then((redirect) => { + getRedirect(req, redirectHandler, app).then((redirect) => { if (redirect) { const { code, to } = redirect; res.redirect(code, to); @@ -74,6 +75,7 @@ module.exports = (app, { template, redirectHandler, onFatalError }) => { app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars const statusCode = err.status || err.statusCode || 500; const opts = { + app, req, res, err, diff --git a/packages/marko-web/express/get-redirect.js b/packages/marko-web/express/get-redirect.js index 9aefcf0a7..3ce696ae4 100644 --- a/packages/marko-web/express/get-redirect.js +++ b/packages/marko-web/express/get-redirect.js @@ -13,8 +13,9 @@ const query = gql` * @param {object} req The Express request object. * @param {function} [handler] An optional redirect handler to execute if no redirect was found. * Must return an object of `{ to, code }` or `null` + * @param {object} app The Express app object. */ -module.exports = async (req, handler) => { +module.exports = async (req, handler, app) => { const { apollo, path, query: params } = req; const from = path.replace(/\/$/, ''); const variables = { input: { from, params } }; @@ -24,7 +25,12 @@ module.exports = async (req, handler) => { if (to) return websiteRedirect; // Attempt to find a redirect using the handler. if (typeof handler !== 'function') return null; - const result = await handler({ from, params, req }); + const result = await handler({ + from, + params, + req, + app, + }); if (!result || !result.to) return null; return { ...result, code: result.code || 301 }; };