Skip to content

Commit 9ad53dd

Browse files
committed
fix: fix bad format/partialFormat TS performance
BREAKING CHANGE: removed BindParams type and changed format/partialFormat return types to string.
1 parent 43917c6 commit 9ad53dd

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

src/index.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ type Parts<Path extends string> = Path extends `${infer Head}/${infer Tail}`
44
? [Head, ...Parts<Tail>]
55
: [Path]
66

7-
type BindParams<
8-
Path extends string,
9-
Params extends Record<string, any>
10-
> = Path extends `${infer Head}/${infer Tail}`
11-
? `${BindParam<Head, Params>}/${BindParams<Tail, Params>}`
12-
: BindParam<Path, Params>
13-
14-
type BindParam<
15-
Elem extends string,
16-
Params extends Record<string, any>
17-
> = Elem extends `:${infer Param}`
18-
? Param extends keyof Params
19-
? Params[Param]
20-
: Elem
21-
: Elem
22-
237
type RawParams<Path extends string> = {
248
[K in Parts<Path>[number] as K extends `:${string}?`
259
? never
@@ -179,7 +163,7 @@ export default class ZodRoute<
179163
return result.data
180164
}
181165

182-
format(params: z.output<Schema>): BindParams<Pattern, z.output<Schema>> {
166+
format(params: z.output<Schema>): string {
183167
const rawParams: any = this.formatSchema.parse(params)
184168
return this.parts
185169
.flatMap((p) => {
@@ -193,9 +177,7 @@ export default class ZodRoute<
193177
.join('/') as any
194178
}
195179

196-
partialFormat<P extends Partial<z.output<Schema>>>(
197-
params: P
198-
): BindParams<Pattern, P> {
180+
partialFormat<P extends Partial<z.output<Schema>>>(params: P): string {
199181
const rawParams: any = this.partialFormatSchema.parse(params)
200182
return this.parts
201183
.flatMap((p) => {
@@ -232,9 +214,9 @@ export default class ZodRoute<
232214
} = {}
233215
): ZodRoute<
234216
`${Pattern}/${Subpattern}`,
235-
// @ts-expect-error probably impossbile to get this type to work
236217
z.ZodIntersection<Schema, Subschema>,
237218
z.ZodIntersection<FormatSchema, FormatSubschema>,
219+
// @ts-expect-error probably impossbile to get this type to work
238220
z.ZodIntersection<PartialFormatSchema, PartialFormatSubschema>
239221
> {
240222
return new ZodRoute<any, any, any>(

0 commit comments

Comments
 (0)