Skip to content

Commit

Permalink
fix(core): support extracting paths from ctrl with many paths #6463
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 22, 2021
1 parent e7fa960 commit 7cb7b1d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
34 changes: 22 additions & 12 deletions packages/core/middleware/routes-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ export class RoutesMapper {
},
];
}
const routePath: string = Reflect.getMetadata(PATH_METADATA, route);
if (this.isRouteInfo(routePath, route)) {
const routePathOrPaths: string | string[] = Reflect.getMetadata(
PATH_METADATA,
route,
);
if (this.isRouteInfo(routePathOrPaths, route)) {
return [
{
path: this.validateRoutePath(route.path),
Expand All @@ -43,21 +46,28 @@ export class RoutesMapper {
);
const concatPaths = <T>(acc: T[], currentValue: T[]) =>
acc.concat(currentValue);
return paths
.map(
item =>
item.path &&
item.path.map(p => ({
path:
this.validateGlobalPath(routePath) + this.validateRoutePath(p),
method: item.requestMethod,
})),

return []
.concat(routePathOrPaths)
.map(routePath =>
paths
.map(
item =>
item.path &&
item.path.map(p => ({
path:
this.validateGlobalPath(routePath) +
this.validateRoutePath(p),
method: item.requestMethod,
})),
)
.reduce(concatPaths, []),
)
.reduce(concatPaths, []);
}

private isRouteInfo(
path: string | undefined,
path: string | string[] | undefined,
objectOrClass: Function | RouteInfo,
): objectOrClass is RouteInfo {
return isUndefined(path);
Expand Down
28 changes: 28 additions & 0 deletions packages/core/test/middleware/routes-mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,32 @@ describe('RoutesMapper', () => {
{ path: '/test/another', method: RequestMethod.DELETE },
]);
});
@Controller(['test', 'test2'])
class TestRouteWithMultiplePaths {
@RequestMapping({ path: 'test' })
public getTest() {}

@RequestMapping({ path: 'another', method: RequestMethod.DELETE })
public getAnother() {}
}

it('should map a controller with multiple paths to "ControllerMetadata" in forRoutes', () => {
const config = {
middleware: 'Test',
forRoutes: [
{ path: 'test', method: RequestMethod.GET },
TestRouteWithMultiplePaths,
],
};

expect(mapper.mapRouteToRouteInfo(config.forRoutes[0])).to.deep.equal([
{ path: '/test', method: RequestMethod.GET },
]);
expect(mapper.mapRouteToRouteInfo(config.forRoutes[1])).to.deep.equal([
{ path: '/test/test', method: RequestMethod.GET },
{ path: '/test/another', method: RequestMethod.DELETE },
{ path: '/test2/test', method: RequestMethod.GET },
{ path: '/test2/another', method: RequestMethod.DELETE },
]);
});
});

0 comments on commit 7cb7b1d

Please sign in to comment.