forked from openapi-ts/openapi-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
244 lines (224 loc) · 6.49 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import type {
ErrorResponse,
SuccessResponse,
FilterKeys,
MediaType,
PathsWithMethod,
ResponseObjectMap,
OperationRequestBodyContent,
HasRequiredKeys,
} from "openapi-typescript-helpers";
// Note: though "any" is considered bad practice in general, this library relies
// on "any" for type inference only it can give. Same goes for the "{}" type.
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types */
/** Options for each client instance */
export interface ClientOptions extends Omit<RequestInit, "headers"> {
/** set the common root URL for all API requests */
baseUrl?: string;
/** custom fetch (defaults to globalThis.fetch) */
fetch?: typeof fetch;
/** global querySerializer */
querySerializer?: QuerySerializer<unknown>;
/** global bodySerializer */
bodySerializer?: BodySerializer<unknown>;
headers?: HeadersOptions;
}
export type HeadersOptions =
| HeadersInit
| Record<string, string | number | boolean | null | undefined>;
export type QuerySerializer<T> = (
query: T extends { parameters: any }
? NonNullable<T["parameters"]["query"]>
: Record<string, unknown>,
) => string;
export type BodySerializer<T> = (body: OperationRequestBodyContent<T>) => any;
export type ParseAs = "json" | "text" | "blob" | "arrayBuffer" | "stream";
export type ParseAsResponse<T, K extends ParseAs> = K extends "text"
? Awaited<ReturnType<Response["text"]>>
: K extends "blob"
? Awaited<ReturnType<Response["blob"]>>
: K extends "arrayBuffer"
? Awaited<ReturnType<Response["arrayBuffer"]>>
: K extends "stream"
? Response["body"]
: T;
export interface DefaultParamsOption {
params?: {
query?: Record<string, unknown>;
};
}
export type ParamsOption<T> = T extends {
parameters: any;
}
? HasRequiredKeys<T["parameters"]> extends never
? { params?: T["parameters"] }
: { params: T["parameters"] }
: DefaultParamsOption;
export type RequestBodyOption<T> = OperationRequestBodyContent<T> extends never
? { body?: never }
: undefined extends OperationRequestBodyContent<T>
? { body?: OperationRequestBodyContent<T> }
: { body: OperationRequestBodyContent<T> };
export type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, "body">;
/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
export type MaybeOptionalInit<
P extends {},
M extends keyof P,
> = HasRequiredKeys<FetchOptions<FilterKeys<P, M>>> extends never
? [(FetchOptions<FilterKeys<P, M>> | undefined)?]
: [FetchOptions<FilterKeys<P, M>>];
export type FetchResponse<T, R extends ParseAs = "json"> =
| {
data: ParseAsResponse<
FilterKeys<SuccessResponse<ResponseObjectMap<T>>, MediaType>,
R
>;
error?: never;
response: Response;
}
| {
data?: never;
error: FilterKeys<ErrorResponse<ResponseObjectMap<T>>, MediaType>;
response: Response;
};
export type RequestOptions<T> = ParamsOption<T> &
RequestBodyOption<T> & {
querySerializer?: QuerySerializer<T>;
bodySerializer?: BodySerializer<T>;
parseAs?: ParseAs;
fetch?: ClientOptions["fetch"];
};
export default function createClient<Paths extends {}>(
clientOptions?: ClientOptions,
): {
/** Call a GET endpoint */
GET<
P extends PathsWithMethod<Paths, "get">,
I extends MaybeOptionalInit<Paths[P], "get">,
>(
url: P,
...init: I
): Promise<
FetchResponse<
Paths[P]["get"],
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json"
>
>;
/** Call a PUT endpoint */
PUT<
P extends PathsWithMethod<Paths, "put">,
I extends MaybeOptionalInit<Paths[P], "put">,
>(
url: P,
...init: I
): Promise<
FetchResponse<
Paths[P]["put"],
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json"
>
>;
/** Call a POST endpoint */
POST<
P extends PathsWithMethod<Paths, "post">,
I extends MaybeOptionalInit<Paths[P], "post">,
>(
url: P,
...init: I
): Promise<
FetchResponse<
Paths[P]["post"],
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json"
>
>;
/** Call a DELETE endpoint */
DELETE<
P extends PathsWithMethod<Paths, "delete">,
I extends MaybeOptionalInit<Paths[P], "delete">,
>(
url: P,
...init: I
): Promise<
FetchResponse<
Paths[P]["delete"],
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json"
>
>;
/** Call a OPTIONS endpoint */
OPTIONS<
P extends PathsWithMethod<Paths, "options">,
I extends MaybeOptionalInit<Paths[P], "options">,
>(
url: P,
...init: I
): Promise<
FetchResponse<
Paths[P]["options"],
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json"
>
>;
/** Call a HEAD endpoint */
HEAD<
P extends PathsWithMethod<Paths, "head">,
I extends MaybeOptionalInit<Paths[P], "head">,
>(
url: P,
...init: I
): Promise<
FetchResponse<
Paths[P]["head"],
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json"
>
>;
/** Call a PATCH endpoint */
PATCH<
P extends PathsWithMethod<Paths, "patch">,
I extends MaybeOptionalInit<Paths[P], "patch">,
>(
url: P,
...init: I
): Promise<
FetchResponse<
Paths[P]["patch"],
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json"
>
>;
/** Call a TRACE endpoint */
TRACE<
P extends PathsWithMethod<Paths, "trace">,
I extends MaybeOptionalInit<Paths[P], "trace">,
>(
url: P,
...init: I
): Promise<
FetchResponse<
Paths[P]["trace"],
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json"
>
>;
};
/** Serialize query params to string */
export declare function defaultQuerySerializer<T = unknown>(q: T): string;
/** Serialize query param schema types according to expected default OpenAPI 3.x behavior */
export declare function defaultQueryParamSerializer<T = unknown>(
key: string[],
value: T,
): string | undefined;
/** Serialize body object to string */
export declare function defaultBodySerializer<T>(body: T): string;
/** Construct URL string from baseUrl and handle path and query params */
export declare function createFinalURL<O>(
pathname: string,
options: {
baseUrl: string;
params: {
query?: Record<string, unknown>;
path?: Record<string, unknown>;
};
querySerializer: QuerySerializer<O>;
},
): string;
/** Merge headers a and b, with b taking priority */
export declare function mergeHeaders(
...allHeaders: (HeadersOptions | undefined)[]
): Headers;
export {};