Skip to content

Commit

Permalink
Merge pull request #12702 from Kagu-chan/12700-update-routetree-type
Browse files Browse the repository at this point in the history
feat(core): allow using modules and nested route trees in parallel
  • Loading branch information
kamilmysliwiec committed Nov 17, 2023
2 parents 75ccccb + 5f717ef commit e00fb64
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/router/interfaces/routes.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Type } from '@nestjs/common';
export interface RouteTree {
path: string;
module?: Type<any>;
children?: Routes | Type<any>[];
children?: (RouteTree | Type<any>)[];
}

export type Routes = RouteTree[];
6 changes: 3 additions & 3 deletions packages/core/router/router-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class RouterModule {
}

private deepCloneRoutes(
routes: Routes | Type<any>[],
): Routes | Array<Type<any>> {
routes: (RouteTree | Type<any>)[],
): (RouteTree | Type<any>)[] {
return routes.map((routeOrType: Type<any> | RouteTree) => {
if (typeof routeOrType === 'function') {
return routeOrType;
Expand All @@ -52,7 +52,7 @@ export class RouterModule {
};
}
return { ...routeOrType };
}) as Routes | Array<Type<any>>;
});
}

private initialize() {
Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/router/utils/flat-routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('flattenRoutePaths', () => {
@Module({})
class ChildModule2 {}
@Module({})
class ChildModule3 {}
@Module({})
class ChildModule4 {}
@Module({})
class ParentChildModule {}
@Module({})
class ChildChildModule2 {}
Expand Down Expand Up @@ -56,6 +60,16 @@ describe('flattenRoutePaths', () => {
},
],
},
{
path: '/child2',
children: [
{
path: 'child',
module: ChildModule3,
},
ChildModule4,
],
},
],
},
{ path: '/v1', children: [AuthModule, CatsModule, DogsModule] },
Expand All @@ -75,6 +89,8 @@ describe('flattenRoutePaths', () => {
path: '/parent/child/parentchild/childchild/child2child',
module: ChildChildModule2,
},
{ path: '/parent/child2', module: ChildModule4 },
{ path: '/parent/child2/child', module: ChildModule3 },
{ path: '/v1', module: AuthModule },
{ path: '/v1', module: CatsModule },
{ path: '/v1', module: DogsModule },
Expand Down

0 comments on commit e00fb64

Please sign in to comment.