From f29a9767e21d81d374e4f6e1c3a1c4f28280c839 Mon Sep 17 00:00:00 2001 From: Mihovil Ilakovac Date: Fri, 6 Sep 2024 22:44:41 +0200 Subject: [PATCH] Update types --- .../templates/sdk/wasp/client/router/types.ts | 50 +++++++++++-------- .../waspBuild/.wasp/build/.waspchecksums | 2 +- .../build/sdk/wasp/client/router/types.ts | 50 +++++++++++-------- .../sdk/wasp/dist/client/router/types.d.ts | 16 ++++-- .../.wasp/out/sdk/wasp/client/router/types.ts | 50 +++++++++++-------- .../sdk/wasp/dist/client/router/types.d.ts | 16 ++++-- .../waspCompile/.wasp/out/.waspchecksums | 2 +- .../.wasp/out/sdk/wasp/client/router/types.ts | 50 +++++++++++-------- .../sdk/wasp/dist/client/router/types.d.ts | 16 ++++-- .../waspComplexTest/.wasp/out/.waspchecksums | 2 +- .../.wasp/out/sdk/wasp/client/router/types.ts | 50 +++++++++++-------- .../sdk/wasp/dist/client/router/types.d.ts | 16 ++++-- .../waspJob/.wasp/out/.waspchecksums | 2 +- .../.wasp/out/sdk/wasp/client/router/types.ts | 50 +++++++++++-------- .../sdk/wasp/dist/client/router/types.d.ts | 16 ++++-- .../waspMigrate/.wasp/out/.waspchecksums | 2 +- .../.wasp/out/sdk/wasp/client/router/types.ts | 50 +++++++++++-------- .../sdk/wasp/dist/client/router/types.d.ts | 16 ++++-- 18 files changed, 273 insertions(+), 183 deletions(-) diff --git a/waspc/data/Generator/templates/sdk/wasp/client/router/types.ts b/waspc/data/Generator/templates/sdk/wasp/client/router/types.ts index fb87a803a5..283e21d832 100644 --- a/waspc/data/Generator/templates/sdk/wasp/client/router/types.ts +++ b/waspc/data/Generator/templates/sdk/wasp/client/router/types.ts @@ -21,7 +21,7 @@ export type Search = type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute + to: ExpandRouteOnOptionalStaticSegments } & ParamsFromBuildFn } @@ -40,25 +40,32 @@ type ParamsFromBuildFn = Parameters[0] extends { ? { params: Params } : { params?: never } -// Optional static segments handling -type GenerateRoute = S extends '/' +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' - : `/${JoinPath>>>}` + : `/${JoinPath>>>}` -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' - ? [...ExplodeOptionalSegments] - : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] + ? [...ExpandOptionalSegments] + : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T -type _ExplodeOptionalSegment = T extends `:${infer P}` - // Param segment - ? { segment: T } +type _ExpandOptionalSegment = T extends `:${infer P}` + ? // Param segment + { segment: T } : T extends `${infer S}?` - // Optional segment - ? { optionalSegment: S } - // Regular segment - : { segment: T } + ? // Optional segment + { optionalSegment: S } + : // Regular segment + { segment: T } type Segment = { segment: string } type OptionalSegment = { optionalSegment: string } @@ -68,12 +75,12 @@ type Elem = Segment | OptionalSegment type JoinSegments = T extends [] ? [] : T extends [infer First extends Elem, ...infer Rest extends Elem[]] - ? First extends Segment - ? [First['segment'], ...JoinSegments] - : First extends OptionalSegment - ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + ? First extends Segment + ? [First['segment'], ...JoinSegments] + : First extends OptionalSegment + ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + : [] : [] - : [] type SplitPath = S extends `${infer T}/${infer U}` ? [T, ...SplitPath] @@ -82,6 +89,5 @@ type SplitPath = S extends `${infer T}/${infer U}` type JoinPath = T extends [infer Only extends string] ? Only : T extends [infer First extends string, ...infer Rest extends string[]] - ? `${First}/${JoinPath}` - : never - \ No newline at end of file + ? `${First}/${JoinPath}` + : never diff --git a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/.waspchecksums b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/.waspchecksums index ebb0bd9ac8..69162894a1 100644 --- a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/.waspchecksums +++ b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/.waspchecksums @@ -130,7 +130,7 @@ "file", "../out/sdk/wasp/client/router/types.ts" ], - "c4aaf60fe2a920d32937aaf56d422ad77444d0afb14ed5396112896d55c974e2" + "c5b95d3525fa7d266b1d2a8f32aa0ab1e93d36a5d5bcc62c7dabc225151d4f73" ], [ [ diff --git a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/sdk/wasp/client/router/types.ts b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/sdk/wasp/client/router/types.ts index fb87a803a5..283e21d832 100644 --- a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/sdk/wasp/client/router/types.ts +++ b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/sdk/wasp/client/router/types.ts @@ -21,7 +21,7 @@ export type Search = type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute + to: ExpandRouteOnOptionalStaticSegments } & ParamsFromBuildFn } @@ -40,25 +40,32 @@ type ParamsFromBuildFn = Parameters[0] extends { ? { params: Params } : { params?: never } -// Optional static segments handling -type GenerateRoute = S extends '/' +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' - : `/${JoinPath>>>}` + : `/${JoinPath>>>}` -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' - ? [...ExplodeOptionalSegments] - : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] + ? [...ExpandOptionalSegments] + : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T -type _ExplodeOptionalSegment = T extends `:${infer P}` - // Param segment - ? { segment: T } +type _ExpandOptionalSegment = T extends `:${infer P}` + ? // Param segment + { segment: T } : T extends `${infer S}?` - // Optional segment - ? { optionalSegment: S } - // Regular segment - : { segment: T } + ? // Optional segment + { optionalSegment: S } + : // Regular segment + { segment: T } type Segment = { segment: string } type OptionalSegment = { optionalSegment: string } @@ -68,12 +75,12 @@ type Elem = Segment | OptionalSegment type JoinSegments = T extends [] ? [] : T extends [infer First extends Elem, ...infer Rest extends Elem[]] - ? First extends Segment - ? [First['segment'], ...JoinSegments] - : First extends OptionalSegment - ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + ? First extends Segment + ? [First['segment'], ...JoinSegments] + : First extends OptionalSegment + ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + : [] : [] - : [] type SplitPath = S extends `${infer T}/${infer U}` ? [T, ...SplitPath] @@ -82,6 +89,5 @@ type SplitPath = S extends `${infer T}/${infer U}` type JoinPath = T extends [infer Only extends string] ? Only : T extends [infer First extends string, ...infer Rest extends string[]] - ? `${First}/${JoinPath}` - : never - \ No newline at end of file + ? `${First}/${JoinPath}` + : never diff --git a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/sdk/wasp/dist/client/router/types.d.ts b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/sdk/wasp/dist/client/router/types.d.ts index 1fe681a98b..8f9b8d95fd 100644 --- a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/sdk/wasp/dist/client/router/types.d.ts +++ b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/build/sdk/wasp/dist/client/router/types.d.ts @@ -10,7 +10,7 @@ export type Params = { export type Search = string[][] | Record | string | URLSearchParams; type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute; + to: ExpandRouteOnOptionalStaticSegments; } & ParamsFromBuildFn; }; type RoutesDefinition = { @@ -27,9 +27,17 @@ type ParamsFromBuildFn = Parameters[0] extends { } : { params?: never; }; -type GenerateRoute = S extends '/' ? '/' : `/${JoinPath>>>}`; -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExplodeOptionalSegments] : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] : T; -type _ExplodeOptionalSegment = T extends `:${infer P}` ? { +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' : `/${JoinPath>>>}`; +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExpandOptionalSegments] : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T; +type _ExpandOptionalSegment = T extends `:${infer P}` ? { segment: T; } : T extends `${infer S}?` ? { optionalSegment: S; diff --git a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/out/sdk/wasp/client/router/types.ts b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/out/sdk/wasp/client/router/types.ts index fb87a803a5..283e21d832 100644 --- a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/out/sdk/wasp/client/router/types.ts +++ b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/out/sdk/wasp/client/router/types.ts @@ -21,7 +21,7 @@ export type Search = type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute + to: ExpandRouteOnOptionalStaticSegments } & ParamsFromBuildFn } @@ -40,25 +40,32 @@ type ParamsFromBuildFn = Parameters[0] extends { ? { params: Params } : { params?: never } -// Optional static segments handling -type GenerateRoute = S extends '/' +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' - : `/${JoinPath>>>}` + : `/${JoinPath>>>}` -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' - ? [...ExplodeOptionalSegments] - : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] + ? [...ExpandOptionalSegments] + : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T -type _ExplodeOptionalSegment = T extends `:${infer P}` - // Param segment - ? { segment: T } +type _ExpandOptionalSegment = T extends `:${infer P}` + ? // Param segment + { segment: T } : T extends `${infer S}?` - // Optional segment - ? { optionalSegment: S } - // Regular segment - : { segment: T } + ? // Optional segment + { optionalSegment: S } + : // Regular segment + { segment: T } type Segment = { segment: string } type OptionalSegment = { optionalSegment: string } @@ -68,12 +75,12 @@ type Elem = Segment | OptionalSegment type JoinSegments = T extends [] ? [] : T extends [infer First extends Elem, ...infer Rest extends Elem[]] - ? First extends Segment - ? [First['segment'], ...JoinSegments] - : First extends OptionalSegment - ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + ? First extends Segment + ? [First['segment'], ...JoinSegments] + : First extends OptionalSegment + ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + : [] : [] - : [] type SplitPath = S extends `${infer T}/${infer U}` ? [T, ...SplitPath] @@ -82,6 +89,5 @@ type SplitPath = S extends `${infer T}/${infer U}` type JoinPath = T extends [infer Only extends string] ? Only : T extends [infer First extends string, ...infer Rest extends string[]] - ? `${First}/${JoinPath}` - : never - \ No newline at end of file + ? `${First}/${JoinPath}` + : never diff --git a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/out/sdk/wasp/dist/client/router/types.d.ts b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/out/sdk/wasp/dist/client/router/types.d.ts index 1fe681a98b..8f9b8d95fd 100644 --- a/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/out/sdk/wasp/dist/client/router/types.d.ts +++ b/waspc/e2e-test/test-outputs/waspBuild-golden/waspBuild/.wasp/out/sdk/wasp/dist/client/router/types.d.ts @@ -10,7 +10,7 @@ export type Params = { export type Search = string[][] | Record | string | URLSearchParams; type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute; + to: ExpandRouteOnOptionalStaticSegments; } & ParamsFromBuildFn; }; type RoutesDefinition = { @@ -27,9 +27,17 @@ type ParamsFromBuildFn = Parameters[0] extends { } : { params?: never; }; -type GenerateRoute = S extends '/' ? '/' : `/${JoinPath>>>}`; -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExplodeOptionalSegments] : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] : T; -type _ExplodeOptionalSegment = T extends `:${infer P}` ? { +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' : `/${JoinPath>>>}`; +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExpandOptionalSegments] : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T; +type _ExpandOptionalSegment = T extends `:${infer P}` ? { segment: T; } : T extends `${infer S}?` ? { optionalSegment: S; diff --git a/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/.waspchecksums b/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/.waspchecksums index 69aaf8ed12..77ed774736 100644 --- a/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/.waspchecksums +++ b/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/.waspchecksums @@ -130,7 +130,7 @@ "file", "../out/sdk/wasp/client/router/types.ts" ], - "c4aaf60fe2a920d32937aaf56d422ad77444d0afb14ed5396112896d55c974e2" + "c5b95d3525fa7d266b1d2a8f32aa0ab1e93d36a5d5bcc62c7dabc225151d4f73" ], [ [ diff --git a/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/sdk/wasp/client/router/types.ts b/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/sdk/wasp/client/router/types.ts index fb87a803a5..283e21d832 100644 --- a/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/sdk/wasp/client/router/types.ts +++ b/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/sdk/wasp/client/router/types.ts @@ -21,7 +21,7 @@ export type Search = type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute + to: ExpandRouteOnOptionalStaticSegments } & ParamsFromBuildFn } @@ -40,25 +40,32 @@ type ParamsFromBuildFn = Parameters[0] extends { ? { params: Params } : { params?: never } -// Optional static segments handling -type GenerateRoute = S extends '/' +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' - : `/${JoinPath>>>}` + : `/${JoinPath>>>}` -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' - ? [...ExplodeOptionalSegments] - : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] + ? [...ExpandOptionalSegments] + : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T -type _ExplodeOptionalSegment = T extends `:${infer P}` - // Param segment - ? { segment: T } +type _ExpandOptionalSegment = T extends `:${infer P}` + ? // Param segment + { segment: T } : T extends `${infer S}?` - // Optional segment - ? { optionalSegment: S } - // Regular segment - : { segment: T } + ? // Optional segment + { optionalSegment: S } + : // Regular segment + { segment: T } type Segment = { segment: string } type OptionalSegment = { optionalSegment: string } @@ -68,12 +75,12 @@ type Elem = Segment | OptionalSegment type JoinSegments = T extends [] ? [] : T extends [infer First extends Elem, ...infer Rest extends Elem[]] - ? First extends Segment - ? [First['segment'], ...JoinSegments] - : First extends OptionalSegment - ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + ? First extends Segment + ? [First['segment'], ...JoinSegments] + : First extends OptionalSegment + ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + : [] : [] - : [] type SplitPath = S extends `${infer T}/${infer U}` ? [T, ...SplitPath] @@ -82,6 +89,5 @@ type SplitPath = S extends `${infer T}/${infer U}` type JoinPath = T extends [infer Only extends string] ? Only : T extends [infer First extends string, ...infer Rest extends string[]] - ? `${First}/${JoinPath}` - : never - \ No newline at end of file + ? `${First}/${JoinPath}` + : never diff --git a/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/sdk/wasp/dist/client/router/types.d.ts b/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/sdk/wasp/dist/client/router/types.d.ts index 1fe681a98b..8f9b8d95fd 100644 --- a/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/sdk/wasp/dist/client/router/types.d.ts +++ b/waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/sdk/wasp/dist/client/router/types.d.ts @@ -10,7 +10,7 @@ export type Params = { export type Search = string[][] | Record | string | URLSearchParams; type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute; + to: ExpandRouteOnOptionalStaticSegments; } & ParamsFromBuildFn; }; type RoutesDefinition = { @@ -27,9 +27,17 @@ type ParamsFromBuildFn = Parameters[0] extends { } : { params?: never; }; -type GenerateRoute = S extends '/' ? '/' : `/${JoinPath>>>}`; -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExplodeOptionalSegments] : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] : T; -type _ExplodeOptionalSegment = T extends `:${infer P}` ? { +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' : `/${JoinPath>>>}`; +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExpandOptionalSegments] : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T; +type _ExpandOptionalSegment = T extends `:${infer P}` ? { segment: T; } : T extends `${infer S}?` ? { optionalSegment: S; diff --git a/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/.waspchecksums b/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/.waspchecksums index a5efeb0245..a95e12d720 100644 --- a/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/.waspchecksums +++ b/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/.waspchecksums @@ -333,7 +333,7 @@ "file", "../out/sdk/wasp/client/router/types.ts" ], - "c4aaf60fe2a920d32937aaf56d422ad77444d0afb14ed5396112896d55c974e2" + "c5b95d3525fa7d266b1d2a8f32aa0ab1e93d36a5d5bcc62c7dabc225151d4f73" ], [ [ diff --git a/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/sdk/wasp/client/router/types.ts b/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/sdk/wasp/client/router/types.ts index fb87a803a5..283e21d832 100644 --- a/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/sdk/wasp/client/router/types.ts +++ b/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/sdk/wasp/client/router/types.ts @@ -21,7 +21,7 @@ export type Search = type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute + to: ExpandRouteOnOptionalStaticSegments } & ParamsFromBuildFn } @@ -40,25 +40,32 @@ type ParamsFromBuildFn = Parameters[0] extends { ? { params: Params } : { params?: never } -// Optional static segments handling -type GenerateRoute = S extends '/' +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' - : `/${JoinPath>>>}` + : `/${JoinPath>>>}` -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' - ? [...ExplodeOptionalSegments] - : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] + ? [...ExpandOptionalSegments] + : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T -type _ExplodeOptionalSegment = T extends `:${infer P}` - // Param segment - ? { segment: T } +type _ExpandOptionalSegment = T extends `:${infer P}` + ? // Param segment + { segment: T } : T extends `${infer S}?` - // Optional segment - ? { optionalSegment: S } - // Regular segment - : { segment: T } + ? // Optional segment + { optionalSegment: S } + : // Regular segment + { segment: T } type Segment = { segment: string } type OptionalSegment = { optionalSegment: string } @@ -68,12 +75,12 @@ type Elem = Segment | OptionalSegment type JoinSegments = T extends [] ? [] : T extends [infer First extends Elem, ...infer Rest extends Elem[]] - ? First extends Segment - ? [First['segment'], ...JoinSegments] - : First extends OptionalSegment - ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + ? First extends Segment + ? [First['segment'], ...JoinSegments] + : First extends OptionalSegment + ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + : [] : [] - : [] type SplitPath = S extends `${infer T}/${infer U}` ? [T, ...SplitPath] @@ -82,6 +89,5 @@ type SplitPath = S extends `${infer T}/${infer U}` type JoinPath = T extends [infer Only extends string] ? Only : T extends [infer First extends string, ...infer Rest extends string[]] - ? `${First}/${JoinPath}` - : never - \ No newline at end of file + ? `${First}/${JoinPath}` + : never diff --git a/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/sdk/wasp/dist/client/router/types.d.ts b/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/sdk/wasp/dist/client/router/types.d.ts index 1fe681a98b..8f9b8d95fd 100644 --- a/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/sdk/wasp/dist/client/router/types.d.ts +++ b/waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/sdk/wasp/dist/client/router/types.d.ts @@ -10,7 +10,7 @@ export type Params = { export type Search = string[][] | Record | string | URLSearchParams; type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute; + to: ExpandRouteOnOptionalStaticSegments; } & ParamsFromBuildFn; }; type RoutesDefinition = { @@ -27,9 +27,17 @@ type ParamsFromBuildFn = Parameters[0] extends { } : { params?: never; }; -type GenerateRoute = S extends '/' ? '/' : `/${JoinPath>>>}`; -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExplodeOptionalSegments] : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] : T; -type _ExplodeOptionalSegment = T extends `:${infer P}` ? { +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' : `/${JoinPath>>>}`; +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExpandOptionalSegments] : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T; +type _ExpandOptionalSegment = T extends `:${infer P}` ? { segment: T; } : T extends `${infer S}?` ? { optionalSegment: S; diff --git a/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/.waspchecksums b/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/.waspchecksums index 7ed27ca94d..8ab6f058ee 100644 --- a/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/.waspchecksums +++ b/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/.waspchecksums @@ -130,7 +130,7 @@ "file", "../out/sdk/wasp/client/router/types.ts" ], - "c4aaf60fe2a920d32937aaf56d422ad77444d0afb14ed5396112896d55c974e2" + "c5b95d3525fa7d266b1d2a8f32aa0ab1e93d36a5d5bcc62c7dabc225151d4f73" ], [ [ diff --git a/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/sdk/wasp/client/router/types.ts b/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/sdk/wasp/client/router/types.ts index fb87a803a5..283e21d832 100644 --- a/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/sdk/wasp/client/router/types.ts +++ b/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/sdk/wasp/client/router/types.ts @@ -21,7 +21,7 @@ export type Search = type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute + to: ExpandRouteOnOptionalStaticSegments } & ParamsFromBuildFn } @@ -40,25 +40,32 @@ type ParamsFromBuildFn = Parameters[0] extends { ? { params: Params } : { params?: never } -// Optional static segments handling -type GenerateRoute = S extends '/' +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' - : `/${JoinPath>>>}` + : `/${JoinPath>>>}` -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' - ? [...ExplodeOptionalSegments] - : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] + ? [...ExpandOptionalSegments] + : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T -type _ExplodeOptionalSegment = T extends `:${infer P}` - // Param segment - ? { segment: T } +type _ExpandOptionalSegment = T extends `:${infer P}` + ? // Param segment + { segment: T } : T extends `${infer S}?` - // Optional segment - ? { optionalSegment: S } - // Regular segment - : { segment: T } + ? // Optional segment + { optionalSegment: S } + : // Regular segment + { segment: T } type Segment = { segment: string } type OptionalSegment = { optionalSegment: string } @@ -68,12 +75,12 @@ type Elem = Segment | OptionalSegment type JoinSegments = T extends [] ? [] : T extends [infer First extends Elem, ...infer Rest extends Elem[]] - ? First extends Segment - ? [First['segment'], ...JoinSegments] - : First extends OptionalSegment - ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + ? First extends Segment + ? [First['segment'], ...JoinSegments] + : First extends OptionalSegment + ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + : [] : [] - : [] type SplitPath = S extends `${infer T}/${infer U}` ? [T, ...SplitPath] @@ -82,6 +89,5 @@ type SplitPath = S extends `${infer T}/${infer U}` type JoinPath = T extends [infer Only extends string] ? Only : T extends [infer First extends string, ...infer Rest extends string[]] - ? `${First}/${JoinPath}` - : never - \ No newline at end of file + ? `${First}/${JoinPath}` + : never diff --git a/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/sdk/wasp/dist/client/router/types.d.ts b/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/sdk/wasp/dist/client/router/types.d.ts index 1fe681a98b..8f9b8d95fd 100644 --- a/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/sdk/wasp/dist/client/router/types.d.ts +++ b/waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/sdk/wasp/dist/client/router/types.d.ts @@ -10,7 +10,7 @@ export type Params = { export type Search = string[][] | Record | string | URLSearchParams; type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute; + to: ExpandRouteOnOptionalStaticSegments; } & ParamsFromBuildFn; }; type RoutesDefinition = { @@ -27,9 +27,17 @@ type ParamsFromBuildFn = Parameters[0] extends { } : { params?: never; }; -type GenerateRoute = S extends '/' ? '/' : `/${JoinPath>>>}`; -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExplodeOptionalSegments] : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] : T; -type _ExplodeOptionalSegment = T extends `:${infer P}` ? { +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' : `/${JoinPath>>>}`; +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExpandOptionalSegments] : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T; +type _ExpandOptionalSegment = T extends `:${infer P}` ? { segment: T; } : T extends `${infer S}?` ? { optionalSegment: S; diff --git a/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/.waspchecksums b/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/.waspchecksums index 523538bdc1..c41232aa5b 100644 --- a/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/.waspchecksums +++ b/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/.waspchecksums @@ -130,7 +130,7 @@ "file", "../out/sdk/wasp/client/router/types.ts" ], - "c4aaf60fe2a920d32937aaf56d422ad77444d0afb14ed5396112896d55c974e2" + "c5b95d3525fa7d266b1d2a8f32aa0ab1e93d36a5d5bcc62c7dabc225151d4f73" ], [ [ diff --git a/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/sdk/wasp/client/router/types.ts b/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/sdk/wasp/client/router/types.ts index fb87a803a5..283e21d832 100644 --- a/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/sdk/wasp/client/router/types.ts +++ b/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/sdk/wasp/client/router/types.ts @@ -21,7 +21,7 @@ export type Search = type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute + to: ExpandRouteOnOptionalStaticSegments } & ParamsFromBuildFn } @@ -40,25 +40,32 @@ type ParamsFromBuildFn = Parameters[0] extends { ? { params: Params } : { params?: never } -// Optional static segments handling -type GenerateRoute = S extends '/' +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' - : `/${JoinPath>>>}` + : `/${JoinPath>>>}` -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' - ? [...ExplodeOptionalSegments] - : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] + ? [...ExpandOptionalSegments] + : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T -type _ExplodeOptionalSegment = T extends `:${infer P}` - // Param segment - ? { segment: T } +type _ExpandOptionalSegment = T extends `:${infer P}` + ? // Param segment + { segment: T } : T extends `${infer S}?` - // Optional segment - ? { optionalSegment: S } - // Regular segment - : { segment: T } + ? // Optional segment + { optionalSegment: S } + : // Regular segment + { segment: T } type Segment = { segment: string } type OptionalSegment = { optionalSegment: string } @@ -68,12 +75,12 @@ type Elem = Segment | OptionalSegment type JoinSegments = T extends [] ? [] : T extends [infer First extends Elem, ...infer Rest extends Elem[]] - ? First extends Segment - ? [First['segment'], ...JoinSegments] - : First extends OptionalSegment - ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + ? First extends Segment + ? [First['segment'], ...JoinSegments] + : First extends OptionalSegment + ? [First['optionalSegment'], ...JoinSegments] | JoinSegments + : [] : [] - : [] type SplitPath = S extends `${infer T}/${infer U}` ? [T, ...SplitPath] @@ -82,6 +89,5 @@ type SplitPath = S extends `${infer T}/${infer U}` type JoinPath = T extends [infer Only extends string] ? Only : T extends [infer First extends string, ...infer Rest extends string[]] - ? `${First}/${JoinPath}` - : never - \ No newline at end of file + ? `${First}/${JoinPath}` + : never diff --git a/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/sdk/wasp/dist/client/router/types.d.ts b/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/sdk/wasp/dist/client/router/types.d.ts index 1fe681a98b..8f9b8d95fd 100644 --- a/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/sdk/wasp/dist/client/router/types.d.ts +++ b/waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/sdk/wasp/dist/client/router/types.d.ts @@ -10,7 +10,7 @@ export type Params = { export type Search = string[][] | Record | string | URLSearchParams; type RouteDefinitionsToRoutesObj = { [K in keyof Routes]: { - to: GenerateRoute; + to: ExpandRouteOnOptionalStaticSegments; } & ParamsFromBuildFn; }; type RoutesDefinition = { @@ -27,9 +27,17 @@ type ParamsFromBuildFn = Parameters[0] extends { } : { params?: never; }; -type GenerateRoute = S extends '/' ? '/' : `/${JoinPath>>>}`; -type ExplodeOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExplodeOptionalSegments] : [_ExplodeOptionalSegment, ...ExplodeOptionalSegments] : T; -type _ExplodeOptionalSegment = T extends `:${infer P}` ? { +/** + * Optional static segments handling: expands routes with optional segments + * into multiple routes, one for each possible combination of optional segments. + * + * For example: /users/tasks?/:id? will be expanded into two routes: + * - /users/:id + * - /users/tasks/:id + */ +type ExpandRouteOnOptionalStaticSegments = S extends '/' ? '/' : `/${JoinPath>>>}`; +type ExpandOptionalSegments = T extends [infer Head, ...infer Tail] ? Head extends '' ? [...ExpandOptionalSegments] : [_ExpandOptionalSegment, ...ExpandOptionalSegments] : T; +type _ExpandOptionalSegment = T extends `:${infer P}` ? { segment: T; } : T extends `${infer S}?` ? { optionalSegment: S;