@shipworthy/openapi-fetch 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +575 -2
- package/dist/index.cjs +538 -0
- package/dist/index.d.cts +450 -0
- package/dist/index.d.ts +450 -0
- package/dist/index.js +522 -0
- package/package.json +55 -4
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
//#region src/helpers.d.ts
|
|
2
|
+
type HttpMethod = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace";
|
|
3
|
+
/** 2XX statuses */
|
|
4
|
+
type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207 | "2XX";
|
|
5
|
+
/** 4XX and 5XX statuses */
|
|
6
|
+
type ErrorStatus = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | "5XX" | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 444 | 450 | 451 | 497 | 498 | 499 | "4XX" | "default";
|
|
7
|
+
/** Get a union of OK Statuses */
|
|
8
|
+
type OKStatusUnion<T> = FilterKeys<T, OkStatus>;
|
|
9
|
+
/** Get first error status, in order */
|
|
10
|
+
type FirstErrorStatus<T> = T extends {
|
|
11
|
+
500: any;
|
|
12
|
+
} ? T[500] : T extends {
|
|
13
|
+
501: any;
|
|
14
|
+
} ? T[501] : T extends {
|
|
15
|
+
502: any;
|
|
16
|
+
} ? T[502] : T extends {
|
|
17
|
+
503: any;
|
|
18
|
+
} ? T[503] : T extends {
|
|
19
|
+
504: any;
|
|
20
|
+
} ? T[504] : T extends {
|
|
21
|
+
505: any;
|
|
22
|
+
} ? T[505] : T extends {
|
|
23
|
+
506: any;
|
|
24
|
+
} ? T[506] : T extends {
|
|
25
|
+
507: any;
|
|
26
|
+
} ? T[507] : T extends {
|
|
27
|
+
508: any;
|
|
28
|
+
} ? T[508] : T extends {
|
|
29
|
+
510: any;
|
|
30
|
+
} ? T[510] : T extends {
|
|
31
|
+
511: any;
|
|
32
|
+
} ? T[511] : T extends {
|
|
33
|
+
"5XX": any;
|
|
34
|
+
} ? T["5XX"] : T extends {
|
|
35
|
+
400: any;
|
|
36
|
+
} ? T[400] : T extends {
|
|
37
|
+
401: any;
|
|
38
|
+
} ? T[401] : T extends {
|
|
39
|
+
402: any;
|
|
40
|
+
} ? T[402] : T extends {
|
|
41
|
+
403: any;
|
|
42
|
+
} ? T[403] : T extends {
|
|
43
|
+
404: any;
|
|
44
|
+
} ? T[404] : T extends {
|
|
45
|
+
405: any;
|
|
46
|
+
} ? T[405] : T extends {
|
|
47
|
+
406: any;
|
|
48
|
+
} ? T[406] : T extends {
|
|
49
|
+
407: any;
|
|
50
|
+
} ? T[407] : T extends {
|
|
51
|
+
408: any;
|
|
52
|
+
} ? T[408] : T extends {
|
|
53
|
+
409: any;
|
|
54
|
+
} ? T[409] : T extends {
|
|
55
|
+
410: any;
|
|
56
|
+
} ? T[410] : T extends {
|
|
57
|
+
411: any;
|
|
58
|
+
} ? T[411] : T extends {
|
|
59
|
+
412: any;
|
|
60
|
+
} ? T[412] : T extends {
|
|
61
|
+
413: any;
|
|
62
|
+
} ? T[413] : T extends {
|
|
63
|
+
414: any;
|
|
64
|
+
} ? T[414] : T extends {
|
|
65
|
+
415: any;
|
|
66
|
+
} ? T[415] : T extends {
|
|
67
|
+
416: any;
|
|
68
|
+
} ? T[416] : T extends {
|
|
69
|
+
417: any;
|
|
70
|
+
} ? T[417] : T extends {
|
|
71
|
+
418: any;
|
|
72
|
+
} ? T[418] : T extends {
|
|
73
|
+
420: any;
|
|
74
|
+
} ? T[420] : T extends {
|
|
75
|
+
421: any;
|
|
76
|
+
} ? T[421] : T extends {
|
|
77
|
+
422: any;
|
|
78
|
+
} ? T[422] : T extends {
|
|
79
|
+
423: any;
|
|
80
|
+
} ? T[423] : T extends {
|
|
81
|
+
424: any;
|
|
82
|
+
} ? T[424] : T extends {
|
|
83
|
+
425: any;
|
|
84
|
+
} ? T[425] : T extends {
|
|
85
|
+
426: any;
|
|
86
|
+
} ? T[426] : T extends {
|
|
87
|
+
427: any;
|
|
88
|
+
} ? T[427] : T extends {
|
|
89
|
+
428: any;
|
|
90
|
+
} ? T[428] : T extends {
|
|
91
|
+
429: any;
|
|
92
|
+
} ? T[429] : T extends {
|
|
93
|
+
430: any;
|
|
94
|
+
} ? T[430] : T extends {
|
|
95
|
+
431: any;
|
|
96
|
+
} ? T[431] : T extends {
|
|
97
|
+
444: any;
|
|
98
|
+
} ? T[444] : T extends {
|
|
99
|
+
450: any;
|
|
100
|
+
} ? T[450] : T extends {
|
|
101
|
+
451: any;
|
|
102
|
+
} ? T[451] : T extends {
|
|
103
|
+
497: any;
|
|
104
|
+
} ? T[497] : T extends {
|
|
105
|
+
498: any;
|
|
106
|
+
} ? T[498] : T extends {
|
|
107
|
+
499: any;
|
|
108
|
+
} ? T[499] : T extends {
|
|
109
|
+
"4XX": any;
|
|
110
|
+
} ? T["4XX"] : T extends {
|
|
111
|
+
default: any;
|
|
112
|
+
} ? T["default"] : never;
|
|
113
|
+
/** Given an OpenAPI **Paths Object**, find all paths that have the given method */
|
|
114
|
+
type PathsWithMethod<Paths extends {}, PathnameMethod extends HttpMethod> = { [Pathname in keyof Paths]: Paths[Pathname] extends { [K in PathnameMethod]: any; } ? Pathname : never; }[keyof Paths];
|
|
115
|
+
/**
|
|
116
|
+
* DO NOT USE!
|
|
117
|
+
* Only used only for OperationObject type inference
|
|
118
|
+
*/
|
|
119
|
+
interface OperationObject {
|
|
120
|
+
parameters: any;
|
|
121
|
+
requestBody: any;
|
|
122
|
+
responses: any;
|
|
123
|
+
}
|
|
124
|
+
/** Internal helper used in PathsWithMethod */
|
|
125
|
+
type PathItemObject = { [M in HttpMethod]: OperationObject; } & {
|
|
126
|
+
parameters?: any;
|
|
127
|
+
};
|
|
128
|
+
/** Return `responses` for an Operation Object */
|
|
129
|
+
type ResponseObjectMap<T> = T extends {
|
|
130
|
+
responses: any;
|
|
131
|
+
} ? T["responses"] : unknown;
|
|
132
|
+
/** `ResponseObjectMap` narrowed to the Response Object shape. TypeScript 5.0 requires the narrowing. */
|
|
133
|
+
type ResponseMap<T> = Extract<ResponseObjectMap<T>, Record<string | number, any>>;
|
|
134
|
+
/** Return `content` for a Response Object */
|
|
135
|
+
type ResponseContent<T> = T extends {
|
|
136
|
+
content: any;
|
|
137
|
+
} ? T["content"] : unknown;
|
|
138
|
+
/** Return type of `requestBody` for an Operation Object */
|
|
139
|
+
type OperationRequestBody<T> = "requestBody" extends keyof T ? T["requestBody"] : never;
|
|
140
|
+
/** Internal helper to get object type with only the `requestBody` property */
|
|
141
|
+
type PickRequestBody<T> = "requestBody" extends keyof T ? Pick<T, "requestBody"> : never;
|
|
142
|
+
/** Resolve to `true` if request body is optional, else `false` */
|
|
143
|
+
type IsOperationRequestBodyOptional<T> = RequiredKeysOf<PickRequestBody<T>> extends never ? true : false;
|
|
144
|
+
/** Internal helper used in OperationRequestBodyContent */
|
|
145
|
+
type OperationRequestBodyMediaContent<T> = IsOperationRequestBodyOptional<T> extends true ? ResponseContent<NonNullable<OperationRequestBody<T>>> | undefined : ResponseContent<OperationRequestBody<T>>;
|
|
146
|
+
/** Return first `content` from a Request Object Mapping, allowing any media type */
|
|
147
|
+
type OperationRequestBodyContent<T> = FilterKeys<OperationRequestBodyMediaContent<T>, MediaType> extends never ? FilterKeys<NonNullable<OperationRequestBodyMediaContent<T>>, MediaType> | undefined : FilterKeys<OperationRequestBodyMediaContent<T>, MediaType>;
|
|
148
|
+
/** Return all 2XX responses from a Response Object Map */
|
|
149
|
+
type SuccessResponse<T extends Record<string | number, any>, Media extends MediaType = MediaType> = GetResponseContent<T, Media, OkStatus>;
|
|
150
|
+
type GetResponseContent<T extends Record<string | number, any>, Media extends MediaType = MediaType, ResponseCode extends keyof T = keyof T> = ResponseCode extends keyof T ? { [K in ResponseCode]: T[K]["content"] extends Record<string, any> ? FilterKeys<T[K]["content"], Media> extends never ? T[K]["content"] : FilterKeys<T[K]["content"], Media> : K extends keyof T ? T[K]["content"] : never; }[ResponseCode] : never;
|
|
151
|
+
/**
|
|
152
|
+
* Return all 5XX and 4XX responses (in that order) from a Response Object Map
|
|
153
|
+
*/
|
|
154
|
+
type ErrorResponse<T extends Record<string | number, any>, Media extends MediaType = MediaType> = GetResponseContent<T, Media, ErrorStatus>;
|
|
155
|
+
/** Return first JSON-like 2XX response from a path + HTTP method */
|
|
156
|
+
type SuccessResponseJSON<PathMethod extends Record<string | number, any>> = SuccessResponse<ResponseMap<PathMethod>, `${string}/json`>;
|
|
157
|
+
/** Return first JSON-like 5XX or 4XX response from a path + HTTP method */
|
|
158
|
+
type ErrorResponseJSON<PathMethod extends Record<string | number, any>> = ErrorResponse<ResponseMap<PathMethod>, `${string}/json`>;
|
|
159
|
+
/** Return JSON-like request body from a path + HTTP method */
|
|
160
|
+
type RequestBodyJSON<PathMethod> = JSONLike<FilterKeys<OperationRequestBody<PathMethod>, "content">>;
|
|
161
|
+
/** Find first match of multiple keys */
|
|
162
|
+
type FilterKeys<Obj, Matchers> = Obj[keyof Obj & Matchers];
|
|
163
|
+
/** Return any `[string]/[string]` media type (important because openapi-fetch allows any content response, not just JSON-like) */
|
|
164
|
+
type MediaType = `${string}/${string}`;
|
|
165
|
+
/** Return any media type containing "json" (works for "application/json", "application/vnd.api+json", "application/vnd.oai.openapi+json") */
|
|
166
|
+
type JSONLike<T> = FilterKeys<T, `${string}/json`>;
|
|
167
|
+
/** Helper to get the required keys of an object. If no keys are required, will be `undefined` with strictNullChecks enabled, else `never` */
|
|
168
|
+
type RequiredKeysOfHelper<T> = { [K in keyof T]: {} extends Pick<T, K> ? never : K; }[keyof T];
|
|
169
|
+
/** Get the required keys of an object, or `never` if no keys are required */
|
|
170
|
+
type RequiredKeysOf<T> = RequiredKeysOfHelper<T> extends undefined ? never : RequiredKeysOfHelper<T>;
|
|
171
|
+
/** Marker type for readOnly properties (excluded from request bodies) */
|
|
172
|
+
type $Read<T> = {
|
|
173
|
+
readonly $read: T;
|
|
174
|
+
};
|
|
175
|
+
/** Marker type for writeOnly properties (excluded from response bodies) */
|
|
176
|
+
type $Write<T> = {
|
|
177
|
+
readonly $write: T;
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Resolve type for reading (responses): strips $Write properties, unwraps $Read
|
|
181
|
+
* - $Read<T> → T (readable), continues recursion
|
|
182
|
+
* - $Write<T> → never (excluded from response)
|
|
183
|
+
* - object → recursively resolve
|
|
184
|
+
*/
|
|
185
|
+
type Readable<T> = T extends $Write<any> ? never : T extends $Read<infer U> ? Readable<U> : T extends (infer E)[] ? Readable<E>[] : T extends object ? { [K in keyof T as NonNullable<T[K]> extends $Write<any> ? never : K]: Readable<T[K]>; } : T;
|
|
186
|
+
/**
|
|
187
|
+
* Resolve type for writing (requests): strips $Read properties, unwraps $Write
|
|
188
|
+
* - $Write<T> → T (writable), continues recursion
|
|
189
|
+
* - $Read<T> → never (excluded from request)
|
|
190
|
+
* - object → recursively resolve
|
|
191
|
+
*/
|
|
192
|
+
type Writable<T> = T extends $Read<any> ? never : T extends $Write<infer U> ? Writable<U> : T extends (infer E)[] ? Writable<E>[] : T extends object ? { [K in keyof T as NonNullable<T[K]> extends $Read<any> ? never : K]: Writable<T[K]>; } & { [K in keyof T as NonNullable<T[K]> extends $Read<any> ? K : never]?: never; } : T;
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/types.d.ts
|
|
195
|
+
/** Options for each client instance */
|
|
196
|
+
interface ClientOptions extends Omit<RequestInit, "headers"> {
|
|
197
|
+
/** set the common root URL for all API requests */
|
|
198
|
+
baseUrl?: string;
|
|
199
|
+
/** custom fetch (defaults to globalThis.fetch) */
|
|
200
|
+
fetch?: (input: Request) => Promise<Response>;
|
|
201
|
+
/** custom Request (defaults to globalThis.Request) */
|
|
202
|
+
Request?: typeof Request;
|
|
203
|
+
/** global querySerializer */
|
|
204
|
+
querySerializer?: QuerySerializer<unknown> | QuerySerializerOptions;
|
|
205
|
+
/** global bodySerializer */
|
|
206
|
+
bodySerializer?: BodySerializer<unknown>;
|
|
207
|
+
/** global pathSerializer */
|
|
208
|
+
pathSerializer?: PathSerializer;
|
|
209
|
+
headers?: HeadersOptions;
|
|
210
|
+
/** RequestInit extension object to pass as 2nd argument to fetch when supported (defaults to undefined) */
|
|
211
|
+
requestInitExt?: Record<string, unknown>;
|
|
212
|
+
}
|
|
213
|
+
type HeadersOptions = Required<RequestInit>["headers"] | Record<string, string | number | boolean | (string | number | boolean)[] | null | undefined>;
|
|
214
|
+
type QuerySerializer<T> = (query: T extends {
|
|
215
|
+
parameters: any;
|
|
216
|
+
} ? NonNullable<T["parameters"]["query"]> : Record<string, unknown>) => string;
|
|
217
|
+
/** @see https://swagger.io/docs/specification/serialization/#query */
|
|
218
|
+
type QuerySerializerOptions = {
|
|
219
|
+
/** Set serialization for arrays. @see https://swagger.io/docs/specification/serialization/#query */
|
|
220
|
+
array?: {
|
|
221
|
+
/** default: "form" */
|
|
222
|
+
style: "form" | "spaceDelimited" | "pipeDelimited";
|
|
223
|
+
/** default: true */
|
|
224
|
+
explode: boolean;
|
|
225
|
+
};
|
|
226
|
+
/** Set serialization for objects. @see https://swagger.io/docs/specification/serialization/#query */
|
|
227
|
+
object?: {
|
|
228
|
+
/** default: "deepObject" */
|
|
229
|
+
style: "form" | "deepObject";
|
|
230
|
+
/** default: true */
|
|
231
|
+
explode: boolean;
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* The `allowReserved` keyword specifies whether the reserved characters
|
|
235
|
+
* `:/?#[]@!$&'()*+,;=` in parameter values are allowed to be sent as they
|
|
236
|
+
* are, or should be percent-encoded. By default, allowReserved is `false`,
|
|
237
|
+
* and reserved characters are percent-encoded.
|
|
238
|
+
* @see https://swagger.io/docs/specification/serialization/#query
|
|
239
|
+
*/
|
|
240
|
+
allowReserved?: boolean;
|
|
241
|
+
};
|
|
242
|
+
type BodySerializer<T> = (body: OperationRequestBodyContent<T>, headers?: Headers) => any;
|
|
243
|
+
type PathSerializer = (pathname: string, pathParams: Record<string, unknown>) => string;
|
|
244
|
+
type BodyType<T = unknown> = {
|
|
245
|
+
json: T;
|
|
246
|
+
text: Awaited<ReturnType<Response["text"]>>;
|
|
247
|
+
blob: Awaited<ReturnType<Response["blob"]>>;
|
|
248
|
+
arrayBuffer: Awaited<ReturnType<Response["arrayBuffer"]>>;
|
|
249
|
+
stream: Response["body"];
|
|
250
|
+
};
|
|
251
|
+
type ParseAs = keyof BodyType;
|
|
252
|
+
type ParseAsResponse<T, Options> = Options extends {
|
|
253
|
+
parseAs: ParseAs;
|
|
254
|
+
} ? BodyType<T>[Options["parseAs"]] : T;
|
|
255
|
+
interface DefaultParamsOption {
|
|
256
|
+
params?: {
|
|
257
|
+
query?: Record<string, unknown>;
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
type ParamsOption<T> = T extends {
|
|
261
|
+
parameters: any;
|
|
262
|
+
} ? RequiredKeysOf<T["parameters"]> extends never ? {
|
|
263
|
+
params?: T["parameters"];
|
|
264
|
+
} : {
|
|
265
|
+
params: T["parameters"];
|
|
266
|
+
} : DefaultParamsOption;
|
|
267
|
+
type RequestBodyOption<T> = Writable<OperationRequestBodyContent<T>> extends never ? {
|
|
268
|
+
body?: never;
|
|
269
|
+
} : IsOperationRequestBodyOptional<T> extends true ? {
|
|
270
|
+
body?: Writable<OperationRequestBodyContent<T>>;
|
|
271
|
+
} : {
|
|
272
|
+
body: Writable<OperationRequestBodyContent<T>>;
|
|
273
|
+
};
|
|
274
|
+
type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, "body" | "headers">;
|
|
275
|
+
type FetchResponse<T extends Record<string | number, any>, Options, Media extends MediaType> = {
|
|
276
|
+
data: ParseAsResponse<Readable<SuccessResponse<ResponseMap<T>, Media>>, Options>;
|
|
277
|
+
error?: never;
|
|
278
|
+
response: Response;
|
|
279
|
+
} | {
|
|
280
|
+
data?: never;
|
|
281
|
+
error: Readable<ErrorResponse<ResponseMap<T>, Media>>;
|
|
282
|
+
response: Response;
|
|
283
|
+
};
|
|
284
|
+
type RequestOptions<T> = ParamsOption<T> & RequestBodyOption<T> & {
|
|
285
|
+
baseUrl?: string;
|
|
286
|
+
querySerializer?: QuerySerializer<T> | QuerySerializerOptions;
|
|
287
|
+
bodySerializer?: BodySerializer<T>;
|
|
288
|
+
pathSerializer?: PathSerializer;
|
|
289
|
+
parseAs?: ParseAs;
|
|
290
|
+
fetch?: ClientOptions["fetch"];
|
|
291
|
+
headers?: HeadersOptions;
|
|
292
|
+
middleware?: Middleware[];
|
|
293
|
+
};
|
|
294
|
+
type MergedOptions<T = unknown> = {
|
|
295
|
+
baseUrl: string;
|
|
296
|
+
parseAs: ParseAs;
|
|
297
|
+
querySerializer: QuerySerializer<T>;
|
|
298
|
+
bodySerializer: BodySerializer<T>;
|
|
299
|
+
pathSerializer: PathSerializer;
|
|
300
|
+
fetch: typeof globalThis.fetch;
|
|
301
|
+
};
|
|
302
|
+
interface MiddlewareRequestParams {
|
|
303
|
+
query?: Record<string, unknown>;
|
|
304
|
+
header?: Record<string, unknown>;
|
|
305
|
+
path?: Record<string, unknown>;
|
|
306
|
+
cookie?: Record<string, unknown>;
|
|
307
|
+
}
|
|
308
|
+
interface MiddlewareCallbackParams {
|
|
309
|
+
/** Current Request object */
|
|
310
|
+
request: Request;
|
|
311
|
+
/** The original OpenAPI schema path (including curly braces) */
|
|
312
|
+
readonly schemaPath: string;
|
|
313
|
+
/** OpenAPI parameters as provided from openapi-fetch */
|
|
314
|
+
readonly params: MiddlewareRequestParams;
|
|
315
|
+
/** Unique ID for this request */
|
|
316
|
+
readonly id: string;
|
|
317
|
+
/** createClient options (read-only) */
|
|
318
|
+
readonly options: MergedOptions;
|
|
319
|
+
}
|
|
320
|
+
type MiddlewareOnRequest = (options: MiddlewareCallbackParams) => void | Request | Response | undefined | Promise<Request | Response | undefined | void>;
|
|
321
|
+
type MiddlewareOnResponse = (options: MiddlewareCallbackParams & {
|
|
322
|
+
response: Response;
|
|
323
|
+
}) => void | Response | undefined | Promise<Response | undefined | void>;
|
|
324
|
+
type MiddlewareOnError = (options: MiddlewareCallbackParams & {
|
|
325
|
+
error: unknown;
|
|
326
|
+
}) => void | Response | Error | Promise<void | Response | Error>;
|
|
327
|
+
type Middleware = {
|
|
328
|
+
onRequest: MiddlewareOnRequest;
|
|
329
|
+
onResponse?: MiddlewareOnResponse;
|
|
330
|
+
onError?: MiddlewareOnError;
|
|
331
|
+
} | {
|
|
332
|
+
onRequest?: MiddlewareOnRequest;
|
|
333
|
+
onResponse: MiddlewareOnResponse;
|
|
334
|
+
onError?: MiddlewareOnError;
|
|
335
|
+
} | {
|
|
336
|
+
onRequest?: MiddlewareOnRequest;
|
|
337
|
+
onResponse?: MiddlewareOnResponse;
|
|
338
|
+
onError: MiddlewareOnError;
|
|
339
|
+
};
|
|
340
|
+
/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
|
|
341
|
+
type MaybeOptionalInit<Params, Location extends keyof Params> = RequiredKeysOf<FetchOptions<FilterKeys<Params, Location>>> extends never ? FetchOptions<FilterKeys<Params, Location>> | undefined : FetchOptions<FilterKeys<Params, Location>>;
|
|
342
|
+
type InitParam<Init> = RequiredKeysOf<Init> extends never ? [(Init & {
|
|
343
|
+
[key: string]: unknown;
|
|
344
|
+
})?] : [Init & {
|
|
345
|
+
[key: string]: unknown;
|
|
346
|
+
}];
|
|
347
|
+
type ClientMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Method extends HttpMethod, Media extends MediaType> = <Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>>(url: Path, ...init: InitParam<Init>) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>;
|
|
348
|
+
type ClientRequestMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>>(method: Method, url: Path, ...init: InitParam<Init>) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>;
|
|
349
|
+
type ClientForPath<PathInfo extends Record<string | number, any>, Media extends MediaType> = { [Method in keyof PathInfo as Uppercase<string & Method>]: <Init extends MaybeOptionalInit<PathInfo, Method>>(...init: InitParam<Init>) => Promise<FetchResponse<PathInfo[Method], Init, Media>>; };
|
|
350
|
+
interface Client<Paths extends {}, Media extends MediaType = MediaType> {
|
|
351
|
+
request: ClientRequestMethod<Paths, Media>;
|
|
352
|
+
/** Call a GET endpoint */
|
|
353
|
+
GET: ClientMethod<Paths, "get", Media>;
|
|
354
|
+
/** Call a PUT endpoint */
|
|
355
|
+
PUT: ClientMethod<Paths, "put", Media>;
|
|
356
|
+
/** Call a POST endpoint */
|
|
357
|
+
POST: ClientMethod<Paths, "post", Media>;
|
|
358
|
+
/** Call a DELETE endpoint */
|
|
359
|
+
DELETE: ClientMethod<Paths, "delete", Media>;
|
|
360
|
+
/** Call a OPTIONS endpoint */
|
|
361
|
+
OPTIONS: ClientMethod<Paths, "options", Media>;
|
|
362
|
+
/** Call a HEAD endpoint */
|
|
363
|
+
HEAD: ClientMethod<Paths, "head", Media>;
|
|
364
|
+
/** Call a PATCH endpoint */
|
|
365
|
+
PATCH: ClientMethod<Paths, "patch", Media>;
|
|
366
|
+
/** Call a TRACE endpoint */
|
|
367
|
+
TRACE: ClientMethod<Paths, "trace", Media>;
|
|
368
|
+
/** Register middleware */
|
|
369
|
+
use(...middleware: Middleware[]): void;
|
|
370
|
+
/** Unregister middleware */
|
|
371
|
+
eject(...middleware: Middleware[]): void;
|
|
372
|
+
}
|
|
373
|
+
type ClientPathsWithMethod<CreatedClient extends Client<any, any>, Method extends HttpMethod> = CreatedClient extends Client<infer Paths, infer _Media> ? PathsWithMethod<Paths, Method> : never;
|
|
374
|
+
type MethodResponse<CreatedClient extends Client<any, any>, Method extends HttpMethod, Path extends ClientPathsWithMethod<CreatedClient, Method>, Options = {}> = CreatedClient extends Client<infer Paths extends {
|
|
375
|
+
[key: string]: any;
|
|
376
|
+
}, infer Media extends MediaType> ? NonNullable<FetchResponse<Paths[Path][Method], Options, Media>["data"]> : never;
|
|
377
|
+
type PathBasedClient<Paths extends Record<string | number, any>, Media extends MediaType = MediaType> = { [Path in keyof Paths]: ClientForPath<Paths[Path], Media>; };
|
|
378
|
+
//#endregion
|
|
379
|
+
//#region src/client.d.ts
|
|
380
|
+
/**
|
|
381
|
+
* Create an openapi-fetch client.
|
|
382
|
+
*/
|
|
383
|
+
export default function createClient<Paths extends {}, Media extends MediaType = MediaType>(clientOptions?: ClientOptions): Client<Paths, Media>;
|
|
384
|
+
//#endregion
|
|
385
|
+
//#region src/path-client.d.ts
|
|
386
|
+
/**
|
|
387
|
+
* Wrap openapi-fetch client to support a path based API.
|
|
388
|
+
*/
|
|
389
|
+
export declare function wrapAsPathBasedClient<Paths extends {}, Media extends MediaType = MediaType>(coreClient: Client<Paths, Media>): PathBasedClient<Paths, Media>;
|
|
390
|
+
/**
|
|
391
|
+
* Convenience method to an openapi-fetch path based client.
|
|
392
|
+
* Strictly equivalent to `wrapAsPathBasedClient(createClient(...))`.
|
|
393
|
+
*/
|
|
394
|
+
export declare function createPathBasedClient<Paths extends {}, Media extends MediaType = MediaType>(clientOptions?: ClientOptions): PathBasedClient<Paths, Media>;
|
|
395
|
+
//#endregion
|
|
396
|
+
//#region src/headers.d.ts
|
|
397
|
+
/** Merge headers a and b, with b taking priority */
|
|
398
|
+
export declare function mergeHeaders(...allHeaders: (HeadersOptions | undefined)[]): Headers;
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/serializers/param.d.ts
|
|
401
|
+
type SerializePrimitiveOptions = {
|
|
402
|
+
allowReserved?: boolean;
|
|
403
|
+
};
|
|
404
|
+
/** Serialize primitive params to string */
|
|
405
|
+
export declare function serializePrimitiveParam(name: string, value: string | number | boolean | null | undefined, options?: SerializePrimitiveOptions): string;
|
|
406
|
+
type SerializeObjectOptions = {
|
|
407
|
+
style: "simple" | "label" | "matrix" | "form" | "deepObject";
|
|
408
|
+
explode: boolean;
|
|
409
|
+
allowReserved?: boolean;
|
|
410
|
+
};
|
|
411
|
+
/** Serialize object param to string */
|
|
412
|
+
export declare function serializeObjectParam(name: string, value: Record<string, unknown>, options: SerializeObjectOptions): string;
|
|
413
|
+
type SerializeArrayOptions = {
|
|
414
|
+
style: "simple" | "label" | "matrix" | "form" | "spaceDelimited" | "pipeDelimited";
|
|
415
|
+
explode: boolean;
|
|
416
|
+
allowReserved?: boolean;
|
|
417
|
+
};
|
|
418
|
+
/** Serialize array param (shallow only) */
|
|
419
|
+
export declare function serializeArrayParam(name: string, value: unknown[], options: SerializeArrayOptions): string;
|
|
420
|
+
//#endregion
|
|
421
|
+
//#region src/serializers/query.d.ts
|
|
422
|
+
/** Serialize query params to string */
|
|
423
|
+
export declare function createQuerySerializer<T = unknown>(options?: QuerySerializerOptions): (queryParams: T) => string;
|
|
424
|
+
//#endregion
|
|
425
|
+
//#region src/serializers/path.d.ts
|
|
426
|
+
/**
|
|
427
|
+
* Handle different OpenAPI 3.x serialization styles
|
|
428
|
+
* @see https://swagger.io/docs/specification/serialization/#path
|
|
429
|
+
*/
|
|
430
|
+
export declare function defaultPathSerializer(pathname: string, pathParams: Record<string, unknown>): string;
|
|
431
|
+
//#endregion
|
|
432
|
+
//#region src/serializers/body.d.ts
|
|
433
|
+
/** Serialize body object to string */
|
|
434
|
+
export declare function defaultBodySerializer<T>(body: T, headers?: Headers | Record<string, unknown>): string | FormData;
|
|
435
|
+
//#endregion
|
|
436
|
+
//#region src/serializers/url.d.ts
|
|
437
|
+
/** Construct URL string from baseUrl and handle path and query params */
|
|
438
|
+
export declare function createFinalURL<O>(pathname: string, options: {
|
|
439
|
+
baseUrl: string;
|
|
440
|
+
params: {
|
|
441
|
+
query?: Record<string, unknown>;
|
|
442
|
+
path?: Record<string, unknown>;
|
|
443
|
+
};
|
|
444
|
+
querySerializer: QuerySerializer<O>;
|
|
445
|
+
pathSerializer: PathSerializer;
|
|
446
|
+
}): string;
|
|
447
|
+
/** Remove trailing slash from url */
|
|
448
|
+
export declare function removeTrailingSlash(url: string): string;
|
|
449
|
+
//#endregion
|
|
450
|
+
export { type $Read, type $Write, type BodySerializer, type Client, type ClientForPath, type ClientMethod, type ClientOptions, type ClientPathsWithMethod, type ClientRequestMethod, type DefaultParamsOption, type ErrorResponse, type ErrorResponseJSON, type ErrorStatus, type FetchOptions, type FetchResponse, type FilterKeys, type FirstErrorStatus, type GetResponseContent, type HeadersOptions, type HttpMethod, type IsOperationRequestBodyOptional, type JSONLike, type MaybeOptionalInit, type MediaType, type MergedOptions, type MethodResponse, type Middleware, type MiddlewareCallbackParams, type MiddlewareRequestParams, type OKStatusUnion, type OkStatus, type OperationObject, type OperationRequestBody, type OperationRequestBodyContent, type OperationRequestBodyMediaContent, type ParamsOption, type ParseAs, type ParseAsResponse, type PathBasedClient, type PathItemObject, type PathSerializer, type PathsWithMethod, type QuerySerializer, type QuerySerializerOptions, type Readable, type RequestBodyJSON, type RequestBodyOption, type RequestOptions, type RequiredKeysOf, type ResponseContent, type ResponseObjectMap, type SuccessResponse, type SuccessResponseJSON, type Writable, createClient };
|