Skip to content

Commit 4765658

Browse files
committed
refactor(openapi-fetch): simplify createClient types
1 parent c5c4a88 commit 4765658

File tree

2 files changed

+21
-56
lines changed

2 files changed

+21
-56
lines changed

.changeset/sixty-oranges-arrive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Exports the ClientMethod utility type.

packages/openapi-fetch/src/index.d.ts

+16-56
Original file line numberDiff line numberDiff line change
@@ -104,73 +104,33 @@ export type RequestOptions<T> = ParamsOption<T> &
104104
fetch?: ClientOptions["fetch"];
105105
};
106106

107+
export type ClientMethod<Paths extends {}, M> = <
108+
P extends PathsWithMethod<Paths, M>,
109+
I extends MaybeOptionalInit<Paths[P], M>,
110+
>(
111+
url: P,
112+
...init: I
113+
) => Promise<FetchResponse<Paths[P][M], I[0]>>;
114+
107115
export default function createClient<Paths extends {}>(
108116
clientOptions?: ClientOptions,
109117
): {
110118
/** Call a GET endpoint */
111-
GET<
112-
P extends PathsWithMethod<Paths, "get">,
113-
I extends MaybeOptionalInit<Paths[P], "get">,
114-
>(
115-
url: P,
116-
...init: I
117-
): Promise<FetchResponse<Paths[P]["get"], I[0]>>;
119+
GET: ClientMethod<Paths, "get">;
118120
/** Call a PUT endpoint */
119-
PUT<
120-
P extends PathsWithMethod<Paths, "put">,
121-
I extends MaybeOptionalInit<Paths[P], "put">,
122-
>(
123-
url: P,
124-
...init: I
125-
): Promise<FetchResponse<Paths[P]["put"], I[0]>>;
121+
PUT: ClientMethod<Paths, "put">;
126122
/** Call a POST endpoint */
127-
POST<
128-
P extends PathsWithMethod<Paths, "post">,
129-
I extends MaybeOptionalInit<Paths[P], "post">,
130-
>(
131-
url: P,
132-
...init: I
133-
): Promise<FetchResponse<Paths[P]["post"], I[0]>>;
123+
POST: ClientMethod<Paths, "post">;
134124
/** Call a DELETE endpoint */
135-
DELETE<
136-
P extends PathsWithMethod<Paths, "delete">,
137-
I extends MaybeOptionalInit<Paths[P], "delete">,
138-
>(
139-
url: P,
140-
...init: I
141-
): Promise<FetchResponse<Paths[P]["delete"], I[0]>>;
125+
DELETE: ClientMethod<Paths, "delete">;
142126
/** Call a OPTIONS endpoint */
143-
OPTIONS<
144-
P extends PathsWithMethod<Paths, "options">,
145-
I extends MaybeOptionalInit<Paths[P], "options">,
146-
>(
147-
url: P,
148-
...init: I
149-
): Promise<FetchResponse<Paths[P]["options"], I[0]>>;
127+
OPTIONS: ClientMethod<Paths, "options">;
150128
/** Call a HEAD endpoint */
151-
HEAD<
152-
P extends PathsWithMethod<Paths, "head">,
153-
I extends MaybeOptionalInit<Paths[P], "head">,
154-
>(
155-
url: P,
156-
...init: I
157-
): Promise<FetchResponse<Paths[P]["head"], I[0]>>;
129+
HEAD: ClientMethod<Paths, "head">;
158130
/** Call a PATCH endpoint */
159-
PATCH<
160-
P extends PathsWithMethod<Paths, "patch">,
161-
I extends MaybeOptionalInit<Paths[P], "patch">,
162-
>(
163-
url: P,
164-
...init: I
165-
): Promise<FetchResponse<Paths[P]["patch"], I[0]>>;
131+
PATCH: ClientMethod<Paths, "patch">;
166132
/** Call a TRACE endpoint */
167-
TRACE<
168-
P extends PathsWithMethod<Paths, "trace">,
169-
I extends MaybeOptionalInit<Paths[P], "trace">,
170-
>(
171-
url: P,
172-
...init: I
173-
): Promise<FetchResponse<Paths[P]["trace"], I[0]>>;
133+
TRACE: ClientMethod<Paths, "trace">;
174134
};
175135

176136
/** Serialize query params to string */

0 commit comments

Comments
 (0)