@npora/request 1.11.0 → 1.11.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/dist/index.cjs +30 -6
- package/dist/index.js +30 -6
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -69,6 +69,17 @@ function createTimeoutSignal(signal, timeout) {
|
|
|
69
69
|
}
|
|
70
70
|
function noop() {}
|
|
71
71
|
//#endregion
|
|
72
|
+
//#region src/utils/isURLSearchParams.ts
|
|
73
|
+
function isURLSearchParams(value) {
|
|
74
|
+
if (typeof URLSearchParams === "undefined") return false;
|
|
75
|
+
try {
|
|
76
|
+
URLSearchParams.prototype.entries.call(value);
|
|
77
|
+
return true;
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
72
83
|
//#region src/utils/buildRequest.ts
|
|
73
84
|
function buildRequest(config) {
|
|
74
85
|
return buildRequestWithHeaders(config, new Headers(config.headers));
|
|
@@ -144,7 +155,7 @@ function buildBody(config, headers) {
|
|
|
144
155
|
return config.body;
|
|
145
156
|
}
|
|
146
157
|
function buildURLSearchParams(form) {
|
|
147
|
-
if (form
|
|
158
|
+
if (isURLSearchParams(form)) return form;
|
|
148
159
|
const params = new URLSearchParams();
|
|
149
160
|
for (const key in form) if (Object.prototype.hasOwnProperty.call(form, key)) appendQuery(params, key, form[key]);
|
|
150
161
|
return params;
|
|
@@ -610,8 +621,18 @@ const RESPONSE_TYPES = [
|
|
|
610
621
|
"sse",
|
|
611
622
|
"ndjson"
|
|
612
623
|
];
|
|
624
|
+
const HTTP_METHODS = [
|
|
625
|
+
"GET",
|
|
626
|
+
"POST",
|
|
627
|
+
"PUT",
|
|
628
|
+
"PATCH",
|
|
629
|
+
"DELETE",
|
|
630
|
+
"HEAD",
|
|
631
|
+
"OPTIONS"
|
|
632
|
+
];
|
|
613
633
|
function validateRequestConfig(config) {
|
|
614
634
|
validateURL(config);
|
|
635
|
+
validateMethod(config);
|
|
615
636
|
validateTimeout(config);
|
|
616
637
|
validateLimits(config);
|
|
617
638
|
validateQuery(config);
|
|
@@ -623,7 +644,10 @@ function validateRequestConfig(config) {
|
|
|
623
644
|
}
|
|
624
645
|
function validateQuery(config) {
|
|
625
646
|
if (config.query !== void 0 && config.searchParams !== void 0) throw configError("Request query and searchParams are mutually exclusive", config);
|
|
626
|
-
if (config.searchParams !== void 0 && !(config.searchParams
|
|
647
|
+
if (config.searchParams !== void 0 && !isURLSearchParams(config.searchParams)) throw configError("Request searchParams must be URLSearchParams", config);
|
|
648
|
+
}
|
|
649
|
+
function validateMethod(config) {
|
|
650
|
+
if (config.method !== void 0 && !HTTP_METHODS.includes(config.method)) throw configError("Request method is invalid", config);
|
|
627
651
|
}
|
|
628
652
|
function validateResponseOptions(config) {
|
|
629
653
|
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
@@ -997,7 +1021,7 @@ function createRawBody(data, headers, status) {
|
|
|
997
1021
|
return JSON.stringify(data);
|
|
998
1022
|
}
|
|
999
1023
|
function isBodyInit(value) {
|
|
1000
|
-
return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData ||
|
|
1024
|
+
return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData || isURLSearchParams(value) || typeof ReadableStream !== "undefined" && value instanceof ReadableStream || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
|
|
1001
1025
|
}
|
|
1002
1026
|
function normalizeDelay$1(delay) {
|
|
1003
1027
|
if (!Number.isFinite(delay)) return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
|
|
@@ -1062,7 +1086,7 @@ var ConfigMerger = class {
|
|
|
1062
1086
|
static mergeQueryConfig(defaults, config) {
|
|
1063
1087
|
if (config.searchParams !== void 0) return {
|
|
1064
1088
|
query: config.query,
|
|
1065
|
-
searchParams: config.searchParams
|
|
1089
|
+
searchParams: isURLSearchParams(config.searchParams) ? new URLSearchParams(config.searchParams) : config.searchParams
|
|
1066
1090
|
};
|
|
1067
1091
|
if (config.query !== void 0) return {
|
|
1068
1092
|
query: this.mergeObject(defaults.searchParams ? void 0 : defaults.query, config.query),
|
|
@@ -1070,7 +1094,7 @@ var ConfigMerger = class {
|
|
|
1070
1094
|
};
|
|
1071
1095
|
return {
|
|
1072
1096
|
query: defaults.query ? { ...defaults.query } : void 0,
|
|
1073
|
-
searchParams: defaults.searchParams
|
|
1097
|
+
searchParams: isURLSearchParams(defaults.searchParams) ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
|
|
1074
1098
|
};
|
|
1075
1099
|
}
|
|
1076
1100
|
static mergeFetchOptions(defaults, options) {
|
|
@@ -2148,7 +2172,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
|
|
|
2148
2172
|
}
|
|
2149
2173
|
function normalizeQuery(query) {
|
|
2150
2174
|
if (!query) return [];
|
|
2151
|
-
if (query
|
|
2175
|
+
if (isURLSearchParams(query)) return [...query.entries()];
|
|
2152
2176
|
const entries = [];
|
|
2153
2177
|
for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
|
|
2154
2178
|
return entries;
|
package/dist/index.js
CHANGED
|
@@ -68,6 +68,17 @@ function createTimeoutSignal(signal, timeout) {
|
|
|
68
68
|
}
|
|
69
69
|
function noop() {}
|
|
70
70
|
//#endregion
|
|
71
|
+
//#region src/utils/isURLSearchParams.ts
|
|
72
|
+
function isURLSearchParams(value) {
|
|
73
|
+
if (typeof URLSearchParams === "undefined") return false;
|
|
74
|
+
try {
|
|
75
|
+
URLSearchParams.prototype.entries.call(value);
|
|
76
|
+
return true;
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
71
82
|
//#region src/utils/buildRequest.ts
|
|
72
83
|
function buildRequest(config) {
|
|
73
84
|
return buildRequestWithHeaders(config, new Headers(config.headers));
|
|
@@ -143,7 +154,7 @@ function buildBody(config, headers) {
|
|
|
143
154
|
return config.body;
|
|
144
155
|
}
|
|
145
156
|
function buildURLSearchParams(form) {
|
|
146
|
-
if (form
|
|
157
|
+
if (isURLSearchParams(form)) return form;
|
|
147
158
|
const params = new URLSearchParams();
|
|
148
159
|
for (const key in form) if (Object.prototype.hasOwnProperty.call(form, key)) appendQuery(params, key, form[key]);
|
|
149
160
|
return params;
|
|
@@ -609,8 +620,18 @@ const RESPONSE_TYPES = [
|
|
|
609
620
|
"sse",
|
|
610
621
|
"ndjson"
|
|
611
622
|
];
|
|
623
|
+
const HTTP_METHODS = [
|
|
624
|
+
"GET",
|
|
625
|
+
"POST",
|
|
626
|
+
"PUT",
|
|
627
|
+
"PATCH",
|
|
628
|
+
"DELETE",
|
|
629
|
+
"HEAD",
|
|
630
|
+
"OPTIONS"
|
|
631
|
+
];
|
|
612
632
|
function validateRequestConfig(config) {
|
|
613
633
|
validateURL(config);
|
|
634
|
+
validateMethod(config);
|
|
614
635
|
validateTimeout(config);
|
|
615
636
|
validateLimits(config);
|
|
616
637
|
validateQuery(config);
|
|
@@ -622,7 +643,10 @@ function validateRequestConfig(config) {
|
|
|
622
643
|
}
|
|
623
644
|
function validateQuery(config) {
|
|
624
645
|
if (config.query !== void 0 && config.searchParams !== void 0) throw configError("Request query and searchParams are mutually exclusive", config);
|
|
625
|
-
if (config.searchParams !== void 0 && !(config.searchParams
|
|
646
|
+
if (config.searchParams !== void 0 && !isURLSearchParams(config.searchParams)) throw configError("Request searchParams must be URLSearchParams", config);
|
|
647
|
+
}
|
|
648
|
+
function validateMethod(config) {
|
|
649
|
+
if (config.method !== void 0 && !HTTP_METHODS.includes(config.method)) throw configError("Request method is invalid", config);
|
|
626
650
|
}
|
|
627
651
|
function validateResponseOptions(config) {
|
|
628
652
|
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
@@ -996,7 +1020,7 @@ function createRawBody(data, headers, status) {
|
|
|
996
1020
|
return JSON.stringify(data);
|
|
997
1021
|
}
|
|
998
1022
|
function isBodyInit(value) {
|
|
999
|
-
return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData ||
|
|
1023
|
+
return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData || isURLSearchParams(value) || typeof ReadableStream !== "undefined" && value instanceof ReadableStream || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
|
|
1000
1024
|
}
|
|
1001
1025
|
function normalizeDelay$1(delay) {
|
|
1002
1026
|
if (!Number.isFinite(delay)) return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
|
|
@@ -1061,7 +1085,7 @@ var ConfigMerger = class {
|
|
|
1061
1085
|
static mergeQueryConfig(defaults, config) {
|
|
1062
1086
|
if (config.searchParams !== void 0) return {
|
|
1063
1087
|
query: config.query,
|
|
1064
|
-
searchParams: config.searchParams
|
|
1088
|
+
searchParams: isURLSearchParams(config.searchParams) ? new URLSearchParams(config.searchParams) : config.searchParams
|
|
1065
1089
|
};
|
|
1066
1090
|
if (config.query !== void 0) return {
|
|
1067
1091
|
query: this.mergeObject(defaults.searchParams ? void 0 : defaults.query, config.query),
|
|
@@ -1069,7 +1093,7 @@ var ConfigMerger = class {
|
|
|
1069
1093
|
};
|
|
1070
1094
|
return {
|
|
1071
1095
|
query: defaults.query ? { ...defaults.query } : void 0,
|
|
1072
|
-
searchParams: defaults.searchParams
|
|
1096
|
+
searchParams: isURLSearchParams(defaults.searchParams) ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
|
|
1073
1097
|
};
|
|
1074
1098
|
}
|
|
1075
1099
|
static mergeFetchOptions(defaults, options) {
|
|
@@ -2147,7 +2171,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
|
|
|
2147
2171
|
}
|
|
2148
2172
|
function normalizeQuery(query) {
|
|
2149
2173
|
if (!query) return [];
|
|
2150
|
-
if (query
|
|
2174
|
+
if (isURLSearchParams(query)) return [...query.entries()];
|
|
2151
2175
|
const entries = [];
|
|
2152
2176
|
for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
|
|
2153
2177
|
return entries;
|