@npora/request 1.10.3 → 1.11.0
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 +29 -5
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +29 -5
- 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
|
@@ -90,8 +90,8 @@ function buildRequestWithHeaders(config, headers) {
|
|
|
90
90
|
}
|
|
91
91
|
function buildURL(config) {
|
|
92
92
|
const url = joinURL(config.baseURL, config.url);
|
|
93
|
-
if (!config.query) return url;
|
|
94
|
-
const query = stringifyQuery(config.query);
|
|
93
|
+
if (!config.query && !config.searchParams) return url;
|
|
94
|
+
const query = config.searchParams ? config.searchParams.toString() : stringifyQuery(config.query);
|
|
95
95
|
if (!query) return url;
|
|
96
96
|
const hashIndex = url.indexOf("#");
|
|
97
97
|
const target = hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
@@ -614,12 +614,17 @@ function validateRequestConfig(config) {
|
|
|
614
614
|
validateURL(config);
|
|
615
615
|
validateTimeout(config);
|
|
616
616
|
validateLimits(config);
|
|
617
|
+
validateQuery(config);
|
|
617
618
|
validateResponseOptions(config);
|
|
618
619
|
validateSchema(config);
|
|
619
620
|
const headers = validateHeaders(config);
|
|
620
621
|
validateBody(config);
|
|
621
622
|
return headers;
|
|
622
623
|
}
|
|
624
|
+
function validateQuery(config) {
|
|
625
|
+
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 instanceof URLSearchParams)) throw configError("Request searchParams must be URLSearchParams", config);
|
|
627
|
+
}
|
|
623
628
|
function validateResponseOptions(config) {
|
|
624
629
|
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
625
630
|
if (config.validateStatus !== void 0 && typeof config.validateStatus !== "function") throw configError("Request validateStatus must be a function", config);
|
|
@@ -907,7 +912,7 @@ function normalizeMatcher(matcher) {
|
|
|
907
912
|
return matcher;
|
|
908
913
|
}
|
|
909
914
|
function matchesRequest(matcher, config) {
|
|
910
|
-
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesHeaders(matcher.headers, config.headers);
|
|
915
|
+
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesSearchParams(matcher.searchParams, config.searchParams) && matchesHeaders(matcher.headers, config.headers);
|
|
911
916
|
}
|
|
912
917
|
function matchesURL(matcher, url) {
|
|
913
918
|
if (typeof matcher === "string") return matcher === url;
|
|
@@ -928,6 +933,10 @@ function normalizeQuery$1(query) {
|
|
|
928
933
|
}
|
|
929
934
|
return JSON.stringify(entries);
|
|
930
935
|
}
|
|
936
|
+
function matchesSearchParams(expected, actual) {
|
|
937
|
+
if (!expected) return true;
|
|
938
|
+
return JSON.stringify([...expected.entries()]) === JSON.stringify([...actual?.entries() ?? []]);
|
|
939
|
+
}
|
|
931
940
|
function matchesHeaders(expected, actual) {
|
|
932
941
|
if (!expected) return true;
|
|
933
942
|
const expectedHeaders = new Headers(expected);
|
|
@@ -1045,11 +1054,25 @@ var ConfigMerger = class {
|
|
|
1045
1054
|
};
|
|
1046
1055
|
if (defaults.fetchOptions || config.fetchOptions) result.fetchOptions = this.mergeFetchOptions(defaults.fetchOptions, config.fetchOptions);
|
|
1047
1056
|
if (defaults.headers || config.headers) result.headers = this.mergeHeaders(defaults.headers, config.headers);
|
|
1048
|
-
if (defaults.query || config.query) result
|
|
1057
|
+
if (defaults.query || config.query || defaults.searchParams || config.searchParams) Object.assign(result, this.mergeQueryConfig(defaults, config));
|
|
1049
1058
|
if (defaults.extensions || config.extensions) result.extensions = this.mergeExtensions(defaults.extensions, config.extensions);
|
|
1050
1059
|
if (this.hasBodyConfig(defaults) || this.hasBodyConfig(config)) Object.assign(result, this.mergeBodyConfig(defaults, config));
|
|
1051
1060
|
return result;
|
|
1052
1061
|
}
|
|
1062
|
+
static mergeQueryConfig(defaults, config) {
|
|
1063
|
+
if (config.searchParams !== void 0) return {
|
|
1064
|
+
query: config.query,
|
|
1065
|
+
searchParams: config.searchParams instanceof URLSearchParams ? new URLSearchParams(config.searchParams) : config.searchParams
|
|
1066
|
+
};
|
|
1067
|
+
if (config.query !== void 0) return {
|
|
1068
|
+
query: this.mergeObject(defaults.searchParams ? void 0 : defaults.query, config.query),
|
|
1069
|
+
searchParams: void 0
|
|
1070
|
+
};
|
|
1071
|
+
return {
|
|
1072
|
+
query: defaults.query ? { ...defaults.query } : void 0,
|
|
1073
|
+
searchParams: defaults.searchParams instanceof URLSearchParams ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
1053
1076
|
static mergeFetchOptions(defaults, options) {
|
|
1054
1077
|
if (!defaults && !options) return;
|
|
1055
1078
|
return {
|
|
@@ -2105,7 +2128,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
2105
2128
|
method: config.method ?? "GET",
|
|
2106
2129
|
baseURL: config.baseURL,
|
|
2107
2130
|
url: config.url,
|
|
2108
|
-
query: normalizeQuery(config.query),
|
|
2131
|
+
query: normalizeQuery(config.searchParams ?? config.query),
|
|
2109
2132
|
responseType: config.responseType ?? "auto",
|
|
2110
2133
|
headers: normalizeCacheHeaders(headers, varyHeaders)
|
|
2111
2134
|
});
|
|
@@ -2125,6 +2148,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
|
|
|
2125
2148
|
}
|
|
2126
2149
|
function normalizeQuery(query) {
|
|
2127
2150
|
if (!query) return [];
|
|
2151
|
+
if (query instanceof URLSearchParams) return [...query.entries()];
|
|
2128
2152
|
const entries = [];
|
|
2129
2153
|
for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
|
|
2130
2154
|
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
|
@@ -89,8 +89,8 @@ function buildRequestWithHeaders(config, headers) {
|
|
|
89
89
|
}
|
|
90
90
|
function buildURL(config) {
|
|
91
91
|
const url = joinURL(config.baseURL, config.url);
|
|
92
|
-
if (!config.query) return url;
|
|
93
|
-
const query = stringifyQuery(config.query);
|
|
92
|
+
if (!config.query && !config.searchParams) return url;
|
|
93
|
+
const query = config.searchParams ? config.searchParams.toString() : stringifyQuery(config.query);
|
|
94
94
|
if (!query) return url;
|
|
95
95
|
const hashIndex = url.indexOf("#");
|
|
96
96
|
const target = hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
@@ -613,12 +613,17 @@ function validateRequestConfig(config) {
|
|
|
613
613
|
validateURL(config);
|
|
614
614
|
validateTimeout(config);
|
|
615
615
|
validateLimits(config);
|
|
616
|
+
validateQuery(config);
|
|
616
617
|
validateResponseOptions(config);
|
|
617
618
|
validateSchema(config);
|
|
618
619
|
const headers = validateHeaders(config);
|
|
619
620
|
validateBody(config);
|
|
620
621
|
return headers;
|
|
621
622
|
}
|
|
623
|
+
function validateQuery(config) {
|
|
624
|
+
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 instanceof URLSearchParams)) throw configError("Request searchParams must be URLSearchParams", config);
|
|
626
|
+
}
|
|
622
627
|
function validateResponseOptions(config) {
|
|
623
628
|
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
624
629
|
if (config.validateStatus !== void 0 && typeof config.validateStatus !== "function") throw configError("Request validateStatus must be a function", config);
|
|
@@ -906,7 +911,7 @@ function normalizeMatcher(matcher) {
|
|
|
906
911
|
return matcher;
|
|
907
912
|
}
|
|
908
913
|
function matchesRequest(matcher, config) {
|
|
909
|
-
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesHeaders(matcher.headers, config.headers);
|
|
914
|
+
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesSearchParams(matcher.searchParams, config.searchParams) && matchesHeaders(matcher.headers, config.headers);
|
|
910
915
|
}
|
|
911
916
|
function matchesURL(matcher, url) {
|
|
912
917
|
if (typeof matcher === "string") return matcher === url;
|
|
@@ -927,6 +932,10 @@ function normalizeQuery$1(query) {
|
|
|
927
932
|
}
|
|
928
933
|
return JSON.stringify(entries);
|
|
929
934
|
}
|
|
935
|
+
function matchesSearchParams(expected, actual) {
|
|
936
|
+
if (!expected) return true;
|
|
937
|
+
return JSON.stringify([...expected.entries()]) === JSON.stringify([...actual?.entries() ?? []]);
|
|
938
|
+
}
|
|
930
939
|
function matchesHeaders(expected, actual) {
|
|
931
940
|
if (!expected) return true;
|
|
932
941
|
const expectedHeaders = new Headers(expected);
|
|
@@ -1044,11 +1053,25 @@ var ConfigMerger = class {
|
|
|
1044
1053
|
};
|
|
1045
1054
|
if (defaults.fetchOptions || config.fetchOptions) result.fetchOptions = this.mergeFetchOptions(defaults.fetchOptions, config.fetchOptions);
|
|
1046
1055
|
if (defaults.headers || config.headers) result.headers = this.mergeHeaders(defaults.headers, config.headers);
|
|
1047
|
-
if (defaults.query || config.query) result
|
|
1056
|
+
if (defaults.query || config.query || defaults.searchParams || config.searchParams) Object.assign(result, this.mergeQueryConfig(defaults, config));
|
|
1048
1057
|
if (defaults.extensions || config.extensions) result.extensions = this.mergeExtensions(defaults.extensions, config.extensions);
|
|
1049
1058
|
if (this.hasBodyConfig(defaults) || this.hasBodyConfig(config)) Object.assign(result, this.mergeBodyConfig(defaults, config));
|
|
1050
1059
|
return result;
|
|
1051
1060
|
}
|
|
1061
|
+
static mergeQueryConfig(defaults, config) {
|
|
1062
|
+
if (config.searchParams !== void 0) return {
|
|
1063
|
+
query: config.query,
|
|
1064
|
+
searchParams: config.searchParams instanceof URLSearchParams ? new URLSearchParams(config.searchParams) : config.searchParams
|
|
1065
|
+
};
|
|
1066
|
+
if (config.query !== void 0) return {
|
|
1067
|
+
query: this.mergeObject(defaults.searchParams ? void 0 : defaults.query, config.query),
|
|
1068
|
+
searchParams: void 0
|
|
1069
|
+
};
|
|
1070
|
+
return {
|
|
1071
|
+
query: defaults.query ? { ...defaults.query } : void 0,
|
|
1072
|
+
searchParams: defaults.searchParams instanceof URLSearchParams ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1052
1075
|
static mergeFetchOptions(defaults, options) {
|
|
1053
1076
|
if (!defaults && !options) return;
|
|
1054
1077
|
return {
|
|
@@ -2104,7 +2127,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
2104
2127
|
method: config.method ?? "GET",
|
|
2105
2128
|
baseURL: config.baseURL,
|
|
2106
2129
|
url: config.url,
|
|
2107
|
-
query: normalizeQuery(config.query),
|
|
2130
|
+
query: normalizeQuery(config.searchParams ?? config.query),
|
|
2108
2131
|
responseType: config.responseType ?? "auto",
|
|
2109
2132
|
headers: normalizeCacheHeaders(headers, varyHeaders)
|
|
2110
2133
|
});
|
|
@@ -2124,6 +2147,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
|
|
|
2124
2147
|
}
|
|
2125
2148
|
function normalizeQuery(query) {
|
|
2126
2149
|
if (!query) return [];
|
|
2150
|
+
if (query instanceof URLSearchParams) return [...query.entries()];
|
|
2127
2151
|
const entries = [];
|
|
2128
2152
|
for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
|
|
2129
2153
|
return entries;
|