2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
import {Type} from '@angular/core';
|
2016-05-23 19:14:23 -04:00
|
|
|
|
|
|
|
export type RouterConfig = Route[];
|
2016-06-27 17:00:07 -04:00
|
|
|
export type Data = {
|
|
|
|
[name: string]: any
|
|
|
|
};
|
|
|
|
export type ResolveData = {
|
|
|
|
[name: string]: any
|
|
|
|
};
|
2016-05-23 19:14:23 -04:00
|
|
|
|
|
|
|
export interface Route {
|
|
|
|
path?: string;
|
2016-06-27 23:10:36 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated - use `pathMatch` instead
|
|
|
|
*/
|
2016-06-14 17:55:59 -04:00
|
|
|
terminal?: boolean;
|
2016-06-27 23:10:36 -04:00
|
|
|
pathMatch?: 'full'|'prefix';
|
2016-06-08 19:14:26 -04:00
|
|
|
component?: Type|string;
|
2016-05-23 19:14:23 -04:00
|
|
|
outlet?: string;
|
2016-06-07 12:50:35 -04:00
|
|
|
canActivate?: any[];
|
|
|
|
canDeactivate?: any[];
|
2016-06-08 19:14:26 -04:00
|
|
|
redirectTo?: string;
|
2016-05-23 19:14:23 -04:00
|
|
|
children?: Route[];
|
2016-06-27 17:00:07 -04:00
|
|
|
data?: Data;
|
|
|
|
resolve?: ResolveData;
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function validateConfig(config: RouterConfig): void {
|
|
|
|
config.forEach(validateNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateNode(route: Route): void {
|
|
|
|
if (!!route.redirectTo && !!route.children) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid configuration of route '${route.path}': redirectTo and children cannot be used together`);
|
|
|
|
}
|
|
|
|
if (!!route.redirectTo && !!route.component) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid configuration of route '${route.path}': redirectTo and component cannot be used together`);
|
|
|
|
}
|
2016-06-19 17:44:20 -04:00
|
|
|
if (route.redirectTo === undefined && !route.component && !route.children) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid configuration of route '${route.path}': component, redirectTo, children must be provided`);
|
|
|
|
}
|
2016-06-16 17:36:51 -04:00
|
|
|
if (route.path === undefined) {
|
|
|
|
throw new Error(`Invalid route configuration: routes must have path specified`);
|
|
|
|
}
|
|
|
|
if (route.path.startsWith('/')) {
|
2016-06-19 17:44:20 -04:00
|
|
|
throw new Error(
|
|
|
|
`Invalid route configuration of route '${route.path}': path cannot start with a slash`);
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
2016-06-27 23:10:36 -04:00
|
|
|
if (route.path === '' && route.redirectTo !== undefined &&
|
|
|
|
(route.terminal === undefined && route.pathMatch === undefined)) {
|
|
|
|
const exp =
|
|
|
|
`The default value of 'pathMatch' is 'prefix', but often the intent is to use 'full'.`;
|
|
|
|
throw new Error(
|
|
|
|
`Invalid route configuration of route '{path: "${route.path}", redirectTo: "${route.redirectTo}"}': please provide 'pathMatch'. ${exp}`);
|
|
|
|
}
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|