Skip to content

Commit

Permalink
zod-runtime: add typecast to ApiClient methods #12
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshartmann authored and astahmer committed Dec 22, 2023
1 parent 4215a18 commit ffcdaa7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-actors-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"typed-openapi": minor
---

zod-runtime: add typecast in ApiClient methods to match the desired type
3 changes: 2 additions & 1 deletion packages/typed-openapi/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ export class ApiClient {
.with("zod", "yup", () => infer(`TEndpoint["response"]`))
.with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["response"]`)
.otherwise(() => `TEndpoint["response"]`)}> {
return this.fetcher("${method}", this.baseUrl + path, params[0]);
return this.fetcher("${method}", this.baseUrl + path, params[0])${match(ctx.runtime)
.with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
}
// </ApiClient.${method}>
`
Expand Down
10 changes: 5 additions & 5 deletions packages/typed-openapi/tests/snapshots/docker.openapi.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3461,7 +3461,7 @@ export class ApiClient {
path: Path,
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
): Promise<z.infer<TEndpoint["response"]>> {
return this.fetcher("get", this.baseUrl + path, params[0]);
return this.fetcher("get", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
}
// </ApiClient.get>

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

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

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

Expand All @@ -3497,7 +3497,7 @@ export class ApiClient {
path: Path,
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
): Promise<z.infer<TEndpoint["response"]>> {
return this.fetcher("head", this.baseUrl + path, params[0]);
return this.fetcher("head", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
}
// </ApiClient.head>
}
Expand Down
8 changes: 4 additions & 4 deletions packages/typed-openapi/tests/snapshots/petstore.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export class ApiClient {
path: Path,
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
): Promise<z.infer<TEndpoint["response"]>> {
return this.fetcher("put", this.baseUrl + path, params[0]);
return this.fetcher("put", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
}
// </ApiClient.put>

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

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

Expand All @@ -416,7 +416,7 @@ export class ApiClient {
path: Path,
...params: MaybeOptionalArg<z.infer<TEndpoint["parameters"]>>
): Promise<z.infer<TEndpoint["response"]>> {
return this.fetcher("delete", this.baseUrl + path, params[0]);
return this.fetcher("delete", this.baseUrl + path, params[0]) as Promise<TEndpoint["response"]>;
}
// </ApiClient.delete>
}
Expand Down

0 comments on commit ffcdaa7

Please sign in to comment.