openapi-typesafe-fetch 1.0.0-rc.4 → 1.0.0-rc.6
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 +59 -0
- package/README.md +3 -3
- package/dist/helpers.d.cts +35 -12
- package/dist/helpers.d.mts +35 -12
- package/dist/helpers.d.ts +35 -12
- package/dist/index.cjs +137 -53
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -9
- package/dist/index.d.mts +32 -9
- package/dist/index.d.ts +32 -9
- package/dist/index.mjs +137 -53
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/helpers.ts +51 -12
- package/src/index.d.ts +32 -9
- package/src/index.js +247 -72
package/dist/index.cjs
CHANGED
|
@@ -7,11 +7,18 @@ const DOT_SEGMENT_RE = /^\.\.?$/;
|
|
|
7
7
|
const QUERY_OR_FRAGMENT_RE = /[?#]/;
|
|
8
8
|
const JSON_MEDIA_TYPE_RE = /^[^/]+\/([^;]*\+)?json\s*(;|$)/i;
|
|
9
9
|
const TEXT_MEDIA_TYPE_RE = /^text\//i;
|
|
10
|
-
const FORM_MEDIA_TYPE_RE = /^application\/x-www-form-urlencoded(;|$)/i;
|
|
11
|
-
const MULTIPART_MEDIA_TYPE_RE = /^multipart\/form-data(;|$)/i;
|
|
10
|
+
const FORM_MEDIA_TYPE_RE = /^application\/x-www-form-urlencoded[ \t]*(;|$)/i;
|
|
11
|
+
const MULTIPART_MEDIA_TYPE_RE = /^multipart\/form-data[ \t]*(;|$)/i;
|
|
12
12
|
const NESTED_FORM_VALUE_MESSAGE = "Nested form values need your own `bodySerializer()`.";
|
|
13
13
|
const BOUNDARY_PARAMETER_RE = /;\s*boundary=/i;
|
|
14
14
|
const CLIENT_BUILT_FORM = Symbol.for("openapi-typesafe-fetch/form");
|
|
15
|
+
const TYPE_TAG_START = "[object ".length;
|
|
16
|
+
const TYPE_TAG_END = -1;
|
|
17
|
+
const BLOB_CLASSES = ["Blob", "File"];
|
|
18
|
+
const RAW_BODY_CLASSES = ["URLSearchParams", "ArrayBuffer"];
|
|
19
|
+
const FORM_DATA_METHODS = ["append", "entries"];
|
|
20
|
+
const BLOB_METHODS = ["arrayBuffer"];
|
|
21
|
+
const STREAM_METHODS = ["getReader"];
|
|
15
22
|
const supportsRequestInitExt = () => {
|
|
16
23
|
return typeof process === "object" && process.versions?.undici;
|
|
17
24
|
};
|
|
@@ -24,6 +31,7 @@ function createClient(clientOptions) {
|
|
|
24
31
|
let {
|
|
25
32
|
baseUrl = "",
|
|
26
33
|
Request: CustomRequest = globalThis.Request,
|
|
34
|
+
FormData: CustomFormData = globalThis.FormData,
|
|
27
35
|
fetch: baseFetch = globalThis.fetch,
|
|
28
36
|
querySerializer: globalQuerySerializer,
|
|
29
37
|
bodySerializer: globalBodySerializer,
|
|
@@ -56,12 +64,10 @@ function createClient(clientOptions) {
|
|
|
56
64
|
}
|
|
57
65
|
let querySerializer = typeof globalQuerySerializer === "function" ? globalQuerySerializer : createQuerySerializer(globalQuerySerializer);
|
|
58
66
|
if (requestQuerySerializer) {
|
|
59
|
-
querySerializer = typeof requestQuerySerializer === "function" ? requestQuerySerializer : createQuerySerializer(
|
|
60
|
-
...typeof globalQuerySerializer === "object" ? globalQuerySerializer : {},
|
|
61
|
-
...requestQuerySerializer
|
|
62
|
-
});
|
|
67
|
+
querySerializer = typeof requestQuerySerializer === "function" ? requestQuerySerializer : createQuerySerializer(mergeQueryOptions(globalQuerySerializer, requestQuerySerializer));
|
|
63
68
|
}
|
|
64
69
|
const pathSerializer = requestPathSerializer || globalPathSerializer || defaultPathSerializer;
|
|
70
|
+
const headerParams = serializeHeaderParams(params.header);
|
|
65
71
|
const serializedBody = body === void 0 ? void 0 : bodySerializer(
|
|
66
72
|
body,
|
|
67
73
|
// Note: we declare mergeHeaders() both here and below because it’s a bit of a
|
|
@@ -70,29 +76,30 @@ function createClient(clientOptions) {
|
|
|
70
76
|
// content-type header. So we re-merge below, setting the content-type at the very
|
|
71
77
|
// beginning to be overwritten. Lastly, based on the way headers work, it’s not a simple
|
|
72
78
|
// “present-or-not” check because null intentionally un-sets headers.
|
|
73
|
-
mergeHeaders(baseHeaders, headers,
|
|
79
|
+
mergeHeaders(baseHeaders, headers, headerParams)
|
|
74
80
|
);
|
|
81
|
+
const requestBody = isFormData(serializedBody) ? asFormDataOf(CustomFormData, serializedBody) : serializedBody;
|
|
75
82
|
const finalHeaders = mergeHeaders(
|
|
76
83
|
// with no body, we should not to set Content-Type
|
|
77
|
-
|
|
78
|
-
isRawBody(
|
|
84
|
+
requestBody === void 0 || // a raw body (FormData, Blob, ...) gets its Content-Type from the platform, or none
|
|
85
|
+
isRawBody(requestBody) ? {} : {
|
|
79
86
|
"Content-Type": "application/json"
|
|
80
87
|
},
|
|
81
88
|
baseHeaders,
|
|
82
89
|
headers,
|
|
83
|
-
|
|
90
|
+
headerParams
|
|
84
91
|
);
|
|
85
|
-
if (
|
|
86
|
-
dropFormContentType(finalHeaders,
|
|
92
|
+
if (isFormData(requestBody)) {
|
|
93
|
+
dropFormContentType(finalHeaders, requestBody[CLIENT_BUILT_FORM] === true);
|
|
87
94
|
}
|
|
88
95
|
const finalMiddlewares = [...globalMiddlewares, ...requestMiddlewares];
|
|
89
96
|
const requestInit = {
|
|
90
97
|
redirect: "follow",
|
|
91
98
|
// The Fetch standard requires `duplex: "half"` (its only value) for a stream body.
|
|
92
|
-
duplex: isStreamBody(
|
|
99
|
+
duplex: isStreamBody(requestBody) ? "half" : void 0,
|
|
93
100
|
...baseOptions,
|
|
94
101
|
...init,
|
|
95
|
-
body:
|
|
102
|
+
body: requestBody,
|
|
96
103
|
headers: finalHeaders
|
|
97
104
|
};
|
|
98
105
|
let id;
|
|
@@ -169,7 +176,7 @@ function createClient(clientOptions) {
|
|
|
169
176
|
response = result;
|
|
170
177
|
break;
|
|
171
178
|
}
|
|
172
|
-
if (result
|
|
179
|
+
if (isError(result)) {
|
|
173
180
|
errorAfterMiddleware = result;
|
|
174
181
|
continue;
|
|
175
182
|
}
|
|
@@ -209,16 +216,10 @@ function createClient(clientOptions) {
|
|
|
209
216
|
}
|
|
210
217
|
}
|
|
211
218
|
}
|
|
212
|
-
|
|
213
|
-
if (response.status === 204 || request.method === "HEAD" || contentLength === "0" && !response.headers.get("Transfer-Encoding")?.includes("chunked")) {
|
|
214
|
-
return response.ok ? { data: void 0, response } : { error: void 0, response };
|
|
215
|
-
}
|
|
216
|
-
if (response.ok) {
|
|
217
|
-
return { data: await readSuccessBody(response, parseAs), response };
|
|
218
|
-
}
|
|
219
|
-
return { error: await readErrorBody(response), response };
|
|
219
|
+
return readResult(response, parseAs, request.method);
|
|
220
220
|
}
|
|
221
221
|
return {
|
|
222
|
+
baseUrl,
|
|
222
223
|
request(method, url, init) {
|
|
223
224
|
return coreFetch(url, { ...init, method: method.toUpperCase() });
|
|
224
225
|
},
|
|
@@ -364,6 +365,20 @@ function parseJSON(raw, response) {
|
|
|
364
365
|
throw new SyntaxError(`${subject} is not JSON (${raw.length} characters); ${hint}`);
|
|
365
366
|
}
|
|
366
367
|
}
|
|
368
|
+
function hasEmptyBody(response, method) {
|
|
369
|
+
const isDeclaredEmpty = response.headers.get("Content-Length") === "0" && !response.headers.get("Transfer-Encoding")?.includes("chunked");
|
|
370
|
+
return method === "HEAD" || isDeclaredEmpty;
|
|
371
|
+
}
|
|
372
|
+
async function readResult(response, parseAs, method) {
|
|
373
|
+
const isEmpty = hasEmptyBody(response, method);
|
|
374
|
+
if (!response.ok) {
|
|
375
|
+
return { error: isEmpty ? void 0 : await readErrorBody(response), response };
|
|
376
|
+
}
|
|
377
|
+
if (parseAs === "json" && isEmpty) {
|
|
378
|
+
return { data: void 0, response };
|
|
379
|
+
}
|
|
380
|
+
return { data: await readSuccessBody(response, parseAs), response };
|
|
381
|
+
}
|
|
367
382
|
async function readSuccessBody(response, parseAs) {
|
|
368
383
|
if (parseAs === "stream") {
|
|
369
384
|
return response.body;
|
|
@@ -395,7 +410,34 @@ function isDate(value) {
|
|
|
395
410
|
function valueText(value) {
|
|
396
411
|
return isDate(value) ? value.toISOString() : value;
|
|
397
412
|
}
|
|
413
|
+
const HEADER_PARAM_STYLE = { style: "simple", explode: false, allowReserved: true };
|
|
414
|
+
function headerParamValue(value) {
|
|
415
|
+
if (value === null || typeof value !== "object" || isDate(value)) {
|
|
416
|
+
return valueText(value);
|
|
417
|
+
}
|
|
418
|
+
if (Object.values(value).some((member) => isNestedValue(member))) {
|
|
419
|
+
throw new Error(`${NESTED_VALUE_ERROR} in header parameters.`);
|
|
420
|
+
}
|
|
421
|
+
const serialize = Array.isArray(value) ? serializeArrayParam : serializeObjectParam;
|
|
422
|
+
return serialize("", value, HEADER_PARAM_STYLE) || void 0;
|
|
423
|
+
}
|
|
424
|
+
function serializeHeaderParams(header) {
|
|
425
|
+
if (!header || typeof header !== "object") {
|
|
426
|
+
return header;
|
|
427
|
+
}
|
|
428
|
+
const entries = Object.entries(header).map(([name, value]) => [name, headerParamValue(value)]);
|
|
429
|
+
return Object.fromEntries(entries);
|
|
430
|
+
}
|
|
431
|
+
const NESTED_VALUE_ERROR = "Deeply-nested arrays/objects aren\u2019t supported";
|
|
432
|
+
function isNestedValue(value) {
|
|
433
|
+
return Boolean(value) && typeof value === "object" && !isDate(value);
|
|
434
|
+
}
|
|
398
435
|
function encodeValue(value, allowReserved) {
|
|
436
|
+
if (isNestedValue(value)) {
|
|
437
|
+
throw new Error(
|
|
438
|
+
`${NESTED_VALUE_ERROR}. Provide your own \`querySerializer()\` to handle these.`
|
|
439
|
+
);
|
|
440
|
+
}
|
|
399
441
|
const text = valueText(value);
|
|
400
442
|
return allowReserved === true ? text : encodeURIComponent(text);
|
|
401
443
|
}
|
|
@@ -403,11 +445,6 @@ function serializePrimitiveParam(name, value, options) {
|
|
|
403
445
|
if (value === void 0 || value === null) {
|
|
404
446
|
return "";
|
|
405
447
|
}
|
|
406
|
-
if (typeof value === "object" && !isDate(value)) {
|
|
407
|
-
throw new Error(
|
|
408
|
-
"Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these."
|
|
409
|
-
);
|
|
410
|
-
}
|
|
411
448
|
return `${name}=${encodeValue(value, options?.allowReserved)}`;
|
|
412
449
|
}
|
|
413
450
|
function joinExploded(values, style) {
|
|
@@ -418,7 +455,15 @@ function joinExploded(values, style) {
|
|
|
418
455
|
function serializeUnexplodedObject(name, value, options) {
|
|
419
456
|
const values = [];
|
|
420
457
|
for (const k in value) {
|
|
421
|
-
|
|
458
|
+
if (value[k] != null) {
|
|
459
|
+
values.push(
|
|
460
|
+
encodeValue(k, options.allowReserved),
|
|
461
|
+
encodeValue(value[k], options.allowReserved)
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (!values.length) {
|
|
466
|
+
return "";
|
|
422
467
|
}
|
|
423
468
|
const final = values.join(",");
|
|
424
469
|
switch (options.style) {
|
|
@@ -455,7 +500,11 @@ function serializeObjectParam(name, value, options) {
|
|
|
455
500
|
}
|
|
456
501
|
function serializeUnexplodedArray(name, value, options) {
|
|
457
502
|
const joiner = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[options.style] || ",";
|
|
458
|
-
const
|
|
503
|
+
const items = value.filter((v) => v != null);
|
|
504
|
+
if (!items.length) {
|
|
505
|
+
return "";
|
|
506
|
+
}
|
|
507
|
+
const final = items.map((v) => encodeValue(v, options.allowReserved)).join(joiner);
|
|
459
508
|
switch (options.style) {
|
|
460
509
|
case "simple": {
|
|
461
510
|
return final;
|
|
@@ -480,31 +529,29 @@ function serializeArrayParam(name, value, options) {
|
|
|
480
529
|
}
|
|
481
530
|
const values = [];
|
|
482
531
|
for (const v of value) {
|
|
532
|
+
if (v == null) {
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
483
535
|
if (options.style === "simple" || options.style === "label") {
|
|
484
536
|
values.push(encodeValue(v, options.allowReserved));
|
|
485
|
-
} else
|
|
537
|
+
} else {
|
|
486
538
|
values.push(serializePrimitiveParam(name, v, options));
|
|
487
539
|
}
|
|
488
540
|
}
|
|
489
541
|
return values.length ? joinExploded(values, options.style) : "";
|
|
490
542
|
}
|
|
543
|
+
function queryStyle(options, defaultStyle, allowReserved) {
|
|
544
|
+
const style = options?.style ?? defaultStyle;
|
|
545
|
+
return { style, explode: options?.explode ?? style === "form", allowReserved };
|
|
546
|
+
}
|
|
491
547
|
function serializeQueryParam(name, value, options) {
|
|
492
548
|
const allowReserved = options?.allowReserved || false;
|
|
493
549
|
if (Array.isArray(value)) {
|
|
494
|
-
return serializeArrayParam(name, value,
|
|
495
|
-
style: "form",
|
|
496
|
-
explode: true,
|
|
497
|
-
...options?.array,
|
|
498
|
-
allowReserved
|
|
499
|
-
});
|
|
550
|
+
return serializeArrayParam(name, value, queryStyle(options?.array, "form", allowReserved));
|
|
500
551
|
}
|
|
501
552
|
if (value && typeof value === "object" && !isDate(value)) {
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
explode: true,
|
|
505
|
-
...options?.object,
|
|
506
|
-
allowReserved
|
|
507
|
-
});
|
|
553
|
+
const style = queryStyle(options?.object, "deepObject", allowReserved);
|
|
554
|
+
return serializeObjectParam(name, value, style);
|
|
508
555
|
}
|
|
509
556
|
return serializePrimitiveParam(name, value, options);
|
|
510
557
|
}
|
|
@@ -522,6 +569,15 @@ function createQuerySerializer(options) {
|
|
|
522
569
|
return search.join("&");
|
|
523
570
|
};
|
|
524
571
|
}
|
|
572
|
+
function mergeQueryOptions(clientOptions, requestOptions) {
|
|
573
|
+
const base = typeof clientOptions === "function" ? {} : clientOptions ?? {};
|
|
574
|
+
return {
|
|
575
|
+
...base,
|
|
576
|
+
...requestOptions,
|
|
577
|
+
array: { ...base.array, ...requestOptions.array },
|
|
578
|
+
object: { ...base.object, ...requestOptions.object }
|
|
579
|
+
};
|
|
580
|
+
}
|
|
525
581
|
function defaultPathSerializer(pathname, pathParams) {
|
|
526
582
|
const matches = pathname.match(PATH_PARAM_RE);
|
|
527
583
|
if (!matches) {
|
|
@@ -576,17 +632,41 @@ function checkSerializedPath(template, url) {
|
|
|
576
632
|
throw new Error(`${problem} path segment in "${name}"`);
|
|
577
633
|
}
|
|
578
634
|
}
|
|
579
|
-
function
|
|
635
|
+
function typeTag(value) {
|
|
636
|
+
return Object.prototype.toString.call(value).slice(TYPE_TAG_START, TYPE_TAG_END);
|
|
637
|
+
}
|
|
638
|
+
function isClass(value, name, methods = []) {
|
|
580
639
|
const type = globalThis[name];
|
|
581
|
-
return typeof type === "function" && value instanceof type;
|
|
640
|
+
return typeof type === "function" && value instanceof type || typeTag(value) === name && methods.every((method) => typeof value[method] === "function");
|
|
641
|
+
}
|
|
642
|
+
function isError(value) {
|
|
643
|
+
return isClass(value, "Error");
|
|
644
|
+
}
|
|
645
|
+
function isFormData(value) {
|
|
646
|
+
return isClass(value, "FormData", FORM_DATA_METHODS);
|
|
647
|
+
}
|
|
648
|
+
function isBlob(value) {
|
|
649
|
+
return BLOB_CLASSES.some((name) => isClass(value, name, BLOB_METHODS));
|
|
650
|
+
}
|
|
651
|
+
function isReadableStream(value) {
|
|
652
|
+
return isClass(value, "ReadableStream", STREAM_METHODS);
|
|
653
|
+
}
|
|
654
|
+
function asFormDataOf(FormDataClass, form) {
|
|
655
|
+
if (typeof FormDataClass !== "function" || form instanceof FormDataClass) {
|
|
656
|
+
return form;
|
|
657
|
+
}
|
|
658
|
+
const copy = new FormDataClass();
|
|
659
|
+
for (const [name, value] of form.entries()) {
|
|
660
|
+
copy.append(name, value);
|
|
661
|
+
}
|
|
662
|
+
copy[CLIENT_BUILT_FORM] = true;
|
|
663
|
+
return copy;
|
|
582
664
|
}
|
|
583
665
|
function isStreamBody(body) {
|
|
584
|
-
return
|
|
666
|
+
return isReadableStream(body) || typeof body?.[Symbol.asyncIterator] === "function";
|
|
585
667
|
}
|
|
586
668
|
function isRawBody(body) {
|
|
587
|
-
return
|
|
588
|
-
(name) => isPlatformInstance(body, name)
|
|
589
|
-
) || ArrayBuffer.isView(body) || isStreamBody(body);
|
|
669
|
+
return isFormData(body) || isBlob(body) || RAW_BODY_CLASSES.some((name) => isClass(body, name)) || ArrayBuffer.isView(body) || isStreamBody(body);
|
|
590
670
|
}
|
|
591
671
|
function isNestedFormValue(value) {
|
|
592
672
|
return value && typeof value === "object" && !isDate(value);
|
|
@@ -615,7 +695,7 @@ function multipartBody(body) {
|
|
|
615
695
|
formData[CLIENT_BUILT_FORM] = true;
|
|
616
696
|
for (const [name, value] of Object.entries(body)) {
|
|
617
697
|
for (const item of formItems(value)) {
|
|
618
|
-
if (
|
|
698
|
+
if (isBlob(item)) {
|
|
619
699
|
formData.append(name, item);
|
|
620
700
|
} else if (isNestedFormValue(item)) {
|
|
621
701
|
throw new Error(NESTED_FORM_VALUE_MESSAGE);
|
|
@@ -651,10 +731,14 @@ function createFinalURL(pathname, options) {
|
|
|
651
731
|
if (search.startsWith("?")) {
|
|
652
732
|
search = search.substring(1);
|
|
653
733
|
}
|
|
654
|
-
if (search) {
|
|
655
|
-
|
|
734
|
+
if (!search) {
|
|
735
|
+
return finalURL;
|
|
656
736
|
}
|
|
657
|
-
|
|
737
|
+
const hashIndex = finalURL.indexOf("#");
|
|
738
|
+
const fragmentStart = hashIndex === -1 ? finalURL.length : hashIndex;
|
|
739
|
+
const beforeFragment = finalURL.slice(0, fragmentStart);
|
|
740
|
+
const joiner = beforeFragment.includes("?") ? "&" : "?";
|
|
741
|
+
return `${beforeFragment}${joiner}${search}${finalURL.slice(fragmentStart)}`;
|
|
658
742
|
}
|
|
659
743
|
function mergeHeaders(...allHeaders) {
|
|
660
744
|
const finalHeaders = new Headers();
|