From 890111d804a8e57d36340b05de4f33710b53fc64 Mon Sep 17 00:00:00 2001 From: titanism <101466223+titanism@users.noreply.github.com> Date: Sat, 2 Jul 2022 01:03:07 -0500 Subject: [PATCH] feat: added support for ctx.request.locale --- README.md | 1 + index.js | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3ef91db..9fc75e3 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ app.use(redirectLoop.middleware); ## Options +* `getDefaultPath` (Function) - function which accepts `ctx` argument and returns a path to fallback to, defaults to either `/${ctx.request.locale}` (e.g. if using `@ladjs/i18n`) or `/` * `defaultPath` (String) - path to fallback to, defaults to `'/'` * `maxRedirects` (Number) - maximum number of redirects to allow, defaults to `5` * `console` (Object) - a logger instance, defaults to `console` diff --git a/index.js b/index.js index e437d21..3978cb3 100644 --- a/index.js +++ b/index.js @@ -4,19 +4,22 @@ const isSANB = require('is-string-and-not-blank'); class RedirectLoop { constructor(config) { this.config = { - defaultPath: '/', + getDefaultPath: (ctx) => + ctx.request.locale ? `/${ctx.request.locale}` : '/', maxRedirects: 5, logger: console, ...config }; - if (!isSANB(this.config.defaultPath)) - throw new Error('defaultPath must be a String'); + if (isSANB(this.config.defaultPath)) + this.config.getDefaultPath = () => this.config.defaultPath; + if ( typeof this.config.maxRedirects !== 'number' || this.config.maxRedirects <= 0 ) throw new Error('maxRedirects must be a Number greater than zero'); + this.middleware = this.middleware.bind(this); } @@ -45,6 +48,8 @@ class RedirectLoop { ctx.redirect = function (url, alt) { let address = url; + const defaultPath = config.getDefaultPath(ctx); + if (url === 'back') { // // NOTE: we can only use the Referrer if they're from the same site @@ -53,13 +58,12 @@ class RedirectLoop { ctx.get('Referrer') && new Url(ctx.get('Referrer'), {}).origin === new Url(ctx.href, {}).origin - ? new Url(ctx.get('Referrer'), {}).pathname || '/' - : alt || '/'; + ? new Url(ctx.get('Referrer'), {}).pathname || defaultPath + : alt || defaultPath; } - const previousPreviousPath = - ctx.session.prevPrevPath || config.defaultPath; - const previousPath = ctx.session.prevPath || config.defaultPath; + const previousPreviousPath = ctx.session.prevPrevPath || defaultPath; + const previousPath = ctx.session.prevPath || defaultPath; const previousMethod = ctx.session.prevMethod || ctx.method; const maxRedirects = ctx.session.maxRedirects || 1; @@ -78,10 +82,13 @@ class RedirectLoop { // if the prevPrevPath w/o querystring is !== prevPrevPath // then redirect then to prevPrevPath w/o querystring const { pathname } = new Url(previousPreviousPath, {}); - address = pathname === previousPreviousPath ? '/' : pathname || '/'; + address = + pathname === previousPreviousPath + ? defaultPath + : pathname || defaultPath; } } else if (maxRedirects > config.maxRedirects) { - address = config.defaultPath; + address = defaultPath; } redirect.call(this, address, alt);