Skip to content
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: add appendChildrenRoutesFirst configuration option #2711

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@ on [Response filters](/docs/response-filter.md).

Object configure Multer. See more on [Upload file](/tutorials/serve-static-files.md).

## router

```typescript
@Configuration({
router: {
appendChildrenRoutesFirst: true
}
})
```

### router.appendChildrenRoutesFirst

- type: `boolean`

Append children routes before the controller routes itself. Defaults to `false`, but will be deprecated and set to `true` in next major version.

## jsonMapper

```typescript
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface PlatformRouterSettings {
appendChildrenRoutesFirst?: boolean;
}

declare global {
namespace TsED {
interface Configuration extends Record<string, any> {
router?: PlatformRouterSettings;
}
}
}
25 changes: 17 additions & 8 deletions packages/platform/platform-router/src/domain/PlatformRouters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,19 @@ export class PlatformRouters {
}

const useBefore = getValue(provider, "middlewares.useBefore", []);

const middlewares: any[] = [...parentMiddlewares, ...useBefore];
const {children} = provider;

// Set default to true in next major version
const appendChildrenRoutesFirst = this.injector.settings.get<boolean>('router.appendChildrenRoutesFirst', false)

if (appendChildrenRoutesFirst) {
children.forEach((token: Type<any>) => {
const nested = this.from(token, middlewares);
router.use(nested);
});
}

getOperationsRoutes(provider.token, {allowedVerbs: this.allowedVerbs}).forEach((operationRoute) => {
const {endpoint} = operationRoute;
const {beforeMiddlewares, middlewares: mldwrs, afterMiddlewares} = endpoint;
Expand Down Expand Up @@ -112,13 +122,12 @@ export class PlatformRouters {
);
});

const middlewares: any[] = [...parentMiddlewares, ...useBefore];

children.forEach((token: Type<any>) => {
const nested = this.from(token, middlewares);

router.use(nested);
});
if (!appendChildrenRoutesFirst) {
children.forEach((token: Type<any>) => {
const nested = this.from(token, middlewares);
router.use(nested);
});
}

return router;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function createAppRouterFixture() {
describe("routers integration", () => {
beforeEach(() => PlatformTest.create());
afterEach(() => PlatformTest.reset());

describe("getLayers()", () => {
it("should declare router", () => {
const {platformRouters, appRouter} = createAppRouterFixture();
Expand Down Expand Up @@ -99,5 +100,47 @@ describe("routers integration", () => {
"/rest/platform/:platform/comments"
]);
});

it('should declare correctly with appendChildrenRoutesFirst', () => {
const {injector, platformRouters, appRouter} = createAppRouterFixture();
injector.settings.set('router.appendChildrenRoutesFirst', true);

platformRouters.prebuild();

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

const layers = platformRouters.getLayers(appRouter);

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

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