diff --git a/npm/ng-packs/packages/core/src/lib/core.module.ts b/npm/ng-packs/packages/core/src/lib/core.module.ts index 72aeacb29d..5023876a4d 100644 --- a/npm/ng-packs/packages/core/src/lib/core.module.ts +++ b/npm/ng-packs/packages/core/src/lib/core.module.ts @@ -21,6 +21,7 @@ import { PermissionDirective } from './directives/permission.directive'; import { ReplaceableTemplateDirective } from './directives/replaceable-template.directive'; import { StopPropagationDirective } from './directives/stop-propagation.directive'; import { VisibilityDirective } from './directives/visibility.directive'; +import { RoutesHandler } from './handlers/routes.handler'; import { ApiInterceptor } from './interceptors/api.interceptor'; import { LocalizationModule } from './localization.module'; import { ABP } from './models/common'; @@ -196,6 +197,12 @@ export class CoreModule { deps: [LocalizationService], useFactory: noop, }, + { + provide: APP_INITIALIZER, + multi: true, + deps: [RoutesHandler], + useFactory: noop, + }, { provide: OAuthStorage, useFactory: storageFactory }, ], }; diff --git a/npm/ng-packs/packages/core/src/lib/handlers/index.ts b/npm/ng-packs/packages/core/src/lib/handlers/index.ts index e69de29bb2..b666218d93 100644 --- a/npm/ng-packs/packages/core/src/lib/handlers/index.ts +++ b/npm/ng-packs/packages/core/src/lib/handlers/index.ts @@ -0,0 +1 @@ +export * from './routes.handler'; diff --git a/npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts b/npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts new file mode 100644 index 0000000000..a5446c7101 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts @@ -0,0 +1,25 @@ +import { Injectable, Optional } from '@angular/core'; +import { Router } from '@angular/router'; +import { RoutesService } from '../services/routes.service'; + +@Injectable({ + providedIn: 'root', +}) +export class RoutesHandler { + constructor(private routes: RoutesService, @Optional() private router: Router) { + this.addRoutes(); + } + + addRoutes() { + this.router?.config.forEach(({ path, data }) => { + if (!data) return; + + if (data.route) { + this.routes.add([{ path: '/' + path, ...data.route }]); + return; + } + + if (data.routes) this.routes.add(data.routes); + }); + } +} diff --git a/npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts new file mode 100644 index 0000000000..e1e32d9221 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts @@ -0,0 +1,63 @@ +import { Router } from '@angular/router'; +import { RoutesHandler } from '../handlers'; +import { RoutesService } from '../services'; + +describe('Routes Handler', () => { + describe('#add', () => { + it('should add routes from router config', () => { + const config = [ + { path: 'x' }, + { path: '', data: { route: { name: 'Foo' } } }, + { path: 'bar', data: { route: { name: 'Bar' } } }, + { data: { routes: [{ path: '/baz', name: 'Baz' }] } }, + ]; + const foo = [{ path: '/', name: 'Foo' }]; + const bar = [{ path: '/bar', name: 'Bar' }]; + const baz = [{ path: '/baz', name: 'Baz' }]; + + const routes = []; + const add = jest.fn(routes.push.bind(routes)); + const mockRoutesService = ({ add } as unknown) as RoutesService; + const mockRouter = ({ config } as unknown) as Router; + + const handler = new RoutesHandler(mockRoutesService, mockRouter); + + expect(add).toHaveBeenCalledTimes(3); + expect(routes).toEqual([foo, bar, baz]); + }); + }); + describe('#add', () => { + it('should add routes from router config', () => { + const config = [ + { path: 'x' }, + { path: 'y', data: {} }, + { path: '', data: { route: { name: 'Foo' } } }, + { path: 'bar', data: { route: { name: 'Bar' } } }, + { data: { routes: [{ path: '/baz', name: 'Baz' }] } }, + ]; + const foo = [{ path: '/', name: 'Foo' }]; + const bar = [{ path: '/bar', name: 'Bar' }]; + const baz = [{ path: '/baz', name: 'Baz' }]; + + const routes = []; + const add = jest.fn(routes.push.bind(routes)); + const mockRoutesService = ({ add } as unknown) as RoutesService; + const mockRouter = ({ config } as unknown) as Router; + + const handler = new RoutesHandler(mockRoutesService, mockRouter); + + expect(add).toHaveBeenCalledTimes(3); + expect(routes).toEqual([foo, bar, baz]); + }); + + it('should not add routes when there is no router', () => { + const routes = []; + const add = jest.fn(routes.push.bind(routes)); + const mockRoutesService = ({ add } as unknown) as RoutesService; + + const handler = new RoutesHandler(mockRoutesService, null); + + expect(add).not.toHaveBeenCalled(); + }); + }); +});