Skip to content

Commit

Permalink
feat: added support for ctx.request.locale
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Jul 2, 2022
1 parent e017738 commit 890111d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
27 changes: 17 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 890111d

Please sign in to comment.