@npora/request 1.10.3 → 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/README.md +10 -3
- package/dist/index.cjs +55 -7
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +55 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,6 +33,8 @@ authentication, and upload/download progress.
|
|
|
33
33
|
## Features
|
|
34
34
|
|
|
35
35
|
- Data-first and complete-response APIs with TypeScript inference.
|
|
36
|
+
- Object query merging plus native ordered `URLSearchParams` through
|
|
37
|
+
`searchParams`.
|
|
36
38
|
- Standard Schema v1 validation and transformation for untrusted responses.
|
|
37
39
|
- Incremental SSE and NDJSON async iterables with cancellation and size limits.
|
|
38
40
|
- Unified errors across Fetch, XHR, parsing, validation, timeouts, and aborts.
|
|
@@ -159,9 +161,11 @@ once; it does not validate individual SSE events or NDJSON records.
|
|
|
159
161
|
|
|
160
162
|
```ts
|
|
161
163
|
await api.post('/users', {
|
|
162
|
-
|
|
163
|
-
notify
|
|
164
|
-
|
|
164
|
+
searchParams: new URLSearchParams([
|
|
165
|
+
['notify', 'true'],
|
|
166
|
+
['tag', 'typescript'],
|
|
167
|
+
['tag', 'fetch']
|
|
168
|
+
]),
|
|
165
169
|
json: {
|
|
166
170
|
name: 'Npora'
|
|
167
171
|
},
|
|
@@ -171,6 +175,9 @@ await api.post('/users', {
|
|
|
171
175
|
})
|
|
172
176
|
```
|
|
173
177
|
|
|
178
|
+
Query objects merge with client defaults. Native `searchParams` replace
|
|
179
|
+
inherited query defaults while preserving repeated keys and order.
|
|
180
|
+
|
|
174
181
|
The body options `body`, `json`, `form`, and `formData` are mutually exclusive.
|
|
175
182
|
`GET` and `HEAD` requests cannot contain a body. Invalid configuration throws a
|
|
176
183
|
`RequestError` before the adapter sends a request.
|
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));
|
|
@@ -90,8 +101,8 @@ function buildRequestWithHeaders(config, headers) {
|
|
|
90
101
|
}
|
|
91
102
|
function buildURL(config) {
|
|
92
103
|
const url = joinURL(config.baseURL, config.url);
|
|
93
|
-
if (!config.query) return url;
|
|
94
|
-
const query = stringifyQuery(config.query);
|
|
104
|
+
if (!config.query && !config.searchParams) return url;
|
|
105
|
+
const query = config.searchParams ? config.searchParams.toString() : stringifyQuery(config.query);
|
|
95
106
|
if (!query) return url;
|
|
96
107
|
const hashIndex = url.indexOf("#");
|
|
97
108
|
const target = hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
@@ -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,16 +621,34 @@ 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);
|
|
638
|
+
validateQuery(config);
|
|
617
639
|
validateResponseOptions(config);
|
|
618
640
|
validateSchema(config);
|
|
619
641
|
const headers = validateHeaders(config);
|
|
620
642
|
validateBody(config);
|
|
621
643
|
return headers;
|
|
622
644
|
}
|
|
645
|
+
function validateQuery(config) {
|
|
646
|
+
if (config.query !== void 0 && config.searchParams !== void 0) throw configError("Request query and searchParams are mutually exclusive", config);
|
|
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);
|
|
651
|
+
}
|
|
623
652
|
function validateResponseOptions(config) {
|
|
624
653
|
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
625
654
|
if (config.validateStatus !== void 0 && typeof config.validateStatus !== "function") throw configError("Request validateStatus must be a function", config);
|
|
@@ -907,7 +936,7 @@ function normalizeMatcher(matcher) {
|
|
|
907
936
|
return matcher;
|
|
908
937
|
}
|
|
909
938
|
function matchesRequest(matcher, config) {
|
|
910
|
-
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesHeaders(matcher.headers, config.headers);
|
|
939
|
+
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesSearchParams(matcher.searchParams, config.searchParams) && matchesHeaders(matcher.headers, config.headers);
|
|
911
940
|
}
|
|
912
941
|
function matchesURL(matcher, url) {
|
|
913
942
|
if (typeof matcher === "string") return matcher === url;
|
|
@@ -928,6 +957,10 @@ function normalizeQuery$1(query) {
|
|
|
928
957
|
}
|
|
929
958
|
return JSON.stringify(entries);
|
|
930
959
|
}
|
|
960
|
+
function matchesSearchParams(expected, actual) {
|
|
961
|
+
if (!expected) return true;
|
|
962
|
+
return JSON.stringify([...expected.entries()]) === JSON.stringify([...actual?.entries() ?? []]);
|
|
963
|
+
}
|
|
931
964
|
function matchesHeaders(expected, actual) {
|
|
932
965
|
if (!expected) return true;
|
|
933
966
|
const expectedHeaders = new Headers(expected);
|
|
@@ -988,7 +1021,7 @@ function createRawBody(data, headers, status) {
|
|
|
988
1021
|
return JSON.stringify(data);
|
|
989
1022
|
}
|
|
990
1023
|
function isBodyInit(value) {
|
|
991
|
-
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);
|
|
992
1025
|
}
|
|
993
1026
|
function normalizeDelay$1(delay) {
|
|
994
1027
|
if (!Number.isFinite(delay)) return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
|
|
@@ -1045,11 +1078,25 @@ var ConfigMerger = class {
|
|
|
1045
1078
|
};
|
|
1046
1079
|
if (defaults.fetchOptions || config.fetchOptions) result.fetchOptions = this.mergeFetchOptions(defaults.fetchOptions, config.fetchOptions);
|
|
1047
1080
|
if (defaults.headers || config.headers) result.headers = this.mergeHeaders(defaults.headers, config.headers);
|
|
1048
|
-
if (defaults.query || config.query) result
|
|
1081
|
+
if (defaults.query || config.query || defaults.searchParams || config.searchParams) Object.assign(result, this.mergeQueryConfig(defaults, config));
|
|
1049
1082
|
if (defaults.extensions || config.extensions) result.extensions = this.mergeExtensions(defaults.extensions, config.extensions);
|
|
1050
1083
|
if (this.hasBodyConfig(defaults) || this.hasBodyConfig(config)) Object.assign(result, this.mergeBodyConfig(defaults, config));
|
|
1051
1084
|
return result;
|
|
1052
1085
|
}
|
|
1086
|
+
static mergeQueryConfig(defaults, config) {
|
|
1087
|
+
if (config.searchParams !== void 0) return {
|
|
1088
|
+
query: config.query,
|
|
1089
|
+
searchParams: isURLSearchParams(config.searchParams) ? new URLSearchParams(config.searchParams) : config.searchParams
|
|
1090
|
+
};
|
|
1091
|
+
if (config.query !== void 0) return {
|
|
1092
|
+
query: this.mergeObject(defaults.searchParams ? void 0 : defaults.query, config.query),
|
|
1093
|
+
searchParams: void 0
|
|
1094
|
+
};
|
|
1095
|
+
return {
|
|
1096
|
+
query: defaults.query ? { ...defaults.query } : void 0,
|
|
1097
|
+
searchParams: isURLSearchParams(defaults.searchParams) ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1053
1100
|
static mergeFetchOptions(defaults, options) {
|
|
1054
1101
|
if (!defaults && !options) return;
|
|
1055
1102
|
return {
|
|
@@ -2105,7 +2152,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
2105
2152
|
method: config.method ?? "GET",
|
|
2106
2153
|
baseURL: config.baseURL,
|
|
2107
2154
|
url: config.url,
|
|
2108
|
-
query: normalizeQuery(config.query),
|
|
2155
|
+
query: normalizeQuery(config.searchParams ?? config.query),
|
|
2109
2156
|
responseType: config.responseType ?? "auto",
|
|
2110
2157
|
headers: normalizeCacheHeaders(headers, varyHeaders)
|
|
2111
2158
|
});
|
|
@@ -2125,6 +2172,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
|
|
|
2125
2172
|
}
|
|
2126
2173
|
function normalizeQuery(query) {
|
|
2127
2174
|
if (!query) return [];
|
|
2175
|
+
if (isURLSearchParams(query)) return [...query.entries()];
|
|
2128
2176
|
const entries = [];
|
|
2129
2177
|
for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
|
|
2130
2178
|
return entries;
|
package/dist/index.d.cts
CHANGED
|
@@ -304,6 +304,7 @@ interface RequestConfig {
|
|
|
304
304
|
fetchOptions?: FetchOptions;
|
|
305
305
|
headers?: HeadersInit;
|
|
306
306
|
query?: QueryParams;
|
|
307
|
+
searchParams?: URLSearchParams;
|
|
307
308
|
body?: BodyInit | Record<string, unknown> | null;
|
|
308
309
|
json?: Record<string, unknown> | unknown[];
|
|
309
310
|
form?: URLSearchParams | Record<string, QueryValue | QueryValue[]>;
|
|
@@ -430,6 +431,7 @@ type MockURLMatcher = string | RegExp;
|
|
|
430
431
|
interface MockRequestMatcher {
|
|
431
432
|
url: MockURLMatcher;
|
|
432
433
|
query?: QueryParams;
|
|
434
|
+
searchParams?: URLSearchParams;
|
|
433
435
|
headers?: HeadersInit;
|
|
434
436
|
}
|
|
435
437
|
interface MockReply<T = unknown> {
|
package/dist/index.d.ts
CHANGED
|
@@ -304,6 +304,7 @@ interface RequestConfig {
|
|
|
304
304
|
fetchOptions?: FetchOptions;
|
|
305
305
|
headers?: HeadersInit;
|
|
306
306
|
query?: QueryParams;
|
|
307
|
+
searchParams?: URLSearchParams;
|
|
307
308
|
body?: BodyInit | Record<string, unknown> | null;
|
|
308
309
|
json?: Record<string, unknown> | unknown[];
|
|
309
310
|
form?: URLSearchParams | Record<string, QueryValue | QueryValue[]>;
|
|
@@ -430,6 +431,7 @@ type MockURLMatcher = string | RegExp;
|
|
|
430
431
|
interface MockRequestMatcher {
|
|
431
432
|
url: MockURLMatcher;
|
|
432
433
|
query?: QueryParams;
|
|
434
|
+
searchParams?: URLSearchParams;
|
|
433
435
|
headers?: HeadersInit;
|
|
434
436
|
}
|
|
435
437
|
interface MockReply<T = unknown> {
|
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));
|
|
@@ -89,8 +100,8 @@ function buildRequestWithHeaders(config, headers) {
|
|
|
89
100
|
}
|
|
90
101
|
function buildURL(config) {
|
|
91
102
|
const url = joinURL(config.baseURL, config.url);
|
|
92
|
-
if (!config.query) return url;
|
|
93
|
-
const query = stringifyQuery(config.query);
|
|
103
|
+
if (!config.query && !config.searchParams) return url;
|
|
104
|
+
const query = config.searchParams ? config.searchParams.toString() : stringifyQuery(config.query);
|
|
94
105
|
if (!query) return url;
|
|
95
106
|
const hashIndex = url.indexOf("#");
|
|
96
107
|
const target = hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
@@ -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,16 +620,34 @@ 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);
|
|
637
|
+
validateQuery(config);
|
|
616
638
|
validateResponseOptions(config);
|
|
617
639
|
validateSchema(config);
|
|
618
640
|
const headers = validateHeaders(config);
|
|
619
641
|
validateBody(config);
|
|
620
642
|
return headers;
|
|
621
643
|
}
|
|
644
|
+
function validateQuery(config) {
|
|
645
|
+
if (config.query !== void 0 && config.searchParams !== void 0) throw configError("Request query and searchParams are mutually exclusive", config);
|
|
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);
|
|
650
|
+
}
|
|
622
651
|
function validateResponseOptions(config) {
|
|
623
652
|
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
624
653
|
if (config.validateStatus !== void 0 && typeof config.validateStatus !== "function") throw configError("Request validateStatus must be a function", config);
|
|
@@ -906,7 +935,7 @@ function normalizeMatcher(matcher) {
|
|
|
906
935
|
return matcher;
|
|
907
936
|
}
|
|
908
937
|
function matchesRequest(matcher, config) {
|
|
909
|
-
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesHeaders(matcher.headers, config.headers);
|
|
938
|
+
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesSearchParams(matcher.searchParams, config.searchParams) && matchesHeaders(matcher.headers, config.headers);
|
|
910
939
|
}
|
|
911
940
|
function matchesURL(matcher, url) {
|
|
912
941
|
if (typeof matcher === "string") return matcher === url;
|
|
@@ -927,6 +956,10 @@ function normalizeQuery$1(query) {
|
|
|
927
956
|
}
|
|
928
957
|
return JSON.stringify(entries);
|
|
929
958
|
}
|
|
959
|
+
function matchesSearchParams(expected, actual) {
|
|
960
|
+
if (!expected) return true;
|
|
961
|
+
return JSON.stringify([...expected.entries()]) === JSON.stringify([...actual?.entries() ?? []]);
|
|
962
|
+
}
|
|
930
963
|
function matchesHeaders(expected, actual) {
|
|
931
964
|
if (!expected) return true;
|
|
932
965
|
const expectedHeaders = new Headers(expected);
|
|
@@ -987,7 +1020,7 @@ function createRawBody(data, headers, status) {
|
|
|
987
1020
|
return JSON.stringify(data);
|
|
988
1021
|
}
|
|
989
1022
|
function isBodyInit(value) {
|
|
990
|
-
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);
|
|
991
1024
|
}
|
|
992
1025
|
function normalizeDelay$1(delay) {
|
|
993
1026
|
if (!Number.isFinite(delay)) return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
|
|
@@ -1044,11 +1077,25 @@ var ConfigMerger = class {
|
|
|
1044
1077
|
};
|
|
1045
1078
|
if (defaults.fetchOptions || config.fetchOptions) result.fetchOptions = this.mergeFetchOptions(defaults.fetchOptions, config.fetchOptions);
|
|
1046
1079
|
if (defaults.headers || config.headers) result.headers = this.mergeHeaders(defaults.headers, config.headers);
|
|
1047
|
-
if (defaults.query || config.query) result
|
|
1080
|
+
if (defaults.query || config.query || defaults.searchParams || config.searchParams) Object.assign(result, this.mergeQueryConfig(defaults, config));
|
|
1048
1081
|
if (defaults.extensions || config.extensions) result.extensions = this.mergeExtensions(defaults.extensions, config.extensions);
|
|
1049
1082
|
if (this.hasBodyConfig(defaults) || this.hasBodyConfig(config)) Object.assign(result, this.mergeBodyConfig(defaults, config));
|
|
1050
1083
|
return result;
|
|
1051
1084
|
}
|
|
1085
|
+
static mergeQueryConfig(defaults, config) {
|
|
1086
|
+
if (config.searchParams !== void 0) return {
|
|
1087
|
+
query: config.query,
|
|
1088
|
+
searchParams: isURLSearchParams(config.searchParams) ? new URLSearchParams(config.searchParams) : config.searchParams
|
|
1089
|
+
};
|
|
1090
|
+
if (config.query !== void 0) return {
|
|
1091
|
+
query: this.mergeObject(defaults.searchParams ? void 0 : defaults.query, config.query),
|
|
1092
|
+
searchParams: void 0
|
|
1093
|
+
};
|
|
1094
|
+
return {
|
|
1095
|
+
query: defaults.query ? { ...defaults.query } : void 0,
|
|
1096
|
+
searchParams: isURLSearchParams(defaults.searchParams) ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1052
1099
|
static mergeFetchOptions(defaults, options) {
|
|
1053
1100
|
if (!defaults && !options) return;
|
|
1054
1101
|
return {
|
|
@@ -2104,7 +2151,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
2104
2151
|
method: config.method ?? "GET",
|
|
2105
2152
|
baseURL: config.baseURL,
|
|
2106
2153
|
url: config.url,
|
|
2107
|
-
query: normalizeQuery(config.query),
|
|
2154
|
+
query: normalizeQuery(config.searchParams ?? config.query),
|
|
2108
2155
|
responseType: config.responseType ?? "auto",
|
|
2109
2156
|
headers: normalizeCacheHeaders(headers, varyHeaders)
|
|
2110
2157
|
});
|
|
@@ -2124,6 +2171,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
|
|
|
2124
2171
|
}
|
|
2125
2172
|
function normalizeQuery(query) {
|
|
2126
2173
|
if (!query) return [];
|
|
2174
|
+
if (isURLSearchParams(query)) return [...query.entries()];
|
|
2127
2175
|
const entries = [];
|
|
2128
2176
|
for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
|
|
2129
2177
|
return entries;
|