-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(@nestjs/core): Add RouterModule to the core #3489
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Type } from '@nestjs/common'; | ||
|
||
/** | ||
* Defines the Routes Tree | ||
* - `path` - a string describe the Module path which will be applied | ||
* to all it's controllers and childs | ||
* - `module` - the parent Module. | ||
* - `children` - an array of child Modules. | ||
*/ | ||
export interface Route { | ||
path: string; | ||
module?: Type<any>; | ||
children?: Routes | Type<any>[]; | ||
} | ||
|
||
/** | ||
* Defines the Routes Tree | ||
* - `path` - a string describe the Module path which will be applied | ||
* to all it's controllers and childs | ||
* - `module` - the parent Module. | ||
* - `children` - an array of child Modules. | ||
* | ||
* @publicapi | ||
*/ | ||
export type Routes = Route[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Module, DynamicModule } from '@nestjs/common'; | ||
import { validatePath } from '@nestjs/common/utils/shared.utils'; | ||
import { MODULE_PATH } from '@nestjs/common/constants'; | ||
import { Routes } from './interfaces/routes.interface'; | ||
import { flatRoutes } from './utils/flat-routes.util'; | ||
|
||
/** | ||
* A Utility Module to Organize your Routes, | ||
* | ||
* @publicApi | ||
*/ | ||
@Module({}) | ||
export class RouterModule { | ||
/** | ||
* takes an array of modules and organize them in hierarchy way | ||
* @param {Routes} routes Array of Routes | ||
* @publicapi | ||
*/ | ||
static register(routes: Routes): DynamicModule { | ||
RouterModule.buildPathMap(routes); | ||
return { | ||
module: RouterModule, | ||
}; | ||
} | ||
|
||
private static buildPathMap(routes: Routes) { | ||
const flattenRoutes = flatRoutes(routes); | ||
flattenRoutes.forEach(route => { | ||
Reflect.defineMetadata( | ||
MODULE_PATH, | ||
validatePath(route.path), | ||
route.module, | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Routes } from '../interfaces/routes.interface'; | ||
import { validatePath } from '@nestjs/common/utils/shared.utils'; | ||
|
||
export function flatRoutes(routes: Routes, result = []) { | ||
routes.forEach(element => { | ||
if (element.module && element.path) { | ||
result.push(element); | ||
} | ||
if (element.children) { | ||
const childrenRef = element.children as Routes; | ||
childrenRef.forEach(child => { | ||
if (!(typeof child === 'string') && child.path) { | ||
child.path = validatePath( | ||
validatePath(element.path) + validatePath(child.path), | ||
); | ||
} else { | ||
result.push({ path: element.path, module: child }); | ||
} | ||
}); | ||
return flatRoutes(childrenRef, result); | ||
} | ||
}); | ||
result.forEach(route => { | ||
// clean up | ||
delete route.children; | ||
}); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { RouterModule } from '../../router/router.module'; | ||
import { Module, Controller } from '@nestjs/common'; | ||
import { MODULE_PATH } from '@nestjs/common/constants'; | ||
import { Test } from '@nestjs/testing'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Routes } from '../../router/interfaces/routes.interface'; | ||
import { expect } from 'chai'; | ||
|
||
describe('RouterModule', () => { | ||
let app: INestApplication; | ||
|
||
@Controller('/parent-controller') | ||
class ParentController {} | ||
@Controller('/child-controller') | ||
class ChildController {} | ||
@Controller('no-slash-controller') | ||
class NoSlashController {} | ||
|
||
class UnknownController {} | ||
@Module({ controllers: [ParentController] }) | ||
class ParentModule {} | ||
|
||
@Module({ controllers: [ChildController] }) | ||
class ChildModule {} | ||
|
||
@Module({}) | ||
class AuthModule {} | ||
@Module({}) | ||
class PaymentsModule {} | ||
|
||
@Module({ controllers: [NoSlashController] }) | ||
class NoSlashModule {} | ||
|
||
const routes1: Routes = [ | ||
{ | ||
path: 'parent', | ||
module: ParentModule, | ||
children: [ | ||
{ | ||
path: 'child', | ||
module: ChildModule, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const routes2: Routes = [ | ||
{ path: 'v1', children: [AuthModule, PaymentsModule] }, | ||
]; | ||
|
||
@Module({ | ||
imports: [ParentModule, ChildModule, RouterModule.register(routes1)], | ||
}) | ||
class MainModule {} | ||
|
||
@Module({ | ||
imports: [AuthModule, PaymentsModule, RouterModule.register(routes2)], | ||
}) | ||
class AppModule {} | ||
|
||
it('should add Path Metadata to all Routes', () => { | ||
const parentPath = Reflect.getMetadata(MODULE_PATH, ParentModule); | ||
const childPath = Reflect.getMetadata(MODULE_PATH, ChildModule); | ||
expect(parentPath).to.be.equal('/parent'); | ||
expect(childPath).to.be.equal('/parent/child'); | ||
}); | ||
|
||
it('should add paths even we omitted the module key', () => { | ||
const authPath = Reflect.getMetadata(MODULE_PATH, AuthModule); | ||
const paymentPath = Reflect.getMetadata(MODULE_PATH, PaymentsModule); | ||
expect(authPath).to.be.equal('/v1'); | ||
expect(paymentPath).to.be.equal('/v1'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { expect } from 'chai'; | ||
import { flatRoutes } from '../../../router/utils/flat-routes.util'; | ||
import { Module } from '@nestjs/common'; | ||
describe('FlatRoutes', () => { | ||
it('should flat all Routes, and we could also ommit the path', () => { | ||
@Module({}) | ||
class ParentModule {} | ||
@Module({}) | ||
class ChildModule {} | ||
@Module({}) | ||
class ChildChildModule {} | ||
@Module({}) | ||
class ChildModule2 {} | ||
@Module({}) | ||
class ParentChildModule {} | ||
@Module({}) | ||
class ChildChildModule2 {} | ||
@Module({}) | ||
class AuthModule {} | ||
@Module({}) | ||
class CatsModule {} | ||
@Module({}) | ||
class DogsModule {} | ||
|
||
@Module({}) | ||
class AuthModule2 {} | ||
@Module({}) | ||
class CatsModule2 {} | ||
@Module({}) | ||
class CatsModule3 {} | ||
@Module({}) | ||
class AuthModule3 {} | ||
const routes = [ | ||
{ | ||
path: '/parent', | ||
module: ParentModule, | ||
children: [ | ||
{ | ||
path: '/child', | ||
module: ChildModule, | ||
children: [ | ||
{ path: '/child2', module: ChildModule2 }, | ||
{ | ||
path: '/parentchild', | ||
module: ParentChildModule, | ||
children: [ | ||
{ | ||
path: '/childchild', | ||
module: ChildChildModule, | ||
children: [ | ||
{ path: '/child2child', module: ChildChildModule2 }, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ path: '/v1', children: [AuthModule, CatsModule, DogsModule] }, | ||
{ path: '/v2', children: [AuthModule2, CatsModule2] }, | ||
{ path: '/v3', children: [AuthModule3, CatsModule3] }, | ||
]; | ||
const expectedRoutes = [ | ||
{ path: '/parent', module: ParentModule }, | ||
{ path: '/parent/child', module: ChildModule }, | ||
{ path: '/parent/child/child2', module: ChildModule2 }, | ||
{ path: '/parent/child/parentchild', module: ParentChildModule }, | ||
{ | ||
path: '/parent/child/parentchild/childchild', | ||
module: ChildChildModule, | ||
}, | ||
{ | ||
path: '/parent/child/parentchild/childchild/child2child', | ||
module: ChildChildModule2, | ||
}, | ||
{ path: '/v1', module: AuthModule }, | ||
{ path: '/v1', module: CatsModule }, | ||
{ path: '/v1', module: DogsModule }, | ||
{ path: '/v2', module: AuthModule2 }, | ||
{ path: '/v2', module: CatsModule2 }, | ||
{ path: '/v3', module: AuthModule3 }, | ||
{ path: '/v3', module: CatsModule3 }, | ||
]; | ||
expect(flatRoutes(routes)).to.be.eql(expectedRoutes); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think doing this would solve all integration issues with any other packages that depend on routes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for example the swagger integration issues and also this issue nestjsx/nest-router#5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very uncommon but still: what if I have the same controller used in 2 modules? In this case, I'd override the metadata twice.