openapi-typesafe-fetch 1.0.0-rc.3 → 1.0.0-rc.4

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # openapi-typesafe-fetch
2
2
 
3
- A type-safe `fetch` client for types generated by [`openapi-typesafe`](../openapi-typesafe). About 8.7 kB minified
4
- (3.4 kB gzipped), no runtime dependencies, works in browsers, Node.js, Bun and Deno, and with React, Vue, Svelte or plain JavaScript.
3
+ A type-safe `fetch` client for types generated by [`openapi-typesafe`](../openapi-typesafe). About 9 kB minified
4
+ (3.7 kB gzipped), no runtime dependencies, works in browsers, Node.js, Bun and Deno, and with React, Vue, Svelte or plain JavaScript.
5
5
 
6
6
  A maintained, drop-in successor to `openapi-fetch` (see the [repository README](../../README.md)).
7
7
 
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=helpers.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
@@ -0,0 +1,299 @@
1
+ /// <reference lib="es2018.asynciterable" />
2
+ type HttpMethod = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace"
3
+ /** OpenAPI 3.2: a safe, idempotent request with a body. */
4
+ | "query";
5
+ /** 2XX statuses */
6
+ type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207 | "2XX";
7
+ /** 4XX and 5XX statuses */
8
+ 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";
9
+ /** Get a union of OK Statuses */
10
+ type OKStatusUnion<T> = FilterKeys<T, OkStatus>;
11
+ /** Get first error status, in order */
12
+ type FirstErrorStatus<T> = T extends {
13
+ 500: unknown;
14
+ } ? T[500] : T extends {
15
+ 501: unknown;
16
+ } ? T[501] : T extends {
17
+ 502: unknown;
18
+ } ? T[502] : T extends {
19
+ 503: unknown;
20
+ } ? T[503] : T extends {
21
+ 504: unknown;
22
+ } ? T[504] : T extends {
23
+ 505: unknown;
24
+ } ? T[505] : T extends {
25
+ 506: unknown;
26
+ } ? T[506] : T extends {
27
+ 507: unknown;
28
+ } ? T[507] : T extends {
29
+ 508: unknown;
30
+ } ? T[508] : T extends {
31
+ 510: unknown;
32
+ } ? T[510] : T extends {
33
+ 511: unknown;
34
+ } ? T[511] : T extends {
35
+ "5XX": unknown;
36
+ } ? T["5XX"] : T extends {
37
+ 400: unknown;
38
+ } ? T[400] : T extends {
39
+ 401: unknown;
40
+ } ? T[401] : T extends {
41
+ 402: unknown;
42
+ } ? T[402] : T extends {
43
+ 403: unknown;
44
+ } ? T[403] : T extends {
45
+ 404: unknown;
46
+ } ? T[404] : T extends {
47
+ 405: unknown;
48
+ } ? T[405] : T extends {
49
+ 406: unknown;
50
+ } ? T[406] : T extends {
51
+ 407: unknown;
52
+ } ? T[407] : T extends {
53
+ 408: unknown;
54
+ } ? T[408] : T extends {
55
+ 409: unknown;
56
+ } ? T[409] : T extends {
57
+ 410: unknown;
58
+ } ? T[410] : T extends {
59
+ 411: unknown;
60
+ } ? T[411] : T extends {
61
+ 412: unknown;
62
+ } ? T[412] : T extends {
63
+ 413: unknown;
64
+ } ? T[413] : T extends {
65
+ 414: unknown;
66
+ } ? T[414] : T extends {
67
+ 415: unknown;
68
+ } ? T[415] : T extends {
69
+ 416: unknown;
70
+ } ? T[416] : T extends {
71
+ 417: unknown;
72
+ } ? T[417] : T extends {
73
+ 418: unknown;
74
+ } ? T[418] : T extends {
75
+ 420: unknown;
76
+ } ? T[420] : T extends {
77
+ 421: unknown;
78
+ } ? T[421] : T extends {
79
+ 422: unknown;
80
+ } ? T[422] : T extends {
81
+ 423: unknown;
82
+ } ? T[423] : T extends {
83
+ 424: unknown;
84
+ } ? T[424] : T extends {
85
+ 425: unknown;
86
+ } ? T[425] : T extends {
87
+ 426: unknown;
88
+ } ? T[426] : T extends {
89
+ 427: unknown;
90
+ } ? T[427] : T extends {
91
+ 428: unknown;
92
+ } ? T[428] : T extends {
93
+ 429: unknown;
94
+ } ? T[429] : T extends {
95
+ 430: unknown;
96
+ } ? T[430] : T extends {
97
+ 431: unknown;
98
+ } ? T[431] : T extends {
99
+ 444: unknown;
100
+ } ? T[444] : T extends {
101
+ 450: unknown;
102
+ } ? T[450] : T extends {
103
+ 451: unknown;
104
+ } ? T[451] : T extends {
105
+ 497: unknown;
106
+ } ? T[497] : T extends {
107
+ 498: unknown;
108
+ } ? T[498] : T extends {
109
+ 499: unknown;
110
+ } ? T[499] : T extends {
111
+ "4XX": unknown;
112
+ } ? T["4XX"] : T extends {
113
+ default: unknown;
114
+ } ? T["default"] : never;
115
+ /** The paths that have each of the methods: upstream's `PathsWithMethod`. */
116
+ type PathsWithEveryMethod<Paths extends {}, PathnameMethod extends HttpMethod> = {
117
+ [Pathname in keyof Paths]: Paths[Pathname] extends {
118
+ [K in PathnameMethod]: unknown;
119
+ } ? Pathname : never;
120
+ }[keyof Paths];
121
+ /** The paths that have at least one method. */
122
+ type PathsWithAnyMethod<Paths extends {}> = {
123
+ [Pathname in keyof Paths]: {
124
+ [Method in HttpMethod]: Paths[Pathname] extends {
125
+ [K in Method]: unknown;
126
+ } ? Pathname : never;
127
+ }[HttpMethod];
128
+ }[keyof Paths];
129
+ /**
130
+ * Given an OpenAPI **Paths Object**, find all paths that have the given method. For a union of
131
+ * methods, the paths that have each of them, so a call with either method finds its operation.
132
+ * For `HttpMethod` itself, a method not known yet, the paths that have any method too: TypeScript
133
+ * uses it where a hook's `Method` is not inferred, as in `Parameters<typeof $api.queryOptions>`,
134
+ * and the paths with every method were none (upstream #2178).
135
+ *
136
+ * A union with upstream's type, not a conditional type over the method: code that is generic in
137
+ * the method (`Path extends PathsWithMethod<Paths, Method>`, as swr-openapi has) must still
138
+ * relate one `PathsWithMethod` to another, and TypeScript cannot relate a type to a conditional
139
+ * type that waits for its method (TS2344). For a known method the second member is `never`.
140
+ */
141
+ type PathsWithMethod<Paths extends {}, PathnameMethod extends HttpMethod> = PathsWithEveryMethod<Paths, PathnameMethod> | (HttpMethod extends PathnameMethod ? PathsWithAnyMethod<Paths> : never);
142
+ /**
143
+ * DO NOT USE!
144
+ * Only used only for OperationObject type inference
145
+ */
146
+ interface OperationObject {
147
+ parameters: any;
148
+ requestBody: any;
149
+ responses: any;
150
+ }
151
+ /** Internal helper used in PathsWithMethod */
152
+ type PathItemObject = {
153
+ [M in HttpMethod]: OperationObject;
154
+ } & {
155
+ parameters?: any;
156
+ };
157
+ /** Return `responses` for an Operation Object */
158
+ type ResponseObjectMap<T> = T extends {
159
+ responses: any;
160
+ } ? T["responses"] : unknown;
161
+ /** Return `content` for a Response Object */
162
+ type ResponseContent<T> = T extends {
163
+ content: any;
164
+ } ? T["content"] : unknown;
165
+ /** Return type of `requestBody` for an Operation Object */
166
+ type OperationRequestBody<T> = "requestBody" extends keyof T ? T["requestBody"] : never;
167
+ /** Internal helper to get object type with only the `requestBody` property */
168
+ type PickRequestBody<T> = "requestBody" extends keyof T ? Pick<T, "requestBody"> : never;
169
+ /** Resolve to `true` if request body is optional, else `false` */
170
+ type IsOperationRequestBodyOptional<T> = RequiredKeysOf<PickRequestBody<T>> extends never ? true : false;
171
+ /** Internal helper used in OperationRequestBodyContent */
172
+ type OperationRequestBodyMediaContent<T> = IsOperationRequestBodyOptional<T> extends true ? ResponseContent<NonNullable<OperationRequestBody<T>>> | undefined : ResponseContent<OperationRequestBody<T>>;
173
+ /** Return first `content` from a Request Object Mapping, allowing any media type */
174
+ type OperationRequestBodyContent<T> = FilterKeys<OperationRequestBodyMediaContent<T>, MediaType> extends never ? FilterKeys<NonNullable<OperationRequestBodyMediaContent<T>>, MediaType> | undefined : FilterKeys<OperationRequestBodyMediaContent<T>, MediaType>;
175
+ /**
176
+ * The statuses of success responses in a Response Object Map: the 2XX statuses, or `default` when
177
+ * no 2XX status is declared. `default` covers every status the operation does not declare (OpenAPI
178
+ * Responses Object), so then it covers every success (upstream #1862). With a 2XX status declared,
179
+ * `default` is left to the errors, as before.
180
+ */
181
+ type SuccessStatus<T> = [Extract<keyof T, OkStatus>] extends [never] ? "default" : OkStatus;
182
+ /**
183
+ * Return all 2XX responses from a Response Object Map, or `default` if it declares no 2XX one. For
184
+ * a union of maps (a call with a union of paths), the union of each map's responses: `keyof` of a
185
+ * union is only the shared statuses, so one map of them lost the body of every path that did not
186
+ * share its statuses, and a `default`-only path typed the data `undefined` (upstream).
187
+ */
188
+ type SuccessResponse<T extends Record<string | number, any>, Media extends MediaType = MediaType> = T extends unknown ? GetResponseContent<T, Media, SuccessStatus<T>> : never;
189
+ type GetResponseContent<T extends Record<string | number, any>, Media extends MediaType = MediaType, ResponseCode extends keyof T = keyof T> = ResponseCode extends keyof T ? {
190
+ [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;
191
+ }[ResponseCode] : never;
192
+ /**
193
+ * Return all 5XX and 4XX responses (in that order) from a Response Object Map; for a union of maps,
194
+ * the union of each map's responses, as `SuccessResponse`.
195
+ */
196
+ type ErrorResponse<T extends Record<string | number, any>, Media extends MediaType = MediaType> = T extends unknown ? GetResponseContent<T, Media, ErrorStatus> : never;
197
+ /** Return first JSON-like 2XX response from a path + HTTP method */
198
+ type SuccessResponseJSON<PathMethod extends Record<string | number, any>> = SuccessResponse<ResponseObjectMap<PathMethod>, `${string}/json`>;
199
+ /** Return first JSON-like 5XX or 4XX response from a path + HTTP method */
200
+ type ErrorResponseJSON<PathMethod extends Record<string | number, any>> = ErrorResponse<ResponseObjectMap<PathMethod>, `${string}/json`>;
201
+ /** Return JSON-like request body from a path + HTTP method */
202
+ type RequestBodyJSON<PathMethod> = JSONLike<FilterKeys<OperationRequestBody<PathMethod>, "content">>;
203
+ /** Find first match of multiple keys */
204
+ type FilterKeys<Obj, Matchers> = Obj[keyof Obj & Matchers];
205
+ /**
206
+ * @deprecated Use `FilterKeys` instead
207
+ * Get the type of a value of an input object with a given key. If the key is
208
+ * not found, return a default type. Works with unions of objects too.
209
+ */
210
+ type GetValueWithDefault<Obj, KeyPattern, Default> = Obj extends unknown ? FilterKeys<Obj, KeyPattern> extends never ? Default : FilterKeys<Obj, KeyPattern> : never;
211
+ /**
212
+ * Return any `[string]/[string]` media type (important because openapi-typesafe-fetch allows any
213
+ * content response, not just JSON-like)
214
+ */
215
+ type MediaType = `${string}/${string}`;
216
+ /**
217
+ * Return any media type containing "json" (works for "application/json",
218
+ * "application/vnd.api+json", "application/vnd.oai.openapi+json")
219
+ */
220
+ type JSONLike<T> = FilterKeys<T, `${string}/json`>;
221
+ /**
222
+ * Filter objects that have required keys
223
+ * @deprecated Use `RequiredKeysOf` instead
224
+ */
225
+ type FindRequiredKeys<T, K extends keyof T> = K extends unknown ? undefined extends T[K] ? never : K : K;
226
+ /**
227
+ * Does this object contain required keys?
228
+ * @deprecated Use `RequiredKeysOf` instead
229
+ */
230
+ type HasRequiredKeys<T> = FindRequiredKeys<T, keyof T>;
231
+ /**
232
+ * Helper to get the required keys of an object. If no keys are required, will be `undefined` with
233
+ * strictNullChecks enabled, else `never`
234
+ */
235
+ type RequiredKeysOfHelper<T> = {
236
+ [K in keyof T]: {} extends Pick<T, K> ? never : K;
237
+ }[keyof T];
238
+ /** Get the required keys of an object, or `never` if no keys are required */
239
+ type RequiredKeysOf<T> = RequiredKeysOfHelper<T> extends undefined ? never : RequiredKeysOfHelper<T>;
240
+ /** Marker type for readOnly properties (excluded from request bodies) */
241
+ type $Read<T> = {
242
+ readonly $read: T;
243
+ };
244
+ /** Marker type for writeOnly properties (excluded from response bodies) */
245
+ type $Write<T> = {
246
+ readonly $write: T;
247
+ };
248
+ /** Primitives, also branded ones (`string & { __brand: "Id" }`), which stay unmapped (#2765). */
249
+ type ReadWritePrimitive = string | number | boolean | bigint | symbol | null | undefined;
250
+ /**
251
+ * Types that stay unmapped. Mapping a function type drops its call signatures, so a `Blob` from
252
+ * `--binary-as-blob` lost its methods (`arrayBuffer()`, `text()`, ...) in response data.
253
+ */
254
+ type ReadWriteLeaf = ReadWritePrimitive | ((...args: never) => unknown);
255
+ /**
256
+ * Keys of `T` whose value is a `Marker`. A key whose value is only `null` or `undefined` is not
257
+ * marked: `NonNullable` of it is `never`, and `never` extends every marker (upstream #2717). An
258
+ * `any` key is not marked either (`0 extends 1 & T[K]` is true only for `any`): a check on `any`
259
+ * takes both branches, so `Readable` dropped the key and `Writable` typed it `undefined`. The
260
+ * checks are not distributive (their check types are not type parameters), so `never` is seen.
261
+ */
262
+ type ReadWriteMarkedKeys<T, Marker> = {
263
+ [K in keyof T]-?: 0 extends 1 & T[K] ? never : NonNullable<T[K]> extends never ? never : NonNullable<T[K]> extends Marker ? K : never;
264
+ }[keyof T];
265
+ /**
266
+ * Resolve an array or a tuple element by element. An array uses `T[number]`, not `infer E`: for an
267
+ * intersection of arrays (`allOf`) inference keeps one member and narrowing breaks (upstream
268
+ * #2620). `T` is a plain array when an array of its elements is assignable to it; otherwise it is a
269
+ * tuple (also `[A, ...B[]]`), mapped homomorphically to stay a tuple (upstream #2632). Both keep
270
+ * `readonly` (upstream #2615).
271
+ */
272
+ type ReadWriteResolveArray<T extends readonly unknown[], Resolved extends "read" | "write"> = T[number][] extends T ? T extends unknown[] ? ReadWriteResolve<T[number], Resolved>[] : readonly ReadWriteResolve<T[number], Resolved>[] : {
273
+ [K in keyof T]: ReadWriteResolve<T[K], Resolved>;
274
+ };
275
+ type ReadWriteResolve<T, Resolved extends "read" | "write"> = Resolved extends "read" ? Readable<T> : Writable<T>;
276
+ /**
277
+ * Resolve type for reading (responses): strips $Write properties, unwraps $Read
278
+ * - $Read<T> → T (readable), continues recursion
279
+ * - $Write<T> → never (excluded from response)
280
+ * - primitives (also branded), functions, arrays and tuples keep their shape; objects resolve
281
+ * recursively
282
+ */
283
+ type Readable<T> = T extends $Write<any> ? never : T extends $Read<infer U> ? Readable<U> : T extends ReadWriteLeaf ? T : T extends readonly unknown[] ? ReadWriteResolveArray<T, "read"> : T extends object ? {
284
+ [K in keyof T as K extends ReadWriteMarkedKeys<T, $Write<any>> ? never : K]: Readable<T[K]>;
285
+ } : T;
286
+ /**
287
+ * Resolve type for writing (requests): strips $Read properties, unwraps $Write
288
+ * - $Write<T> → T (writable), continues recursion
289
+ * - $Read<T> → never (excluded from request)
290
+ * - primitives (also branded), functions, arrays and tuples keep their shape; objects resolve
291
+ * recursively
292
+ */
293
+ type Writable<T> = T extends $Read<any> ? never : T extends $Write<infer U> ? Writable<U> : T extends ReadWriteLeaf ? T : T extends readonly unknown[] ? ReadWriteResolveArray<T, "write"> : T extends object ? {
294
+ [K in keyof T as K extends ReadWriteMarkedKeys<T, $Read<any>> ? never : K]: Writable<T[K]>;
295
+ } & {
296
+ [K in ReadWriteMarkedKeys<T, $Read<any>>]?: never;
297
+ } : T;
298
+
299
+ export type { $Read, $Write, ErrorResponse, ErrorResponseJSON, ErrorStatus, FilterKeys, FindRequiredKeys, FirstErrorStatus, GetResponseContent, GetValueWithDefault, HasRequiredKeys, HttpMethod, IsOperationRequestBodyOptional, JSONLike, MediaType, OKStatusUnion, OkStatus, OperationObject, OperationRequestBody, OperationRequestBodyContent, OperationRequestBodyMediaContent, PathItemObject, PathsWithMethod, Readable, RequestBodyJSON, RequiredKeysOf, ResponseContent, ResponseObjectMap, SuccessResponse, SuccessResponseJSON, Writable };
@@ -0,0 +1,299 @@
1
+ /// <reference lib="es2018.asynciterable" />
2
+ type HttpMethod = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace"
3
+ /** OpenAPI 3.2: a safe, idempotent request with a body. */
4
+ | "query";
5
+ /** 2XX statuses */
6
+ type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207 | "2XX";
7
+ /** 4XX and 5XX statuses */
8
+ 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";
9
+ /** Get a union of OK Statuses */
10
+ type OKStatusUnion<T> = FilterKeys<T, OkStatus>;
11
+ /** Get first error status, in order */
12
+ type FirstErrorStatus<T> = T extends {
13
+ 500: unknown;
14
+ } ? T[500] : T extends {
15
+ 501: unknown;
16
+ } ? T[501] : T extends {
17
+ 502: unknown;
18
+ } ? T[502] : T extends {
19
+ 503: unknown;
20
+ } ? T[503] : T extends {
21
+ 504: unknown;
22
+ } ? T[504] : T extends {
23
+ 505: unknown;
24
+ } ? T[505] : T extends {
25
+ 506: unknown;
26
+ } ? T[506] : T extends {
27
+ 507: unknown;
28
+ } ? T[507] : T extends {
29
+ 508: unknown;
30
+ } ? T[508] : T extends {
31
+ 510: unknown;
32
+ } ? T[510] : T extends {
33
+ 511: unknown;
34
+ } ? T[511] : T extends {
35
+ "5XX": unknown;
36
+ } ? T["5XX"] : T extends {
37
+ 400: unknown;
38
+ } ? T[400] : T extends {
39
+ 401: unknown;
40
+ } ? T[401] : T extends {
41
+ 402: unknown;
42
+ } ? T[402] : T extends {
43
+ 403: unknown;
44
+ } ? T[403] : T extends {
45
+ 404: unknown;
46
+ } ? T[404] : T extends {
47
+ 405: unknown;
48
+ } ? T[405] : T extends {
49
+ 406: unknown;
50
+ } ? T[406] : T extends {
51
+ 407: unknown;
52
+ } ? T[407] : T extends {
53
+ 408: unknown;
54
+ } ? T[408] : T extends {
55
+ 409: unknown;
56
+ } ? T[409] : T extends {
57
+ 410: unknown;
58
+ } ? T[410] : T extends {
59
+ 411: unknown;
60
+ } ? T[411] : T extends {
61
+ 412: unknown;
62
+ } ? T[412] : T extends {
63
+ 413: unknown;
64
+ } ? T[413] : T extends {
65
+ 414: unknown;
66
+ } ? T[414] : T extends {
67
+ 415: unknown;
68
+ } ? T[415] : T extends {
69
+ 416: unknown;
70
+ } ? T[416] : T extends {
71
+ 417: unknown;
72
+ } ? T[417] : T extends {
73
+ 418: unknown;
74
+ } ? T[418] : T extends {
75
+ 420: unknown;
76
+ } ? T[420] : T extends {
77
+ 421: unknown;
78
+ } ? T[421] : T extends {
79
+ 422: unknown;
80
+ } ? T[422] : T extends {
81
+ 423: unknown;
82
+ } ? T[423] : T extends {
83
+ 424: unknown;
84
+ } ? T[424] : T extends {
85
+ 425: unknown;
86
+ } ? T[425] : T extends {
87
+ 426: unknown;
88
+ } ? T[426] : T extends {
89
+ 427: unknown;
90
+ } ? T[427] : T extends {
91
+ 428: unknown;
92
+ } ? T[428] : T extends {
93
+ 429: unknown;
94
+ } ? T[429] : T extends {
95
+ 430: unknown;
96
+ } ? T[430] : T extends {
97
+ 431: unknown;
98
+ } ? T[431] : T extends {
99
+ 444: unknown;
100
+ } ? T[444] : T extends {
101
+ 450: unknown;
102
+ } ? T[450] : T extends {
103
+ 451: unknown;
104
+ } ? T[451] : T extends {
105
+ 497: unknown;
106
+ } ? T[497] : T extends {
107
+ 498: unknown;
108
+ } ? T[498] : T extends {
109
+ 499: unknown;
110
+ } ? T[499] : T extends {
111
+ "4XX": unknown;
112
+ } ? T["4XX"] : T extends {
113
+ default: unknown;
114
+ } ? T["default"] : never;
115
+ /** The paths that have each of the methods: upstream's `PathsWithMethod`. */
116
+ type PathsWithEveryMethod<Paths extends {}, PathnameMethod extends HttpMethod> = {
117
+ [Pathname in keyof Paths]: Paths[Pathname] extends {
118
+ [K in PathnameMethod]: unknown;
119
+ } ? Pathname : never;
120
+ }[keyof Paths];
121
+ /** The paths that have at least one method. */
122
+ type PathsWithAnyMethod<Paths extends {}> = {
123
+ [Pathname in keyof Paths]: {
124
+ [Method in HttpMethod]: Paths[Pathname] extends {
125
+ [K in Method]: unknown;
126
+ } ? Pathname : never;
127
+ }[HttpMethod];
128
+ }[keyof Paths];
129
+ /**
130
+ * Given an OpenAPI **Paths Object**, find all paths that have the given method. For a union of
131
+ * methods, the paths that have each of them, so a call with either method finds its operation.
132
+ * For `HttpMethod` itself, a method not known yet, the paths that have any method too: TypeScript
133
+ * uses it where a hook's `Method` is not inferred, as in `Parameters<typeof $api.queryOptions>`,
134
+ * and the paths with every method were none (upstream #2178).
135
+ *
136
+ * A union with upstream's type, not a conditional type over the method: code that is generic in
137
+ * the method (`Path extends PathsWithMethod<Paths, Method>`, as swr-openapi has) must still
138
+ * relate one `PathsWithMethod` to another, and TypeScript cannot relate a type to a conditional
139
+ * type that waits for its method (TS2344). For a known method the second member is `never`.
140
+ */
141
+ type PathsWithMethod<Paths extends {}, PathnameMethod extends HttpMethod> = PathsWithEveryMethod<Paths, PathnameMethod> | (HttpMethod extends PathnameMethod ? PathsWithAnyMethod<Paths> : never);
142
+ /**
143
+ * DO NOT USE!
144
+ * Only used only for OperationObject type inference
145
+ */
146
+ interface OperationObject {
147
+ parameters: any;
148
+ requestBody: any;
149
+ responses: any;
150
+ }
151
+ /** Internal helper used in PathsWithMethod */
152
+ type PathItemObject = {
153
+ [M in HttpMethod]: OperationObject;
154
+ } & {
155
+ parameters?: any;
156
+ };
157
+ /** Return `responses` for an Operation Object */
158
+ type ResponseObjectMap<T> = T extends {
159
+ responses: any;
160
+ } ? T["responses"] : unknown;
161
+ /** Return `content` for a Response Object */
162
+ type ResponseContent<T> = T extends {
163
+ content: any;
164
+ } ? T["content"] : unknown;
165
+ /** Return type of `requestBody` for an Operation Object */
166
+ type OperationRequestBody<T> = "requestBody" extends keyof T ? T["requestBody"] : never;
167
+ /** Internal helper to get object type with only the `requestBody` property */
168
+ type PickRequestBody<T> = "requestBody" extends keyof T ? Pick<T, "requestBody"> : never;
169
+ /** Resolve to `true` if request body is optional, else `false` */
170
+ type IsOperationRequestBodyOptional<T> = RequiredKeysOf<PickRequestBody<T>> extends never ? true : false;
171
+ /** Internal helper used in OperationRequestBodyContent */
172
+ type OperationRequestBodyMediaContent<T> = IsOperationRequestBodyOptional<T> extends true ? ResponseContent<NonNullable<OperationRequestBody<T>>> | undefined : ResponseContent<OperationRequestBody<T>>;
173
+ /** Return first `content` from a Request Object Mapping, allowing any media type */
174
+ type OperationRequestBodyContent<T> = FilterKeys<OperationRequestBodyMediaContent<T>, MediaType> extends never ? FilterKeys<NonNullable<OperationRequestBodyMediaContent<T>>, MediaType> | undefined : FilterKeys<OperationRequestBodyMediaContent<T>, MediaType>;
175
+ /**
176
+ * The statuses of success responses in a Response Object Map: the 2XX statuses, or `default` when
177
+ * no 2XX status is declared. `default` covers every status the operation does not declare (OpenAPI
178
+ * Responses Object), so then it covers every success (upstream #1862). With a 2XX status declared,
179
+ * `default` is left to the errors, as before.
180
+ */
181
+ type SuccessStatus<T> = [Extract<keyof T, OkStatus>] extends [never] ? "default" : OkStatus;
182
+ /**
183
+ * Return all 2XX responses from a Response Object Map, or `default` if it declares no 2XX one. For
184
+ * a union of maps (a call with a union of paths), the union of each map's responses: `keyof` of a
185
+ * union is only the shared statuses, so one map of them lost the body of every path that did not
186
+ * share its statuses, and a `default`-only path typed the data `undefined` (upstream).
187
+ */
188
+ type SuccessResponse<T extends Record<string | number, any>, Media extends MediaType = MediaType> = T extends unknown ? GetResponseContent<T, Media, SuccessStatus<T>> : never;
189
+ type GetResponseContent<T extends Record<string | number, any>, Media extends MediaType = MediaType, ResponseCode extends keyof T = keyof T> = ResponseCode extends keyof T ? {
190
+ [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;
191
+ }[ResponseCode] : never;
192
+ /**
193
+ * Return all 5XX and 4XX responses (in that order) from a Response Object Map; for a union of maps,
194
+ * the union of each map's responses, as `SuccessResponse`.
195
+ */
196
+ type ErrorResponse<T extends Record<string | number, any>, Media extends MediaType = MediaType> = T extends unknown ? GetResponseContent<T, Media, ErrorStatus> : never;
197
+ /** Return first JSON-like 2XX response from a path + HTTP method */
198
+ type SuccessResponseJSON<PathMethod extends Record<string | number, any>> = SuccessResponse<ResponseObjectMap<PathMethod>, `${string}/json`>;
199
+ /** Return first JSON-like 5XX or 4XX response from a path + HTTP method */
200
+ type ErrorResponseJSON<PathMethod extends Record<string | number, any>> = ErrorResponse<ResponseObjectMap<PathMethod>, `${string}/json`>;
201
+ /** Return JSON-like request body from a path + HTTP method */
202
+ type RequestBodyJSON<PathMethod> = JSONLike<FilterKeys<OperationRequestBody<PathMethod>, "content">>;
203
+ /** Find first match of multiple keys */
204
+ type FilterKeys<Obj, Matchers> = Obj[keyof Obj & Matchers];
205
+ /**
206
+ * @deprecated Use `FilterKeys` instead
207
+ * Get the type of a value of an input object with a given key. If the key is
208
+ * not found, return a default type. Works with unions of objects too.
209
+ */
210
+ type GetValueWithDefault<Obj, KeyPattern, Default> = Obj extends unknown ? FilterKeys<Obj, KeyPattern> extends never ? Default : FilterKeys<Obj, KeyPattern> : never;
211
+ /**
212
+ * Return any `[string]/[string]` media type (important because openapi-typesafe-fetch allows any
213
+ * content response, not just JSON-like)
214
+ */
215
+ type MediaType = `${string}/${string}`;
216
+ /**
217
+ * Return any media type containing "json" (works for "application/json",
218
+ * "application/vnd.api+json", "application/vnd.oai.openapi+json")
219
+ */
220
+ type JSONLike<T> = FilterKeys<T, `${string}/json`>;
221
+ /**
222
+ * Filter objects that have required keys
223
+ * @deprecated Use `RequiredKeysOf` instead
224
+ */
225
+ type FindRequiredKeys<T, K extends keyof T> = K extends unknown ? undefined extends T[K] ? never : K : K;
226
+ /**
227
+ * Does this object contain required keys?
228
+ * @deprecated Use `RequiredKeysOf` instead
229
+ */
230
+ type HasRequiredKeys<T> = FindRequiredKeys<T, keyof T>;
231
+ /**
232
+ * Helper to get the required keys of an object. If no keys are required, will be `undefined` with
233
+ * strictNullChecks enabled, else `never`
234
+ */
235
+ type RequiredKeysOfHelper<T> = {
236
+ [K in keyof T]: {} extends Pick<T, K> ? never : K;
237
+ }[keyof T];
238
+ /** Get the required keys of an object, or `never` if no keys are required */
239
+ type RequiredKeysOf<T> = RequiredKeysOfHelper<T> extends undefined ? never : RequiredKeysOfHelper<T>;
240
+ /** Marker type for readOnly properties (excluded from request bodies) */
241
+ type $Read<T> = {
242
+ readonly $read: T;
243
+ };
244
+ /** Marker type for writeOnly properties (excluded from response bodies) */
245
+ type $Write<T> = {
246
+ readonly $write: T;
247
+ };
248
+ /** Primitives, also branded ones (`string & { __brand: "Id" }`), which stay unmapped (#2765). */
249
+ type ReadWritePrimitive = string | number | boolean | bigint | symbol | null | undefined;
250
+ /**
251
+ * Types that stay unmapped. Mapping a function type drops its call signatures, so a `Blob` from
252
+ * `--binary-as-blob` lost its methods (`arrayBuffer()`, `text()`, ...) in response data.
253
+ */
254
+ type ReadWriteLeaf = ReadWritePrimitive | ((...args: never) => unknown);
255
+ /**
256
+ * Keys of `T` whose value is a `Marker`. A key whose value is only `null` or `undefined` is not
257
+ * marked: `NonNullable` of it is `never`, and `never` extends every marker (upstream #2717). An
258
+ * `any` key is not marked either (`0 extends 1 & T[K]` is true only for `any`): a check on `any`
259
+ * takes both branches, so `Readable` dropped the key and `Writable` typed it `undefined`. The
260
+ * checks are not distributive (their check types are not type parameters), so `never` is seen.
261
+ */
262
+ type ReadWriteMarkedKeys<T, Marker> = {
263
+ [K in keyof T]-?: 0 extends 1 & T[K] ? never : NonNullable<T[K]> extends never ? never : NonNullable<T[K]> extends Marker ? K : never;
264
+ }[keyof T];
265
+ /**
266
+ * Resolve an array or a tuple element by element. An array uses `T[number]`, not `infer E`: for an
267
+ * intersection of arrays (`allOf`) inference keeps one member and narrowing breaks (upstream
268
+ * #2620). `T` is a plain array when an array of its elements is assignable to it; otherwise it is a
269
+ * tuple (also `[A, ...B[]]`), mapped homomorphically to stay a tuple (upstream #2632). Both keep
270
+ * `readonly` (upstream #2615).
271
+ */
272
+ type ReadWriteResolveArray<T extends readonly unknown[], Resolved extends "read" | "write"> = T[number][] extends T ? T extends unknown[] ? ReadWriteResolve<T[number], Resolved>[] : readonly ReadWriteResolve<T[number], Resolved>[] : {
273
+ [K in keyof T]: ReadWriteResolve<T[K], Resolved>;
274
+ };
275
+ type ReadWriteResolve<T, Resolved extends "read" | "write"> = Resolved extends "read" ? Readable<T> : Writable<T>;
276
+ /**
277
+ * Resolve type for reading (responses): strips $Write properties, unwraps $Read
278
+ * - $Read<T> → T (readable), continues recursion
279
+ * - $Write<T> → never (excluded from response)
280
+ * - primitives (also branded), functions, arrays and tuples keep their shape; objects resolve
281
+ * recursively
282
+ */
283
+ type Readable<T> = T extends $Write<any> ? never : T extends $Read<infer U> ? Readable<U> : T extends ReadWriteLeaf ? T : T extends readonly unknown[] ? ReadWriteResolveArray<T, "read"> : T extends object ? {
284
+ [K in keyof T as K extends ReadWriteMarkedKeys<T, $Write<any>> ? never : K]: Readable<T[K]>;
285
+ } : T;
286
+ /**
287
+ * Resolve type for writing (requests): strips $Read properties, unwraps $Write
288
+ * - $Write<T> → T (writable), continues recursion
289
+ * - $Read<T> → never (excluded from request)
290
+ * - primitives (also branded), functions, arrays and tuples keep their shape; objects resolve
291
+ * recursively
292
+ */
293
+ type Writable<T> = T extends $Read<any> ? never : T extends $Write<infer U> ? Writable<U> : T extends ReadWriteLeaf ? T : T extends readonly unknown[] ? ReadWriteResolveArray<T, "write"> : T extends object ? {
294
+ [K in keyof T as K extends ReadWriteMarkedKeys<T, $Read<any>> ? never : K]: Writable<T[K]>;
295
+ } & {
296
+ [K in ReadWriteMarkedKeys<T, $Read<any>>]?: never;
297
+ } : T;
298
+
299
+ export type { $Read, $Write, ErrorResponse, ErrorResponseJSON, ErrorStatus, FilterKeys, FindRequiredKeys, FirstErrorStatus, GetResponseContent, GetValueWithDefault, HasRequiredKeys, HttpMethod, IsOperationRequestBodyOptional, JSONLike, MediaType, OKStatusUnion, OkStatus, OperationObject, OperationRequestBody, OperationRequestBodyContent, OperationRequestBodyMediaContent, PathItemObject, PathsWithMethod, Readable, RequestBodyJSON, RequiredKeysOf, ResponseContent, ResponseObjectMap, SuccessResponse, SuccessResponseJSON, Writable };