@npora/request 1.10.2 → 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 +43 -5
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +43 -5
- package/package.json +3 -3
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);
|
|
@@ -601,15 +601,34 @@ const BODY_FIELDS = [
|
|
|
601
601
|
"form",
|
|
602
602
|
"formData"
|
|
603
603
|
];
|
|
604
|
+
const RESPONSE_TYPES = [
|
|
605
|
+
"json",
|
|
606
|
+
"text",
|
|
607
|
+
"blob",
|
|
608
|
+
"arrayBuffer",
|
|
609
|
+
"stream",
|
|
610
|
+
"sse",
|
|
611
|
+
"ndjson"
|
|
612
|
+
];
|
|
604
613
|
function validateRequestConfig(config) {
|
|
605
614
|
validateURL(config);
|
|
606
615
|
validateTimeout(config);
|
|
607
616
|
validateLimits(config);
|
|
617
|
+
validateQuery(config);
|
|
618
|
+
validateResponseOptions(config);
|
|
608
619
|
validateSchema(config);
|
|
609
620
|
const headers = validateHeaders(config);
|
|
610
621
|
validateBody(config);
|
|
611
622
|
return headers;
|
|
612
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
|
+
}
|
|
628
|
+
function validateResponseOptions(config) {
|
|
629
|
+
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
630
|
+
if (config.validateStatus !== void 0 && typeof config.validateStatus !== "function") throw configError("Request validateStatus must be a function", config);
|
|
631
|
+
}
|
|
613
632
|
function validateSchema(config) {
|
|
614
633
|
if (config.schema === void 0) return;
|
|
615
634
|
try {
|
|
@@ -893,7 +912,7 @@ function normalizeMatcher(matcher) {
|
|
|
893
912
|
return matcher;
|
|
894
913
|
}
|
|
895
914
|
function matchesRequest(matcher, config) {
|
|
896
|
-
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);
|
|
897
916
|
}
|
|
898
917
|
function matchesURL(matcher, url) {
|
|
899
918
|
if (typeof matcher === "string") return matcher === url;
|
|
@@ -914,6 +933,10 @@ function normalizeQuery$1(query) {
|
|
|
914
933
|
}
|
|
915
934
|
return JSON.stringify(entries);
|
|
916
935
|
}
|
|
936
|
+
function matchesSearchParams(expected, actual) {
|
|
937
|
+
if (!expected) return true;
|
|
938
|
+
return JSON.stringify([...expected.entries()]) === JSON.stringify([...actual?.entries() ?? []]);
|
|
939
|
+
}
|
|
917
940
|
function matchesHeaders(expected, actual) {
|
|
918
941
|
if (!expected) return true;
|
|
919
942
|
const expectedHeaders = new Headers(expected);
|
|
@@ -1031,11 +1054,25 @@ var ConfigMerger = class {
|
|
|
1031
1054
|
};
|
|
1032
1055
|
if (defaults.fetchOptions || config.fetchOptions) result.fetchOptions = this.mergeFetchOptions(defaults.fetchOptions, config.fetchOptions);
|
|
1033
1056
|
if (defaults.headers || config.headers) result.headers = this.mergeHeaders(defaults.headers, config.headers);
|
|
1034
|
-
if (defaults.query || config.query) result
|
|
1057
|
+
if (defaults.query || config.query || defaults.searchParams || config.searchParams) Object.assign(result, this.mergeQueryConfig(defaults, config));
|
|
1035
1058
|
if (defaults.extensions || config.extensions) result.extensions = this.mergeExtensions(defaults.extensions, config.extensions);
|
|
1036
1059
|
if (this.hasBodyConfig(defaults) || this.hasBodyConfig(config)) Object.assign(result, this.mergeBodyConfig(defaults, config));
|
|
1037
1060
|
return result;
|
|
1038
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
|
+
}
|
|
1039
1076
|
static mergeFetchOptions(defaults, options) {
|
|
1040
1077
|
if (!defaults && !options) return;
|
|
1041
1078
|
return {
|
|
@@ -2091,7 +2128,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
2091
2128
|
method: config.method ?? "GET",
|
|
2092
2129
|
baseURL: config.baseURL,
|
|
2093
2130
|
url: config.url,
|
|
2094
|
-
query: normalizeQuery(config.query),
|
|
2131
|
+
query: normalizeQuery(config.searchParams ?? config.query),
|
|
2095
2132
|
responseType: config.responseType ?? "auto",
|
|
2096
2133
|
headers: normalizeCacheHeaders(headers, varyHeaders)
|
|
2097
2134
|
});
|
|
@@ -2111,6 +2148,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
|
|
|
2111
2148
|
}
|
|
2112
2149
|
function normalizeQuery(query) {
|
|
2113
2150
|
if (!query) return [];
|
|
2151
|
+
if (query instanceof URLSearchParams) return [...query.entries()];
|
|
2114
2152
|
const entries = [];
|
|
2115
2153
|
for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
|
|
2116
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);
|
|
@@ -600,15 +600,34 @@ const BODY_FIELDS = [
|
|
|
600
600
|
"form",
|
|
601
601
|
"formData"
|
|
602
602
|
];
|
|
603
|
+
const RESPONSE_TYPES = [
|
|
604
|
+
"json",
|
|
605
|
+
"text",
|
|
606
|
+
"blob",
|
|
607
|
+
"arrayBuffer",
|
|
608
|
+
"stream",
|
|
609
|
+
"sse",
|
|
610
|
+
"ndjson"
|
|
611
|
+
];
|
|
603
612
|
function validateRequestConfig(config) {
|
|
604
613
|
validateURL(config);
|
|
605
614
|
validateTimeout(config);
|
|
606
615
|
validateLimits(config);
|
|
616
|
+
validateQuery(config);
|
|
617
|
+
validateResponseOptions(config);
|
|
607
618
|
validateSchema(config);
|
|
608
619
|
const headers = validateHeaders(config);
|
|
609
620
|
validateBody(config);
|
|
610
621
|
return headers;
|
|
611
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
|
+
}
|
|
627
|
+
function validateResponseOptions(config) {
|
|
628
|
+
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
629
|
+
if (config.validateStatus !== void 0 && typeof config.validateStatus !== "function") throw configError("Request validateStatus must be a function", config);
|
|
630
|
+
}
|
|
612
631
|
function validateSchema(config) {
|
|
613
632
|
if (config.schema === void 0) return;
|
|
614
633
|
try {
|
|
@@ -892,7 +911,7 @@ function normalizeMatcher(matcher) {
|
|
|
892
911
|
return matcher;
|
|
893
912
|
}
|
|
894
913
|
function matchesRequest(matcher, config) {
|
|
895
|
-
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);
|
|
896
915
|
}
|
|
897
916
|
function matchesURL(matcher, url) {
|
|
898
917
|
if (typeof matcher === "string") return matcher === url;
|
|
@@ -913,6 +932,10 @@ function normalizeQuery$1(query) {
|
|
|
913
932
|
}
|
|
914
933
|
return JSON.stringify(entries);
|
|
915
934
|
}
|
|
935
|
+
function matchesSearchParams(expected, actual) {
|
|
936
|
+
if (!expected) return true;
|
|
937
|
+
return JSON.stringify([...expected.entries()]) === JSON.stringify([...actual?.entries() ?? []]);
|
|
938
|
+
}
|
|
916
939
|
function matchesHeaders(expected, actual) {
|
|
917
940
|
if (!expected) return true;
|
|
918
941
|
const expectedHeaders = new Headers(expected);
|
|
@@ -1030,11 +1053,25 @@ var ConfigMerger = class {
|
|
|
1030
1053
|
};
|
|
1031
1054
|
if (defaults.fetchOptions || config.fetchOptions) result.fetchOptions = this.mergeFetchOptions(defaults.fetchOptions, config.fetchOptions);
|
|
1032
1055
|
if (defaults.headers || config.headers) result.headers = this.mergeHeaders(defaults.headers, config.headers);
|
|
1033
|
-
if (defaults.query || config.query) result
|
|
1056
|
+
if (defaults.query || config.query || defaults.searchParams || config.searchParams) Object.assign(result, this.mergeQueryConfig(defaults, config));
|
|
1034
1057
|
if (defaults.extensions || config.extensions) result.extensions = this.mergeExtensions(defaults.extensions, config.extensions);
|
|
1035
1058
|
if (this.hasBodyConfig(defaults) || this.hasBodyConfig(config)) Object.assign(result, this.mergeBodyConfig(defaults, config));
|
|
1036
1059
|
return result;
|
|
1037
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
|
+
}
|
|
1038
1075
|
static mergeFetchOptions(defaults, options) {
|
|
1039
1076
|
if (!defaults && !options) return;
|
|
1040
1077
|
return {
|
|
@@ -2090,7 +2127,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
2090
2127
|
method: config.method ?? "GET",
|
|
2091
2128
|
baseURL: config.baseURL,
|
|
2092
2129
|
url: config.url,
|
|
2093
|
-
query: normalizeQuery(config.query),
|
|
2130
|
+
query: normalizeQuery(config.searchParams ?? config.query),
|
|
2094
2131
|
responseType: config.responseType ?? "auto",
|
|
2095
2132
|
headers: normalizeCacheHeaders(headers, varyHeaders)
|
|
2096
2133
|
});
|
|
@@ -2110,6 +2147,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
|
|
|
2110
2147
|
}
|
|
2111
2148
|
function normalizeQuery(query) {
|
|
2112
2149
|
if (!query) return [];
|
|
2150
|
+
if (query instanceof URLSearchParams) return [...query.entries()];
|
|
2113
2151
|
const entries = [];
|
|
2114
2152
|
for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
|
|
2115
2153
|
return entries;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@npora/request",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "A modern, TypeScript-first HTTP client powered by the Fetch API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"npora",
|
|
@@ -51,10 +51,10 @@
|
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@changesets/cli": "^2.31.0",
|
|
53
53
|
"@playwright/test": "^1.61.1",
|
|
54
|
-
"@types/node": "^26.
|
|
54
|
+
"@types/node": "^26.2.0",
|
|
55
55
|
"@vitest/coverage-v8": "^4.1.10",
|
|
56
56
|
"tsdown": "^0.22.14",
|
|
57
|
-
"tsx": "^4.23.
|
|
57
|
+
"tsx": "^4.23.11",
|
|
58
58
|
"typescript": "^7.0.2",
|
|
59
59
|
"typescript-api": "npm:@typescript/typescript6@^6.0.2",
|
|
60
60
|
"vitest": "^4.1.10"
|