|
| 1 | +import { autoinject } from 'aurelia-framework'; |
| 2 | +import { Router, RouteConfig } from 'aurelia-router'; |
| 3 | +import { RouteRecognizer, RouteHandler } from 'aurelia-route-recognizer'; |
| 4 | +import * as _ from 'lodash'; |
| 5 | + |
| 6 | +import * as appRoutes from './../app.routes'; |
| 7 | +import * as childRouterRoutes from './../modules/child-router/child-router.routes'; |
| 8 | + |
| 9 | +export type TAllowedRouteNames = appRoutes.RouteNames | childRouterRoutes.RouteNames; |
| 10 | + |
| 11 | +export type TRouteConfigItem = { routeName: TAllowedRouteNames, params?: { [key: string]: any } }; |
| 12 | +export type TRouteConfig = [TRouteConfigItem]; |
| 13 | +export type ITreeConfig = RouteConfig & { subroutes?: ITreeConfig[] }; |
| 14 | + |
| 15 | +@autoinject() |
| 16 | +export class RouteGeneratorService { |
| 17 | + |
| 18 | + private routeTree: ITreeConfig[] = [ |
| 19 | + appRoutes.welcome, |
| 20 | + appRoutes.users, |
| 21 | + { |
| 22 | + ...appRoutes.childRouter, |
| 23 | + subroutes: [ |
| 24 | + childRouterRoutes.welcome, |
| 25 | + childRouterRoutes.users, |
| 26 | + childRouterRoutes.childRouter, |
| 27 | + ] |
| 28 | + } |
| 29 | + ]; |
| 30 | + |
| 31 | + constructor( |
| 32 | + private router: Router, |
| 33 | + ) { } |
| 34 | + |
| 35 | + public getUrlByRouteNames(routes: TRouteConfig): string { |
| 36 | + return this.buildUrl(_.cloneDeep(routes), this.routeTree); |
| 37 | + } |
| 38 | + |
| 39 | + public navigateByUrl(url: string, options?: any): void { |
| 40 | + this.router.navigate(url, options); |
| 41 | + } |
| 42 | + |
| 43 | + public navigateByRouteNames(routes: TRouteConfig, options?: any): void { |
| 44 | + const url = this.buildUrl(_.cloneDeep(routes), this.routeTree); |
| 45 | + this.navigateByUrl(url, options); |
| 46 | + } |
| 47 | + |
| 48 | + public getRoutesConfigByParentRouteName(routeName: TAllowedRouteNames): RouteConfig[] { |
| 49 | + const parentRoute = this.findNameInRouteTree(routeName, this.routeTree); |
| 50 | + |
| 51 | + if (!parentRoute) { |
| 52 | + throw new Error( |
| 53 | + `You route name ${routeName} couldn't be found!` |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + if (parentRoute && !parentRoute.subroutes) { |
| 58 | + throw new Error( |
| 59 | + `You route name ${routeName} has no subroutes!` |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + return this.removeSubroutes(parentRoute.subroutes as RouteConfig[]); |
| 64 | + } |
| 65 | + |
| 66 | + public getRootRoutesConfig(): RouteConfig[] { |
| 67 | + return this.removeSubroutes(this.routeTree); |
| 68 | + } |
| 69 | + |
| 70 | + public getRouteConfigByRouteName(routeName: TAllowedRouteNames): RouteConfig | undefined { |
| 71 | + return this.findNameInRouteTree(routeName, this.routeTree); |
| 72 | + } |
| 73 | + |
| 74 | + private removeSubroutes(routes: RouteConfig[]): RouteConfig[] { |
| 75 | + return routes.map(route => { |
| 76 | + const newRoute = _.cloneDeep(route); |
| 77 | + delete newRoute.subroutes; |
| 78 | + return newRoute; |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private findNameInRouteTree(routeName: TAllowedRouteNames, treeConfig: ITreeConfig | ITreeConfig[]): ITreeConfig | undefined { |
| 83 | + let result; |
| 84 | + if (treeConfig instanceof Array) { |
| 85 | + for (let conf of treeConfig) { |
| 86 | + result = this.findNameInRouteTree(routeName, conf); |
| 87 | + if (result) { |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + } else { |
| 92 | + if (treeConfig.name === routeName) { |
| 93 | + return treeConfig; |
| 94 | + } |
| 95 | + if (treeConfig.subroutes) { |
| 96 | + result = this.findNameInRouteTree(routeName, treeConfig.subroutes); |
| 97 | + } |
| 98 | + } |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + private buildUrl(routes: TRouteConfig, treeConfig: ITreeConfig[]): string { |
| 103 | + |
| 104 | + if (routes.length as any === 0) { |
| 105 | + return ''; |
| 106 | + } |
| 107 | + |
| 108 | + const currentRouteConfig = routes.shift() as TRouteConfigItem; |
| 109 | + const currentTreeConfig = treeConfig.find(config => config.name === currentRouteConfig.routeName); |
| 110 | + |
| 111 | + if (!currentTreeConfig) { |
| 112 | + throw new Error(`The route config with name ${currentRouteConfig.routeName} doesn't exist!`); |
| 113 | + } |
| 114 | + |
| 115 | + const url = this.getUrlByTreeConfig(currentTreeConfig, currentRouteConfig); |
| 116 | + |
| 117 | + if (url.includes('?') && routes.length >= 1) { |
| 118 | + throw new Error( |
| 119 | + `You provided a parameter not used in ${currentRouteConfig.routeName}. Add query parameters to the last route configuration!` |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + return url + this.buildUrl(routes, currentTreeConfig.subroutes as ITreeConfig[]); |
| 124 | + } |
| 125 | + |
| 126 | + private getUrlByTreeConfig(treeConfig: ITreeConfig, routeConfig: TRouteConfigItem): string { |
| 127 | + |
| 128 | + const routeRecognizer = new RouteRecognizer(); |
| 129 | + if (Array.isArray(treeConfig.route)) { |
| 130 | + treeConfig.route.forEach(value => routeRecognizer.add([{ |
| 131 | + path: value, |
| 132 | + handler: treeConfig as RouteHandler, |
| 133 | + caseSensitive: true |
| 134 | + }])); |
| 135 | + } else { |
| 136 | + routeRecognizer.add([{ |
| 137 | + path: treeConfig.route, |
| 138 | + handler: treeConfig as RouteHandler, |
| 139 | + caseSensitive: true |
| 140 | + }]); |
| 141 | + } |
| 142 | + |
| 143 | + return routeRecognizer.generate(routeConfig.routeName, routeConfig.params as any); |
| 144 | + } |
| 145 | +} |
0 commit comments