diff options
Diffstat (limited to 'bskylink/src/routes')
-rw-r--r-- | bskylink/src/routes/redirect.ts | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/bskylink/src/routes/redirect.ts b/bskylink/src/routes/redirect.ts index 7791ea815..276aae1ca 100644 --- a/bskylink/src/routes/redirect.ts +++ b/bskylink/src/routes/redirect.ts @@ -11,6 +11,7 @@ export default function (ctx: AppContext, app: Express) { '/:linkId', handler(async (req, res) => { const linkId = req.params.linkId + const contentType = req.accepts(['html', 'json']) assert( typeof linkId === 'string', 'express guarantees id parameter is a string', @@ -21,9 +22,19 @@ export default function (ctx: AppContext, app: Express) { .where('id', '=', linkId) .executeTakeFirst() if (!found) { - // potentially broken or mistyped link— send user to the app - res.setHeader('Location', `https://${ctx.cfg.service.appHostname}`) + // potentially broken or mistyped link res.setHeader('Cache-Control', 'no-store') + if (contentType === 'json') { + return res + .status(404) + .json({ + error: 'NotFound', + message: 'Link not found', + }) + .end() + } + // send the user to the app + res.setHeader('Location', `https://${ctx.cfg.service.appHostname}`) return res.status(302).end() } // build url from original url in order to preserve query params @@ -32,8 +43,11 @@ export default function (ctx: AppContext, app: Express) { `https://${ctx.cfg.service.appHostname}`, ) url.pathname = found.path - res.setHeader('Location', url.href) res.setHeader('Cache-Control', `max-age=${(7 * DAY) / SECOND}`) + if (contentType === 'json') { + return res.json({url: url.href}).end() + } + res.setHeader('Location', url.href) return res.status(301).end() }), ) |