openapi-typesafe-fetch 1.0.0-rc.2 → 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/CHANGELOG.md +763 -0
- package/README.md +2 -2
- package/dist/helpers.cjs +3 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.cts +299 -0
- package/dist/helpers.d.mts +299 -0
- package/dist/helpers.d.ts +299 -0
- package/dist/helpers.mjs +2 -0
- package/dist/helpers.mjs.map +1 -0
- package/dist/index.cjs +190 -131
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +64 -17
- package/dist/index.d.mts +64 -17
- package/dist/index.d.ts +64 -17
- package/dist/index.mjs +190 -131
- package/dist/index.mjs.map +1 -1
- package/package.json +18 -9
- package/src/helpers.ts +437 -0
- package/src/index.d.ts +60 -16
- package/src/index.js +305 -183
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference lib="es2018.asynciterable" />
|
|
2
|
+
import { OperationRequestBodyContent, MediaType, HttpMethod, PathsWithMethod, RequiredKeysOf, Writable, IsOperationRequestBodyOptional, FilterKeys, Readable, SuccessResponse, ResponseObjectMap, ErrorResponse } from './helpers.js';
|
|
3
|
+
|
|
4
|
+
// A stream body may be an async iterable (`RawRequestBody`). The reference brings that type into a
|
|
5
|
+
// project whose `lib` is older than ES2018, which fails with TS2583 otherwise. The declaration
|
|
6
|
+
// build copies it into the published declarations (build.config.ts).
|
|
7
|
+
/// <reference lib="es2018.asynciterable" />
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// The client declares the helper types in `./helpers.js`, published as
|
|
11
|
+
// `openapi-typesafe-fetch/helpers`, and `openapi-typesafe-helpers` re-exports them: one
|
|
12
|
+
// declaration, so generic code may mix the names of both packages. Types inferred from the
|
|
13
|
+
// client print them by that path, which a library that uses the client can import, also under
|
|
14
|
+
// pnpm and Yarn PnP (ADR-034). They are not exported here, as in openapi-fetch: a project that
|
|
15
|
+
// writes `export type * from` the client next to its own `ErrorResponse` fails with TS2308. This
|
|
16
|
+
// file has no `export { … }` either, so its other top-level types stay importable from
|
|
17
|
+
// `openapi-typesafe-fetch/src/index.js`, as openapi-fetch's are (`InitParam`).
|
|
2
18
|
|
|
3
19
|
/** Options for each client instance */
|
|
4
20
|
interface ClientOptions extends Omit<RequestInit, "headers"> {
|
|
@@ -134,7 +150,17 @@ type RequestBodyOption<T> =
|
|
|
134
150
|
? { body?: Writable<OperationRequestBodyContent<T>> }
|
|
135
151
|
: { body: Writable<OperationRequestBodyContent<T>> };
|
|
136
152
|
|
|
137
|
-
|
|
153
|
+
/**
|
|
154
|
+
* The init of a call to the operation `T`; for a union of operations, the union of their inits. It
|
|
155
|
+
* distributes so that the client's init type and a wrapper's `FetchOptions` of a generic path are
|
|
156
|
+
* the same type, which TypeScript can relate: an alias that distributed only inside the client
|
|
157
|
+
* gave a conditional type no `FetchOptions` argument was assignable to (TS2345). The operations
|
|
158
|
+
* of a union are not merged into one deep init type, which gave TS2589 on hundreds of paths
|
|
159
|
+
* (upstream #1882).
|
|
160
|
+
*/
|
|
161
|
+
type FetchOptions<T> = T extends unknown
|
|
162
|
+
? RequestOptions<T> & Omit<RequestInit, "body" | "headers">
|
|
163
|
+
: never;
|
|
138
164
|
|
|
139
165
|
// Readable<T> strips $Write markers (writeOnly properties excluded from response)
|
|
140
166
|
type FetchResponse<
|
|
@@ -241,19 +267,16 @@ type MaybeOptionalInit<Params, Location extends PropertyKey> = OperationInits<
|
|
|
241
267
|
* One conditional type, as openapi-fetch's, not a union with a conditional `undefined`: while the
|
|
242
268
|
* operation is generic, a union made `FetchResponse` distribute over its members, so the `data` of
|
|
243
269
|
* a generic wrapper's call no longer related to `FetchResponse<Paths[Path][Method], Init, Media>`.
|
|
270
|
+
* Its check type is not a type parameter, so TypeScript takes a `FetchOptions` of a generic path
|
|
271
|
+
* for it when that type fits both branches. `FetchOptions` itself makes one init per operation:
|
|
272
|
+
* while the path is generic it stays an alias, not an `infer` type, which TypeScript reads as
|
|
273
|
+
* `unknown`, so the init stays the contextual type of an argument and a literal such as
|
|
274
|
+
* `"available"` keeps its type (swr-openapi's `Exact<Init>` checks).
|
|
244
275
|
*/
|
|
245
276
|
type OperationInits<Operation> =
|
|
246
|
-
RequiredKeysOf<
|
|
247
|
-
?
|
|
248
|
-
:
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Each operation's init. A distributive alias, not `extends infer Operation`: while the path is
|
|
252
|
-
* not known, TypeScript reads an `infer` type as `unknown`, so the init lost its type as the
|
|
253
|
-
* contextual type of an argument and a literal such as `"available"` widened to `string`
|
|
254
|
-
* (swr-openapi's `Exact<Init>` checks).
|
|
255
|
-
*/
|
|
256
|
-
type OperationFetchOptions<Operation> = Operation extends unknown ? FetchOptions<Operation> : never;
|
|
277
|
+
RequiredKeysOf<FetchOptions<Operation>> extends never
|
|
278
|
+
? FetchOptions<Operation> | undefined
|
|
279
|
+
: FetchOptions<Operation>;
|
|
257
280
|
|
|
258
281
|
// The final init param to accept.
|
|
259
282
|
// - Determines if the param is optional or not.
|
|
@@ -281,11 +304,19 @@ type WithAnyKeys<Init> = Init & { [key: string]: unknown };
|
|
|
281
304
|
* adds none.
|
|
282
305
|
*/
|
|
283
306
|
type PathOperation<Paths, Path, Method> = Extract<
|
|
284
|
-
FilterKeys<Paths
|
|
307
|
+
FilterKeys<PathItem<Paths, Path>, Method>,
|
|
285
308
|
// biome-ignore lint/suspicious/noExplicitAny: unknown rejects the generated interfaces
|
|
286
309
|
Record<string | number, any>
|
|
287
310
|
>;
|
|
288
311
|
|
|
312
|
+
/**
|
|
313
|
+
* The path item of each path of `Path`. Not `Paths[Path & keyof Paths]`: with template keys
|
|
314
|
+
* (`--path-params-as-types`), `/apps/${string}` intersected with the keys is every key it
|
|
315
|
+
* overlaps, such as `/apps/${string}/logs/${number}`, so a call to one path got the responses of
|
|
316
|
+
* the others too once the responses distribute over the operations.
|
|
317
|
+
*/
|
|
318
|
+
type PathItem<Paths, Path> = Path extends keyof Paths ? Paths[Path] : never;
|
|
319
|
+
|
|
289
320
|
type ClientMethod<Paths extends {}, Method extends HttpMethod, Media extends MediaType> = <
|
|
290
321
|
Path extends PathsWithMethod<Paths, Method>,
|
|
291
322
|
Init extends MaybeOptionalInit<Paths[Path], Method>,
|
|
@@ -444,8 +475,18 @@ declare function defaultPathSerializer(
|
|
|
444
475
|
pathParams: Record<string, unknown>,
|
|
445
476
|
): string;
|
|
446
477
|
|
|
447
|
-
/**
|
|
448
|
-
|
|
478
|
+
/**
|
|
479
|
+
* Bodies that `fetch` sends as they are, so `defaultBodySerializer` returns them unchanged. A
|
|
480
|
+
* stream is a `ReadableStream` or an async iterable, such as a Node.js `Readable`.
|
|
481
|
+
*/
|
|
482
|
+
type RawRequestBody =
|
|
483
|
+
| FormData
|
|
484
|
+
| Blob
|
|
485
|
+
| URLSearchParams
|
|
486
|
+
| ArrayBuffer
|
|
487
|
+
| ArrayBufferView
|
|
488
|
+
| ReadableStream
|
|
489
|
+
| AsyncIterable<unknown>;
|
|
449
490
|
|
|
450
491
|
/**
|
|
451
492
|
* The result of `defaultBodySerializer` for a body of type `Body`: the body itself when it is raw,
|
|
@@ -539,5 +580,11 @@ declare function mergeHeaders(...allHeaders: (HeadersOptions | undefined)[]): He
|
|
|
539
580
|
/** Remove trailing slash from url */
|
|
540
581
|
declare function removeTrailingSlash(url: string): string;
|
|
541
582
|
|
|
542
|
-
|
|
583
|
+
/**
|
|
584
|
+
* Return a new ID, unique within the process: the request `id` that middleware callbacks get.
|
|
585
|
+
* It comes from a counter shared by all clients, not from `Math.random()`; the name is upstream's.
|
|
586
|
+
*/
|
|
587
|
+
declare function randomID(): string;
|
|
588
|
+
|
|
589
|
+
export { createFinalURL, createPathBasedClient, createQuerySerializer, createClient as default, defaultBodySerializer, defaultPathSerializer, mergeHeaders, randomID, removeTrailingSlash, serializeArrayParam, serializeObjectParam, serializePrimitiveParam, wrapAsPathBasedClient };
|
|
543
590
|
export type { BodySerializer, Client, ClientForPath, ClientMethod, ClientOptions, ClientPathsWithMethod, ClientRequestMethod, CustomFetchClientOptions, DefaultParamsOption, FetchOptions, FetchResponse, HeadersOptions, MaybeOptionalInit, MergedOptions, MethodResponse, Middleware, MiddlewareCallbackParams, MiddlewareRequestParams, ParamsOption, ParseAs, ParseAsResponse, PathBasedClient, PathOperation, PathSerializer, QuerySerializer, QuerySerializerOptions, RequestBodyOption, RequestOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
const PATH_PARAM_RE = /\{[^{}]+\}/g;
|
|
2
|
-
const DOT_SEGMENT_RE =
|
|
2
|
+
const DOT_SEGMENT_RE = /^\.\.?$/;
|
|
3
3
|
const QUERY_OR_FRAGMENT_RE = /[?#]/;
|
|
4
|
-
const JSON_MEDIA_TYPE_RE =
|
|
4
|
+
const JSON_MEDIA_TYPE_RE = /^[^/]+\/([^;]*\+)?json\s*(;|$)/i;
|
|
5
|
+
const TEXT_MEDIA_TYPE_RE = /^text\//i;
|
|
5
6
|
const FORM_MEDIA_TYPE_RE = /^application\/x-www-form-urlencoded(;|$)/i;
|
|
6
7
|
const MULTIPART_MEDIA_TYPE_RE = /^multipart\/form-data(;|$)/i;
|
|
7
8
|
const NESTED_FORM_VALUE_MESSAGE = "Nested form values need your own `bodySerializer()`.";
|
|
8
9
|
const BOUNDARY_PARAMETER_RE = /;\s*boundary=/i;
|
|
10
|
+
const CLIENT_BUILT_FORM = Symbol.for("openapi-typesafe-fetch/form");
|
|
9
11
|
const supportsRequestInitExt = () => {
|
|
10
12
|
return typeof process === "object" && process.versions?.undici;
|
|
11
13
|
};
|
|
@@ -76,12 +78,14 @@ function createClient(clientOptions) {
|
|
|
76
78
|
headers,
|
|
77
79
|
params.header
|
|
78
80
|
);
|
|
79
|
-
if (serializedBody
|
|
80
|
-
finalHeaders
|
|
81
|
+
if (isPlatformInstance(serializedBody, "FormData")) {
|
|
82
|
+
dropFormContentType(finalHeaders, serializedBody[CLIENT_BUILT_FORM] === true);
|
|
81
83
|
}
|
|
82
84
|
const finalMiddlewares = [...globalMiddlewares, ...requestMiddlewares];
|
|
83
85
|
const requestInit = {
|
|
84
86
|
redirect: "follow",
|
|
87
|
+
// The Fetch standard requires `duplex: "half"` (its only value) for a stream body.
|
|
88
|
+
duplex: isStreamBody(serializedBody) ? "half" : void 0,
|
|
85
89
|
...baseOptions,
|
|
86
90
|
...init,
|
|
87
91
|
body: serializedBody,
|
|
@@ -141,8 +145,8 @@ function createClient(clientOptions) {
|
|
|
141
145
|
if (!response) {
|
|
142
146
|
try {
|
|
143
147
|
response = await fetch(request, requestInitExt);
|
|
144
|
-
} catch (
|
|
145
|
-
let errorAfterMiddleware =
|
|
148
|
+
} catch (error) {
|
|
149
|
+
let errorAfterMiddleware = error;
|
|
146
150
|
if (finalMiddlewares.length) {
|
|
147
151
|
for (let i = finalMiddlewares.length - 1; i >= 0; i--) {
|
|
148
152
|
const m = finalMiddlewares[i];
|
|
@@ -174,6 +178,9 @@ function createClient(clientOptions) {
|
|
|
174
178
|
throw errorAfterMiddleware;
|
|
175
179
|
}
|
|
176
180
|
}
|
|
181
|
+
if (!isResponse(response)) {
|
|
182
|
+
throw new Error("fetch: must return new Response()");
|
|
183
|
+
}
|
|
177
184
|
if (finalMiddlewares.length) {
|
|
178
185
|
for (let i = finalMiddlewares.length - 1; i >= 0; i--) {
|
|
179
186
|
const m = finalMiddlewares[i];
|
|
@@ -203,28 +210,9 @@ function createClient(clientOptions) {
|
|
|
203
210
|
return response.ok ? { data: void 0, response } : { error: void 0, response };
|
|
204
211
|
}
|
|
205
212
|
if (response.ok) {
|
|
206
|
-
|
|
207
|
-
if (parseAs === "stream") {
|
|
208
|
-
return response.body;
|
|
209
|
-
}
|
|
210
|
-
if (parseAs === "json" && !contentLength) {
|
|
211
|
-
const raw2 = await response.text();
|
|
212
|
-
return raw2 ? JSON.parse(raw2) : void 0;
|
|
213
|
-
}
|
|
214
|
-
return await response[parseAs]();
|
|
215
|
-
};
|
|
216
|
-
return { data: await getResponseData(), response };
|
|
217
|
-
}
|
|
218
|
-
const raw = await response.text();
|
|
219
|
-
if (!raw) {
|
|
220
|
-
return { error: void 0, response };
|
|
221
|
-
}
|
|
222
|
-
let error = raw;
|
|
223
|
-
try {
|
|
224
|
-
error = JSON.parse(raw);
|
|
225
|
-
} catch {
|
|
213
|
+
return { data: await readSuccessBody(response, parseAs), response };
|
|
226
214
|
}
|
|
227
|
-
return { error, response };
|
|
215
|
+
return { error: await readErrorBody(response), response };
|
|
228
216
|
}
|
|
229
217
|
return {
|
|
230
218
|
request(method, url, init) {
|
|
@@ -348,6 +336,11 @@ function wrapAsPathBasedClient(coreClient) {
|
|
|
348
336
|
function createPathBasedClient(clientOptions) {
|
|
349
337
|
return wrapAsPathBasedClient(createClient(clientOptions));
|
|
350
338
|
}
|
|
339
|
+
function dropFormContentType(headers, isClientBuilt) {
|
|
340
|
+
if (isClientBuilt || !BOUNDARY_PARAMETER_RE.test(headers.get("Content-Type"))) {
|
|
341
|
+
headers.delete("Content-Type");
|
|
342
|
+
}
|
|
343
|
+
}
|
|
351
344
|
function isHeaders(value) {
|
|
352
345
|
return typeof value?.get === "function";
|
|
353
346
|
}
|
|
@@ -355,142 +348,183 @@ function isRequest(value) {
|
|
|
355
348
|
return typeof value?.method === "string" && typeof value.url === "string" && isHeaders(value.headers);
|
|
356
349
|
}
|
|
357
350
|
function isResponse(value) {
|
|
358
|
-
return typeof value?.status === "number" && isHeaders(value.headers);
|
|
351
|
+
return typeof value?.ok === "boolean" && typeof value.status === "number" && typeof value.text === "function" && isHeaders(value.headers);
|
|
352
|
+
}
|
|
353
|
+
function parseJSON(raw, response) {
|
|
354
|
+
try {
|
|
355
|
+
return JSON.parse(raw);
|
|
356
|
+
} catch {
|
|
357
|
+
const contentType = response.headers.get("Content-Type");
|
|
358
|
+
const hint = TEXT_MEDIA_TYPE_RE.test(contentType) ? 'set `parseAs: "text"`' : "set `parseAs`";
|
|
359
|
+
const subject = `Response ${response.status} (${contentType ?? "no Content-Type"})`;
|
|
360
|
+
throw new SyntaxError(`${subject} is not JSON (${raw.length} characters); ${hint}`);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
async function readSuccessBody(response, parseAs) {
|
|
364
|
+
if (parseAs === "stream") {
|
|
365
|
+
return response.body;
|
|
366
|
+
}
|
|
367
|
+
if (parseAs !== "json") {
|
|
368
|
+
return response[parseAs]();
|
|
369
|
+
}
|
|
370
|
+
const raw = await response.text();
|
|
371
|
+
return raw ? parseJSON(raw, response) : void 0;
|
|
372
|
+
}
|
|
373
|
+
async function readErrorBody(response) {
|
|
374
|
+
const raw = await response.text();
|
|
375
|
+
if (!raw) {
|
|
376
|
+
return void 0;
|
|
377
|
+
}
|
|
378
|
+
try {
|
|
379
|
+
return JSON.parse(raw);
|
|
380
|
+
} catch {
|
|
381
|
+
return raw;
|
|
382
|
+
}
|
|
359
383
|
}
|
|
360
384
|
const KEPT_NAME_ESCAPES = /%(?:5B|5D|24|3A|40|2C)/g;
|
|
361
385
|
function encodeName(name) {
|
|
362
386
|
return encodeURIComponent(name).replace(KEPT_NAME_ESCAPES, decodeURIComponent);
|
|
363
387
|
}
|
|
388
|
+
function isDate(value) {
|
|
389
|
+
return typeof value?.toISOString === "function";
|
|
390
|
+
}
|
|
391
|
+
function valueText(value) {
|
|
392
|
+
return isDate(value) ? value.toISOString() : value;
|
|
393
|
+
}
|
|
394
|
+
function encodeValue(value, allowReserved) {
|
|
395
|
+
const text = valueText(value);
|
|
396
|
+
return allowReserved === true ? text : encodeURIComponent(text);
|
|
397
|
+
}
|
|
364
398
|
function serializePrimitiveParam(name, value, options) {
|
|
365
399
|
if (value === void 0 || value === null) {
|
|
366
400
|
return "";
|
|
367
401
|
}
|
|
368
|
-
if (typeof value === "object") {
|
|
402
|
+
if (typeof value === "object" && !isDate(value)) {
|
|
369
403
|
throw new Error(
|
|
370
404
|
"Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these."
|
|
371
405
|
);
|
|
372
406
|
}
|
|
373
|
-
return `${name}=${options?.allowReserved
|
|
407
|
+
return `${name}=${encodeValue(value, options?.allowReserved)}`;
|
|
408
|
+
}
|
|
409
|
+
function joinExploded(values, style) {
|
|
410
|
+
const joiner = { simple: ",", label: ".", matrix: ";" }[style] || "&";
|
|
411
|
+
const final = values.join(joiner);
|
|
412
|
+
return style === "label" || style === "matrix" ? `${joiner}${final}` : final;
|
|
413
|
+
}
|
|
414
|
+
function serializeUnexplodedObject(name, value, options) {
|
|
415
|
+
const values = [];
|
|
416
|
+
for (const k in value) {
|
|
417
|
+
values.push(encodeValue(k), encodeValue(value[k], options.allowReserved));
|
|
418
|
+
}
|
|
419
|
+
const final = values.join(",");
|
|
420
|
+
switch (options.style) {
|
|
421
|
+
case "form": {
|
|
422
|
+
return `${name}=${final}`;
|
|
423
|
+
}
|
|
424
|
+
case "label": {
|
|
425
|
+
return `.${final}`;
|
|
426
|
+
}
|
|
427
|
+
case "matrix": {
|
|
428
|
+
return `;${name}=${final}`;
|
|
429
|
+
}
|
|
430
|
+
default: {
|
|
431
|
+
return final;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
374
434
|
}
|
|
375
435
|
function serializeObjectParam(name, value, options) {
|
|
376
|
-
if (!value || typeof value !== "object") {
|
|
436
|
+
if (!value || typeof value !== "object" || !Object.keys(value).length) {
|
|
377
437
|
return "";
|
|
378
438
|
}
|
|
379
|
-
const values = [];
|
|
380
|
-
const joiner = {
|
|
381
|
-
simple: ",",
|
|
382
|
-
label: ".",
|
|
383
|
-
matrix: ";"
|
|
384
|
-
}[options.style] || "&";
|
|
385
439
|
if (options.style !== "deepObject" && options.explode === false) {
|
|
386
|
-
|
|
387
|
-
values.push(
|
|
388
|
-
encodeName(k),
|
|
389
|
-
options.allowReserved === true ? value[k] : encodeURIComponent(value[k])
|
|
390
|
-
);
|
|
391
|
-
}
|
|
392
|
-
const final2 = values.join(",");
|
|
393
|
-
switch (options.style) {
|
|
394
|
-
case "form": {
|
|
395
|
-
return `${name}=${final2}`;
|
|
396
|
-
}
|
|
397
|
-
case "label": {
|
|
398
|
-
return `.${final2}`;
|
|
399
|
-
}
|
|
400
|
-
case "matrix": {
|
|
401
|
-
return `;${name}=${final2}`;
|
|
402
|
-
}
|
|
403
|
-
default: {
|
|
404
|
-
return final2;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
440
|
+
return serializeUnexplodedObject(name, value, options);
|
|
407
441
|
}
|
|
442
|
+
const values = [];
|
|
408
443
|
for (const k in value) {
|
|
409
|
-
const
|
|
410
|
-
|
|
444
|
+
const key = encodeValue(k);
|
|
445
|
+
const finalName = options.style === "deepObject" ? `${name}[${key}]` : key;
|
|
446
|
+
if (value[k] != null) {
|
|
447
|
+
values.push(serializePrimitiveParam(finalName, value[k], options));
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return values.length ? joinExploded(values, options.style) : "";
|
|
451
|
+
}
|
|
452
|
+
function serializeUnexplodedArray(name, value, options) {
|
|
453
|
+
const joiner = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[options.style] || ",";
|
|
454
|
+
const final = value.map((v) => encodeValue(v, options.allowReserved)).join(joiner);
|
|
455
|
+
switch (options.style) {
|
|
456
|
+
case "simple": {
|
|
457
|
+
return final;
|
|
458
|
+
}
|
|
459
|
+
case "label": {
|
|
460
|
+
return `.${final}`;
|
|
461
|
+
}
|
|
462
|
+
case "matrix": {
|
|
463
|
+
return `;${name}=${final}`;
|
|
464
|
+
}
|
|
465
|
+
default: {
|
|
466
|
+
return `${name}=${final}`;
|
|
467
|
+
}
|
|
411
468
|
}
|
|
412
|
-
const final = values.join(joiner);
|
|
413
|
-
return options.style === "label" || options.style === "matrix" ? `${joiner}${final}` : final;
|
|
414
469
|
}
|
|
415
470
|
function serializeArrayParam(name, value, options) {
|
|
416
|
-
if (!Array.isArray(value)) {
|
|
471
|
+
if (!Array.isArray(value) || !value.length) {
|
|
417
472
|
return "";
|
|
418
473
|
}
|
|
419
474
|
if (options.explode === false) {
|
|
420
|
-
|
|
421
|
-
const final = (options.allowReserved === true ? value : value.map((v) => encodeURIComponent(v))).join(joiner2);
|
|
422
|
-
switch (options.style) {
|
|
423
|
-
case "simple": {
|
|
424
|
-
return final;
|
|
425
|
-
}
|
|
426
|
-
case "label": {
|
|
427
|
-
return `.${final}`;
|
|
428
|
-
}
|
|
429
|
-
case "matrix": {
|
|
430
|
-
return `;${name}=${final}`;
|
|
431
|
-
}
|
|
432
|
-
// case "spaceDelimited":
|
|
433
|
-
// case "pipeDelimited":
|
|
434
|
-
default: {
|
|
435
|
-
return `${name}=${final}`;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
475
|
+
return serializeUnexplodedArray(name, value, options);
|
|
438
476
|
}
|
|
439
|
-
const joiner = { simple: ",", label: ".", matrix: ";" }[options.style] || "&";
|
|
440
477
|
const values = [];
|
|
441
478
|
for (const v of value) {
|
|
442
479
|
if (options.style === "simple" || options.style === "label") {
|
|
443
|
-
values.push(options.allowReserved
|
|
444
|
-
} else {
|
|
480
|
+
values.push(encodeValue(v, options.allowReserved));
|
|
481
|
+
} else if (v != null) {
|
|
445
482
|
values.push(serializePrimitiveParam(name, v, options));
|
|
446
483
|
}
|
|
447
484
|
}
|
|
448
|
-
return
|
|
485
|
+
return values.length ? joinExploded(values, options.style) : "";
|
|
486
|
+
}
|
|
487
|
+
function serializeQueryParam(name, value, options) {
|
|
488
|
+
const allowReserved = options?.allowReserved || false;
|
|
489
|
+
if (Array.isArray(value)) {
|
|
490
|
+
return serializeArrayParam(name, value, {
|
|
491
|
+
style: "form",
|
|
492
|
+
explode: true,
|
|
493
|
+
...options?.array,
|
|
494
|
+
allowReserved
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
if (value && typeof value === "object" && !isDate(value)) {
|
|
498
|
+
return serializeObjectParam(name, value, {
|
|
499
|
+
style: "deepObject",
|
|
500
|
+
explode: true,
|
|
501
|
+
...options?.object,
|
|
502
|
+
allowReserved
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
return serializePrimitiveParam(name, value, options);
|
|
449
506
|
}
|
|
450
507
|
function createQuerySerializer(options) {
|
|
451
508
|
return function querySerializer(queryParams) {
|
|
452
509
|
const search = [];
|
|
453
510
|
if (queryParams && typeof queryParams === "object") {
|
|
454
511
|
for (const key in queryParams) {
|
|
455
|
-
const
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
continue;
|
|
459
|
-
}
|
|
460
|
-
if (Array.isArray(value)) {
|
|
461
|
-
if (value.length === 0) {
|
|
462
|
-
continue;
|
|
463
|
-
}
|
|
464
|
-
search.push(
|
|
465
|
-
serializeArrayParam(name, value, {
|
|
466
|
-
style: "form",
|
|
467
|
-
explode: true,
|
|
468
|
-
...options?.array,
|
|
469
|
-
allowReserved: options?.allowReserved || false
|
|
470
|
-
})
|
|
471
|
-
);
|
|
472
|
-
continue;
|
|
473
|
-
}
|
|
474
|
-
if (typeof value === "object") {
|
|
475
|
-
search.push(
|
|
476
|
-
serializeObjectParam(name, value, {
|
|
477
|
-
style: "deepObject",
|
|
478
|
-
explode: true,
|
|
479
|
-
...options?.object,
|
|
480
|
-
allowReserved: options?.allowReserved || false
|
|
481
|
-
})
|
|
482
|
-
);
|
|
483
|
-
continue;
|
|
512
|
+
const part = serializeQueryParam(encodeName(key), queryParams[key], options);
|
|
513
|
+
if (part) {
|
|
514
|
+
search.push(part);
|
|
484
515
|
}
|
|
485
|
-
search.push(serializePrimitiveParam(name, value, options));
|
|
486
516
|
}
|
|
487
517
|
}
|
|
488
518
|
return search.join("&");
|
|
489
519
|
};
|
|
490
520
|
}
|
|
491
521
|
function defaultPathSerializer(pathname, pathParams) {
|
|
522
|
+
const matches = pathname.match(PATH_PARAM_RE);
|
|
523
|
+
if (!matches) {
|
|
524
|
+
return pathname;
|
|
525
|
+
}
|
|
492
526
|
let nextURL = pathname;
|
|
493
|
-
for (const match of
|
|
527
|
+
for (const match of matches) {
|
|
494
528
|
let name = match.substring(1, match.length - 1);
|
|
495
529
|
let explode = false;
|
|
496
530
|
let style = "simple";
|
|
@@ -512,26 +546,49 @@ function defaultPathSerializer(pathname, pathParams) {
|
|
|
512
546
|
let serialized;
|
|
513
547
|
if (Array.isArray(value)) {
|
|
514
548
|
serialized = serializeArrayParam(name, value, { style, explode });
|
|
515
|
-
} else if (typeof value === "object") {
|
|
549
|
+
} else if (typeof value === "object" && !isDate(value)) {
|
|
516
550
|
serialized = serializeObjectParam(name, value, { style, explode });
|
|
517
551
|
} else if (style === "matrix") {
|
|
518
552
|
serialized = `;${serializePrimitiveParam(name, value)}`;
|
|
519
553
|
} else {
|
|
520
|
-
serialized = style === "label" ? `.${
|
|
554
|
+
serialized = style === "label" ? `.${encodeValue(value)}` : encodeValue(value);
|
|
521
555
|
}
|
|
522
556
|
nextURL = nextURL.replace(match, () => serialized);
|
|
523
557
|
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
558
|
+
checkSerializedPath(pathname, nextURL);
|
|
559
|
+
return nextURL;
|
|
560
|
+
}
|
|
561
|
+
function checkSerializedPath(template, url) {
|
|
562
|
+
const fixed = template.slice(0, template.search(PATH_PARAM_RE));
|
|
563
|
+
const path = url.split(QUERY_OR_FRAGMENT_RE, 1)[0];
|
|
564
|
+
const first = fixed.split("/").length - 1;
|
|
565
|
+
let index = path.split("/").findIndex((segment, at) => at >= first && DOT_SEGMENT_RE.test(segment));
|
|
566
|
+
let problem = "Dot";
|
|
567
|
+
if (index === -1 && path.startsWith("//") && !fixed.startsWith("//")) {
|
|
568
|
+
[index, problem] = [1, "Empty first"];
|
|
527
569
|
}
|
|
528
|
-
if (
|
|
529
|
-
|
|
570
|
+
if (index !== -1) {
|
|
571
|
+
const name = template.split(QUERY_OR_FRAGMENT_RE, 1)[0].split("/")[index];
|
|
572
|
+
throw new Error(`${problem} path segment in "${name}"`);
|
|
530
573
|
}
|
|
531
|
-
|
|
574
|
+
}
|
|
575
|
+
function isPlatformInstance(value, name) {
|
|
576
|
+
const type = globalThis[name];
|
|
577
|
+
return typeof type === "function" && value instanceof type;
|
|
578
|
+
}
|
|
579
|
+
function isStreamBody(body) {
|
|
580
|
+
return isPlatformInstance(body, "ReadableStream") || typeof body?.[Symbol.asyncIterator] === "function";
|
|
532
581
|
}
|
|
533
582
|
function isRawBody(body) {
|
|
534
|
-
return
|
|
583
|
+
return ["FormData", "Blob", "URLSearchParams", "ArrayBuffer"].some(
|
|
584
|
+
(name) => isPlatformInstance(body, name)
|
|
585
|
+
) || ArrayBuffer.isView(body) || isStreamBody(body);
|
|
586
|
+
}
|
|
587
|
+
function isNestedFormValue(value) {
|
|
588
|
+
return value && typeof value === "object" && !isDate(value);
|
|
589
|
+
}
|
|
590
|
+
function formItems(value) {
|
|
591
|
+
return Array.isArray(value) ? Array.from(value) : [value];
|
|
535
592
|
}
|
|
536
593
|
function formBody(body) {
|
|
537
594
|
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
@@ -539,25 +596,27 @@ function formBody(body) {
|
|
|
539
596
|
}
|
|
540
597
|
const form = new URLSearchParams();
|
|
541
598
|
for (const [name, value] of Object.entries(body)) {
|
|
542
|
-
|
|
599
|
+
const items = formItems(value);
|
|
600
|
+
if (items.some(isNestedFormValue)) {
|
|
543
601
|
throw new Error(NESTED_FORM_VALUE_MESSAGE);
|
|
544
602
|
}
|
|
545
603
|
if (value != null) {
|
|
546
|
-
form.append(name,
|
|
604
|
+
form.append(name, items.map(valueText).join(","));
|
|
547
605
|
}
|
|
548
606
|
}
|
|
549
607
|
return form.toString();
|
|
550
608
|
}
|
|
551
609
|
function multipartBody(body) {
|
|
552
610
|
const formData = new FormData();
|
|
611
|
+
formData[CLIENT_BUILT_FORM] = true;
|
|
553
612
|
for (const [name, value] of Object.entries(body)) {
|
|
554
|
-
for (const item of
|
|
555
|
-
if (item
|
|
613
|
+
for (const item of formItems(value)) {
|
|
614
|
+
if (isPlatformInstance(item, "Blob")) {
|
|
556
615
|
formData.append(name, item);
|
|
557
|
-
} else if (item
|
|
616
|
+
} else if (isNestedFormValue(item)) {
|
|
558
617
|
throw new Error(NESTED_FORM_VALUE_MESSAGE);
|
|
559
618
|
} else if (item != null) {
|
|
560
|
-
formData.append(name, String(item));
|
|
619
|
+
formData.append(name, String(valueText(item)));
|
|
561
620
|
}
|
|
562
621
|
}
|
|
563
622
|
}
|