Skip to content

Commit b23897d

Browse files
committed
feat(typescript-fetch): simplify response types
- avoid `Promise<Promise<T>>` / webstorm wanting to double `await` after latest updates - reduce unnecessary noise in type hints, particularly in-editor tooltips, etc
1 parent b35edf9 commit b23897d

File tree

5 files changed

+12
-17
lines changed

5 files changed

+12
-17
lines changed

packages/documentation/src/pages/guides/concepts/servers-object-handling.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class ApiClient {
9595
.build(),
9696
timeout?: number,
9797
opts: RequestInit = {},
98-
): Promise<TypedFetchResponse<Res<202, void>>> {
98+
): Promise<Res<202, void>> {
9999
...
100100
}
101101
}

packages/openapi-code-generator/src/typescript/typescript-fetch/typescript-fetch-client-builder.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export class TypescriptFetchClientBuilder extends TypescriptClientBuilder {
1515
"AbstractFetchClient",
1616
"Server",
1717
"Res",
18-
"TypedFetchResponse",
1918
"StatusCode2xx",
2019
"StatusCode3xx",
2120
"StatusCode4xx",
@@ -112,7 +111,7 @@ export class TypescriptFetchClientBuilder extends TypescriptClientBuilder {
112111
default: "{}",
113112
},
114113
],
115-
returnType: `TypedFetchResponse<${returnType}>`,
114+
returnType,
116115
body,
117116
})
118117
}

packages/typescript-fetch-runtime/src/joi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {Schema as JoiSchema} from "@hapi/joi"
2-
import type {Res, StatusCode, TypedFetchResponse} from "./main"
2+
import type {Res, StatusCode} from "./main"
33

44
export function responseValidationFactory(
55
possibleResponses: [string, JoiSchema][],
@@ -10,9 +10,9 @@ export function responseValidationFactory(
1010

1111
// TODO: avoid any
1212
return async (
13-
whenRes: TypedFetchResponse<Res<StatusCode, unknown>>,
13+
whenRes: Promise<Res<StatusCode, unknown>>,
1414
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
15-
): Promise<TypedFetchResponse<any>> => {
15+
): Promise<any> => {
1616
const res = await whenRes
1717

1818
return {

packages/typescript-fetch-runtime/src/main.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@ export type StatusCode =
2626
| StatusCode4xx
2727
| StatusCode5xx
2828

29-
export type Res<Status extends StatusCode, Type> = {
29+
export interface Res<Status extends StatusCode, JsonBody> extends Response {
3030
status: Status
31-
json: () => Promise<Type>
31+
json: () => Promise<JsonBody>
3232
}
3333

34-
export type TypedFetchResponse<R extends Res<StatusCode, unknown>> = Promise<
35-
Omit<Response, "json" | "status"> & R
36-
>
37-
3834
export type Server<T> = string & {__server__: T}
3935

4036
export interface AbstractFetchClientConfig {
@@ -83,7 +79,7 @@ export abstract class AbstractFetchClient {
8379
url: string,
8480
opts: RequestInit,
8581
timeout: number | undefined = this.defaultTimeout,
86-
): TypedFetchResponse<R> {
82+
): Promise<R> {
8783
// main abort controller that will be returned to the caller
8884
const cancelRequest = new AbortController()
8985

@@ -118,7 +114,7 @@ export abstract class AbstractFetchClient {
118114
}
119115
// if not aborted just throw
120116
throw err
121-
}) as TypedFetchResponse<R>
117+
}) as unknown as R
122118
}
123119

124120
protected _query(params: QueryParams): string {

packages/typescript-fetch-runtime/src/zod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {z} from "zod"
2-
import type {Res, StatusCode, TypedFetchResponse} from "./main"
2+
import type {Res, StatusCode} from "./main"
33

44
export function responseValidationFactory(
55
possibleResponses: [string, z.ZodTypeAny][],
@@ -10,9 +10,9 @@ export function responseValidationFactory(
1010

1111
// TODO: avoid any
1212
return async (
13-
whenRes: TypedFetchResponse<Res<StatusCode, unknown>>,
13+
whenRes: Promise<Res<StatusCode, unknown>>,
1414
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
15-
): Promise<TypedFetchResponse<any>> => {
15+
): Promise<any> => {
1616
const res = await whenRes
1717

1818
return {

0 commit comments

Comments
 (0)