Skip to content

Commit

Permalink
support redirect in routing configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
agubler committed Dec 1, 2020
1 parent 861eeda commit dcff5da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/routing/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
private _register(config: RouteConfig[], routes?: Route[], parentRoute?: Route): void {
routes = routes ? routes : this._routes;
for (let i = 0; i < config.length; i++) {
let { path, outlet, children, defaultRoute = false, defaultParams = {}, id, title } = config[i];
let { path, outlet, children, defaultRoute = false, defaultParams = {}, id, title, redirect } = config[i];
let [parsedPath, queryParamString] = path.split('?');
let queryParams: string[] = [];
parsedPath = this._stripLeadingSlash(parsedPath);
Expand All @@ -200,6 +200,7 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
title,
path: parsedPath,
segments,
redirect,
defaultParams: parentRoute ? { ...parentRoute.defaultParams, ...defaultParams } : defaultParams,
children: [],
fullPath: parentRoute ? `${parentRoute.fullPath}/${parsedPath}` : parsedPath,
Expand Down Expand Up @@ -348,6 +349,15 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
}

if (matchedRoute) {
if (matchedRoute.route.redirect && matchedRoute.type === 'index') {
let { redirect } = matchedRoute.route;
const params = { ...matchedRoute.params };
Object.keys(params).forEach((paramKey) => {
redirect = redirect.replace(`{${paramKey}}`, params[paramKey]);
});
this.setPath(redirect);
return;
}
if (matchedRoute.type === 'partial') {
matchedRoute.type = 'error';
}
Expand Down
2 changes: 2 additions & 0 deletions src/routing/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Route {
params: string[];
segments: string[];
children: Route[];
redirect?: string;
fullPath: string;
fullParams: string[];
fullQueryParams: string[];
Expand All @@ -34,6 +35,7 @@ export interface RouteConfig {
path: string;
outlet: string;
children?: RouteConfig[];
redirect?: string;
defaultParams?: Params;
defaultRoute?: boolean;
title?: string;
Expand Down
30 changes: 30 additions & 0 deletions tests/routing/unit/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,34 @@ describe('Router', () => {
assert.strictEqual(contextMap!.size, 2);
});
});

describe('redirect', () => {
it('should redirect to path on matched route', () => {
const router = new Router(
[
{
path: 'foo/{param}',
id: 'foo',
outlet: 'main',
redirect: 'foo/{param}/bar',
defaultParams: {
param: 'default-param'
},
defaultRoute: true,
children: [
{
path: 'bar',
id: 'bar',
outlet: 'bar'
}
]
}
],
{ HistoryManager }
);
const contextMap = router.getOutlet('bar');
assert.isOk(contextMap);
assert.strictEqual(contextMap!.size, 1);
});
});
});

0 comments on commit dcff5da

Please sign in to comment.