Skip to content

Commit

Permalink
fix(platform-router): produce routes for all nested controllers add t…
Browse files Browse the repository at this point in the history
…o multiple parent controller

Close: #2221
  • Loading branch information
Romakita committed Jan 24, 2023
1 parent bd366ff commit 9cdcfd2
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 16 deletions.
26 changes: 24 additions & 2 deletions packages/platform/common/src/services/Platform.spec.ts
Expand Up @@ -39,7 +39,7 @@ export class FlaggedCommentController {
children: [FlaggedCommentController]
})
export class CommentController {
@Get("/")
@Get("/all")
get() {}
}

Expand All @@ -48,6 +48,14 @@ export class CommentController {
children: [CommentController]
})
export class DomainController {
@Get("/all")
get() {}
}
@Controller({
path: "/platform/:platform",
children: [CommentController]
})
export class PlatformController {
@Get("/")
get() {}
}
Expand Down Expand Up @@ -93,10 +101,12 @@ describe("Platform", () => {

// WHEN
platform.addRoutes([{route: "/rest", token: DomainController}]);
platform.addRoutes([{route: "/rest", token: PlatformController}]);

platform.getLayers();

const result = platform.getMountedControllers();

console.log(result);
// THEN
const data = result.map((c) => ({route: c.route, name: nameOf(c.provider)}));
expect(data).toEqual([
Expand All @@ -111,6 +121,18 @@ describe("Platform", () => {
{
name: "Token:FlaggedCommentController:FlaggedCommentController",
route: "/rest/domain/:contextID/comments"
},
{
name: "Token:PlatformController:PlatformController",
route: "/rest"
},
{
name: "Token:CommentController:CommentController",
route: "/rest/platform/:platform"
},
{
name: "Token:FlaggedCommentController:FlaggedCommentController",
route: "/rest/platform/:platform/comments"
}
]);
});
Expand Down
13 changes: 9 additions & 4 deletions packages/platform/common/src/services/Platform.ts
Expand Up @@ -61,10 +61,15 @@ export class Platform {
public getMountedControllers() {
const controllers = this.getLayers().reduce((controllers, layer) => {
if (layer.isProvider()) {
controllers.set(layer.provider.token, {
route: String(layer.getBasePath()),
provider: layer.provider
});
const route = String(layer.getBasePath());
const key = `${layer.provider.toString()}:${route}`;

if (!controllers.has(key)) {
controllers.set(key, {
route,
provider: layer.provider
});
}
}

return controllers;
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/platform-router/package.json
Expand Up @@ -59,4 +59,4 @@
"optional": false
}
}
}
}
8 changes: 6 additions & 2 deletions packages/platform/platform-router/src/domain/PlatformLayer.ts
Expand Up @@ -39,8 +39,11 @@ export class PlatformLayer {
constructor(props: Partial<PlatformLayerProps> = {}) {
Object.assign(this, props);

this.layers.forEach((layer) => {
layer.parent = this;
this.layers = this.layers.map((layer) => {
return new PlatformLayer({
...layer,
parent: this
});
});
}

Expand All @@ -58,6 +61,7 @@ export class PlatformLayer {

inspect() {
return {
basePath: this.getBasePath(),
path: this.path,
method: this.method,
handlers: this.handlers.map((item: any) => String(item)),
Expand Down
Expand Up @@ -3,6 +3,7 @@
exports[`routers integration getLayers() should declare router - appRouter 1`] = `
Array [
Object {
"basePath": "/rest",
"handlers": Array [
"",
"useBefore",
Expand All @@ -20,6 +21,7 @@ Array [
"path": "/rest/controller",
},
Object {
"basePath": "/rest",
"handlers": Array [
"",
"useBefore",
Expand All @@ -37,6 +39,7 @@ Array [
"path": "/rest/controller",
},
Object {
"basePath": "/rest",
"handlers": Array [
"",
"useBefore",
Expand All @@ -54,6 +57,7 @@ Array [
"path": "/rest/controller/:id",
},
Object {
"basePath": "/rest",
"handlers": Array [
"",
"useBefore",
Expand All @@ -71,6 +75,7 @@ Array [
"path": "/rest/controller/:id",
},
Object {
"basePath": "/rest",
"handlers": Array [
"",
"useBefore",
Expand All @@ -88,6 +93,7 @@ Array [
"path": "/rest/controller/:id",
},
Object {
"basePath": "/rest",
"handlers": Array [
"",
"useBefore",
Expand All @@ -105,6 +111,7 @@ Array [
"path": "/rest/controller/:id",
},
Object {
"basePath": "/rest",
"handlers": Array [
"",
"useBefore",
Expand All @@ -122,6 +129,7 @@ Array [
"path": "/rest/controller/:id",
},
Object {
"basePath": "/rest/controller",
"handlers": Array [
"",
"useBefore",
Expand All @@ -139,6 +147,7 @@ Array [
"path": "/rest/controller/nested",
},
Object {
"basePath": "/rest/controller",
"handlers": Array [
"",
"useBefore",
Expand All @@ -156,6 +165,7 @@ Array [
"path": "/rest/controller/nested",
},
Object {
"basePath": "/rest/controller",
"handlers": Array [
"",
"useBefore",
Expand All @@ -173,6 +183,7 @@ Array [
"path": "/rest/controller/nested/:id",
},
Object {
"basePath": "/rest/controller",
"handlers": Array [
"",
"useBefore",
Expand All @@ -190,6 +201,7 @@ Array [
"path": "/rest/controller/nested/:id",
},
Object {
"basePath": "/rest/controller",
"handlers": Array [
"",
"useBefore",
Expand All @@ -207,6 +219,7 @@ Array [
"path": "/rest/controller/nested/:id",
},
Object {
"basePath": "/rest/controller",
"handlers": Array [
"",
"useBefore",
Expand All @@ -224,6 +237,7 @@ Array [
"path": "/rest/controller/nested/:id",
},
Object {
"basePath": "/rest/controller",
"handlers": Array [
"",
"useBefore",
Expand All @@ -246,6 +260,7 @@ Array [
exports[`routers integration getLayers() should declare router 1`] = `
Array [
Object {
"basePath": "",
"handlers": Array [
"",
"useBefore",
Expand All @@ -263,6 +278,7 @@ Array [
"path": "/controller",
},
Object {
"basePath": "",
"handlers": Array [
"",
"useBefore",
Expand All @@ -280,6 +296,7 @@ Array [
"path": "/controller",
},
Object {
"basePath": "",
"handlers": Array [
"",
"useBefore",
Expand All @@ -297,6 +314,7 @@ Array [
"path": "/controller/:id",
},
Object {
"basePath": "",
"handlers": Array [
"",
"useBefore",
Expand All @@ -314,6 +332,7 @@ Array [
"path": "/controller/:id",
},
Object {
"basePath": "",
"handlers": Array [
"",
"useBefore",
Expand All @@ -331,6 +350,7 @@ Array [
"path": "/controller/:id",
},
Object {
"basePath": "",
"handlers": Array [
"",
"useBefore",
Expand All @@ -348,6 +368,7 @@ Array [
"path": "/controller/:id",
},
Object {
"basePath": "",
"handlers": Array [
"",
"useBefore",
Expand All @@ -365,6 +386,7 @@ Array [
"path": "/controller/:id",
},
Object {
"basePath": "/controller",
"handlers": Array [
"[object Object]",
],
Expand Down
Expand Up @@ -32,6 +32,15 @@ export class DomainController {
get() {}
}

@Controller({
path: "/platform/:platform",
children: [CommentController]
})
export class PlatformController {
@Get("/")
get() {}
}

function createAppRouterFixture() {
const injector = new InjectorService();
const platformRouters = injector.invoke<PlatformRouters>(PlatformRouters);
Expand All @@ -41,6 +50,7 @@ function createAppRouterFixture() {
injector.addProvider(FlaggedCommentController, {});
injector.addProvider(CommentController, {});
injector.addProvider(DomainController, {});
injector.addProvider(PlatformController, {});

return {injector, appRouter, platformRouters, platformParams};
}
Expand All @@ -54,9 +64,8 @@ describe("routers integration", () => {

platformRouters.prebuild();

const router = platformRouters.from(DomainController);

appRouter.use("/rest", router);
appRouter.use("/rest", platformRouters.from(DomainController));
appRouter.use("/rest", platformRouters.from(PlatformController));

const layers = platformRouters.getLayers(appRouter);

Expand All @@ -68,14 +77,27 @@ describe("routers integration", () => {
"/rest/domain/:contextID",
"/rest/domain/:contextID/comments",
"/rest/domain/:contextID/comments/flag",
"/rest/domain/:contextID/comments/:commentID/flag"
"/rest/domain/:contextID/comments/:commentID/flag",
"/rest/platform/:platform",
"/rest/platform/:platform/comments",
"/rest/platform/:platform/comments/flag",
"/rest/platform/:platform/comments/:commentID/flag"
]);

expect(
layers.map((layer) => {
return layer.getBasePath();
})
).toEqual(["/rest", "/rest/domain/:contextID", "/rest/domain/:contextID/comments", "/rest/domain/:contextID/comments"]);
).toEqual([
"/rest",
"/rest/domain/:contextID",
"/rest/domain/:contextID/comments",
"/rest/domain/:contextID/comments",
"/rest",
"/rest/platform/:platform",
"/rest/platform/:platform/comments",
"/rest/platform/:platform/comments"
]);
});
});
});

0 comments on commit 9cdcfd2

Please sign in to comment.