Skip to content

Commit ffcdaa7

Browse files
nilshartmannastahmer
authored andcommitted
zod-runtime: add typecast to ApiClient methods #12
1 parent 4215a18 commit ffcdaa7

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

.changeset/fresh-actors-kiss.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typed-openapi": minor
3+
---
4+
5+
zod-runtime: add typecast in ApiClient methods to match the desired type

packages/typed-openapi/src/generator.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ export class ApiClient {
293293
.with("zod", "yup", () => infer(`TEndpoint["response"]`))
294294
.with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["response"]`)
295295
.otherwise(() => `TEndpoint["response"]`)}> {
296-
return this.fetcher("${method}", this.baseUrl + path, params[0]);
296+
return this.fetcher("${method}", this.baseUrl + path, params[0])${match(ctx.runtime)
297+
.with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
297298
}
298299
// </ApiClient.${method}>
299300
`

packages/typed-openapi/tests/snapshots/docker.openapi.zod.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3461,7 +3461,7 @@ export class ApiClient {
34613461
path: Path,
34623462
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
34633463
): Promise<z.infer<TEndpoint["response"]>> {
3464-
return this.fetcher("get", this.baseUrl + path, params[0]);
3464+
return this.fetcher("get", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
34653465
}
34663466
// </ApiClient.get>
34673467

@@ -3470,7 +3470,7 @@ export class ApiClient {
34703470
path: Path,
34713471
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
34723472
): Promise<z.infer<TEndpoint["response"]>> {
3473-
return this.fetcher("post", this.baseUrl + path, params[0]);
3473+
return this.fetcher("post", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
34743474
}
34753475
// </ApiClient.post>
34763476

@@ -3479,7 +3479,7 @@ export class ApiClient {
34793479
path: Path,
34803480
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
34813481
): Promise<z.infer<TEndpoint["response"]>> {
3482-
return this.fetcher("delete", this.baseUrl + path, params[0]);
3482+
return this.fetcher("delete", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
34833483
}
34843484
// </ApiClient.delete>
34853485

@@ -3488,7 +3488,7 @@ export class ApiClient {
34883488
path: Path,
34893489
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
34903490
): Promise<z.infer<TEndpoint["response"]>> {
3491-
return this.fetcher("put", this.baseUrl + path, params[0]);
3491+
return this.fetcher("put", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
34923492
}
34933493
// </ApiClient.put>
34943494

@@ -3497,7 +3497,7 @@ export class ApiClient {
34973497
path: Path,
34983498
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
34993499
): Promise<z.infer<TEndpoint["response"]>> {
3500-
return this.fetcher("head", this.baseUrl + path, params[0]);
3500+
return this.fetcher("head", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
35013501
}
35023502
// </ApiClient.head>
35033503
}

packages/typed-openapi/tests/snapshots/petstore.zod.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ export class ApiClient {
389389
path: Path,
390390
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
391391
): Promise<z.infer<TEndpoint["response"]>> {
392-
return this.fetcher("put", this.baseUrl + path, params[0]);
392+
return this.fetcher("put", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
393393
}
394394
// </ApiClient.put>
395395

@@ -398,7 +398,7 @@ export class ApiClient {
398398
path: Path,
399399
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
400400
): Promise<z.infer<TEndpoint["response"]>> {
401-
return this.fetcher("post", this.baseUrl + path, params[0]);
401+
return this.fetcher("post", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
402402
}
403403
// </ApiClient.post>
404404

@@ -407,7 +407,7 @@ export class ApiClient {
407407
path: Path,
408408
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
409409
): Promise<z.infer<TEndpoint["response"]>> {
410-
return this.fetcher("get", this.baseUrl + path, params[0]);
410+
return this.fetcher("get", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
411411
}
412412
// </ApiClient.get>
413413

@@ -416,7 +416,7 @@ export class ApiClient {
416416
path: Path,
417417
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
418418
): Promise<z.infer<TEndpoint["response"]>> {
419-
return this.fetcher("delete", this.baseUrl + path, params[0]);
419+
return this.fetcher("delete", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
420420
}
421421
// </ApiClient.delete>
422422
}

0 commit comments

Comments
 (0)