From 387b9861781de058af5b15b15cd514fbf7ee7000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aura=20Rom=C3=A1n?= Date: Mon, 26 Aug 2024 19:23:00 +0200 Subject: [PATCH 1/3] feat(router): add FS group syntax This allows for grouping routes together in the router: ```ts -| routes/ ---| information.get.ts ---| (legal)/ -----| license.get.ts -----| privacy.get.ts ``` This will produce the following routes: - `GET /information` - `GET /license` - `GET /privacy` The `legal` group is ignored for purposes of the route URL path. --- packages/api/src/lib/structures/Route.ts | 8 ++++---- .../api/src/lib/structures/router/RouterRoot.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/api/src/lib/structures/Route.ts b/packages/api/src/lib/structures/Route.ts index 87c3ced7..0158cb91 100644 --- a/packages/api/src/lib/structures/Route.ts +++ b/packages/api/src/lib/structures/Route.ts @@ -68,10 +68,10 @@ export abstract class Route exten super(context, options); const api = this.container.server.options; - const path = ([] as string[]).concat( - RouterRoot.normalize(api.prefix), - RouterRoot.normalize(options.route ?? (this.location.virtual ? this.name : this.location.directories.concat(this.name).join('/'))) - ); + const path = [ + ...RouterRoot.normalize(api.prefix), + ...RouterRoot.normalize(options.route ?? RouterRoot.makeRoutePathForPiece(this.location.directories, this.name)) + ]; const methods = new Set(options.methods); const implied = RouterRoot.extractMethod(path); diff --git a/packages/api/src/lib/structures/router/RouterRoot.ts b/packages/api/src/lib/structures/router/RouterRoot.ts index cbe18623..26c0c92e 100644 --- a/packages/api/src/lib/structures/router/RouterRoot.ts +++ b/packages/api/src/lib/structures/router/RouterRoot.ts @@ -38,6 +38,23 @@ export class RouterRoot extends RouterBranch { return ''; } + public static makeRoutePathForPiece(directories: readonly string[], name: string): string { + const parts: string[] = []; + for (let directory of directories) { + directory = directory.trim(); + + // If empty, skip: + if (isNullishOrEmpty(directory)) continue; + // If it's a group, skip: + if (directory.startsWith('(') && directory.endsWith(')')) continue; + + parts.push(directory); + } + + parts.push(name); + return parts.join('/'); + } + public static normalize(path: string | null | undefined): string[] { const parts = [] as string[]; if (isNullishOrEmpty(path)) return parts; From 0829ab7702efa1c6ea8b7288d43ee7aa5a31ee13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aura=20Rom=C3=A1n?= Date: Mon, 26 Aug 2024 19:29:55 +0200 Subject: [PATCH 2/3] fix: sonar --- packages/api/src/lib/structures/router/RouterRoot.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/api/src/lib/structures/router/RouterRoot.ts b/packages/api/src/lib/structures/router/RouterRoot.ts index 26c0c92e..52271cfc 100644 --- a/packages/api/src/lib/structures/router/RouterRoot.ts +++ b/packages/api/src/lib/structures/router/RouterRoot.ts @@ -40,15 +40,15 @@ export class RouterRoot extends RouterBranch { public static makeRoutePathForPiece(directories: readonly string[], name: string): string { const parts: string[] = []; - for (let directory of directories) { - directory = directory.trim(); + for (const directory of directories) { + const trimmed = directory.trim(); // If empty, skip: - if (isNullishOrEmpty(directory)) continue; + if (isNullishOrEmpty(trimmed)) continue; // If it's a group, skip: - if (directory.startsWith('(') && directory.endsWith(')')) continue; + if (trimmed.startsWith('(') && trimmed.endsWith(')')) continue; - parts.push(directory); + parts.push(trimmed); } parts.push(name); From 08c876f3a7ab2a499910dca93953a935d3742c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aura=20Rom=C3=A1n?= Date: Mon, 26 Aug 2024 19:33:43 +0200 Subject: [PATCH 3/3] fix: also trim file name --- packages/api/src/lib/structures/router/RouterRoot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api/src/lib/structures/router/RouterRoot.ts b/packages/api/src/lib/structures/router/RouterRoot.ts index 52271cfc..dc6fe964 100644 --- a/packages/api/src/lib/structures/router/RouterRoot.ts +++ b/packages/api/src/lib/structures/router/RouterRoot.ts @@ -51,7 +51,7 @@ export class RouterRoot extends RouterBranch { parts.push(trimmed); } - parts.push(name); + parts.push(name.trim()); return parts.join('/'); }