@npora/request 1.10.1 → 1.10.3
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 +6 -0
- package/dist/index.cjs +72 -155
- package/dist/index.js +72 -155
- package/package.json +11 -4
package/README.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @npora/request
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@npora/request)
|
|
4
|
+
[](https://github.com/npora/request/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/npora/request/actions/workflows/security.yml)
|
|
6
|
+
[](package.json)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
3
9
|
A production-focused, TypeScript-first HTTP client built on the standard Fetch
|
|
4
10
|
API, with zero runtime dependencies.
|
|
5
11
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/errors/RequestError.ts
|
|
3
|
-
/**
|
|
4
|
-
* Unified public error for all request failures.
|
|
5
|
-
*/
|
|
6
3
|
var RequestError = class extends Error {
|
|
7
4
|
constructor(message, options) {
|
|
8
5
|
super(message);
|
|
@@ -18,9 +15,6 @@ var RequestError = class extends Error {
|
|
|
18
15
|
};
|
|
19
16
|
//#endregion
|
|
20
17
|
//#region src/errors/SchemaValidationError.ts
|
|
21
|
-
/**
|
|
22
|
-
* Response failure reported by a Standard Schema compatible validator.
|
|
23
|
-
*/
|
|
24
18
|
var SchemaValidationError = class extends RequestError {
|
|
25
19
|
constructor(message, response, schemaVendor, issues = [], cause) {
|
|
26
20
|
super(message, {
|
|
@@ -76,15 +70,9 @@ function createTimeoutSignal(signal, timeout) {
|
|
|
76
70
|
function noop() {}
|
|
77
71
|
//#endregion
|
|
78
72
|
//#region src/utils/buildRequest.ts
|
|
79
|
-
/**
|
|
80
|
-
* Build a Fetch-compatible request from RequestConfig.
|
|
81
|
-
*/
|
|
82
73
|
function buildRequest(config) {
|
|
83
74
|
return buildRequestWithHeaders(config, new Headers(config.headers));
|
|
84
75
|
}
|
|
85
|
-
/**
|
|
86
|
-
* Internal fast path for a Headers instance already created by validation.
|
|
87
|
-
*/
|
|
88
76
|
function buildRequestWithHeaders(config, headers) {
|
|
89
77
|
const body = buildBody(config, headers);
|
|
90
78
|
const timeoutSignal = createTimeoutSignal(config.signal, config.timeout);
|
|
@@ -164,7 +152,7 @@ function buildURLSearchParams(form) {
|
|
|
164
152
|
function buildFormData(input, maxDepth = 32) {
|
|
165
153
|
if (input instanceof FormData) return input;
|
|
166
154
|
const formData = new FormData();
|
|
167
|
-
const ancestors =
|
|
155
|
+
const ancestors = new WeakSet();
|
|
168
156
|
for (const key in input) if (Object.prototype.hasOwnProperty.call(input, key)) appendFormDataValue(formData, key, input[key], 0, maxDepth, ancestors);
|
|
169
157
|
return formData;
|
|
170
158
|
}
|
|
@@ -196,19 +184,12 @@ function isPlainObject$1(value) {
|
|
|
196
184
|
}
|
|
197
185
|
//#endregion
|
|
198
186
|
//#region src/utils/parseStreamingResponse.ts
|
|
199
|
-
/**
|
|
200
|
-
* Decode a server-sent event response without buffering the complete body.
|
|
201
|
-
*/
|
|
202
187
|
function parseServerSentEvents(response, config) {
|
|
203
188
|
return decodeServerSentEvents(response.body, {
|
|
204
189
|
config,
|
|
205
190
|
response
|
|
206
191
|
});
|
|
207
192
|
}
|
|
208
|
-
/**
|
|
209
|
-
* Decode a newline-delimited JSON response without buffering the complete
|
|
210
|
-
* body.
|
|
211
|
-
*/
|
|
212
193
|
function parseNdjson(response, config) {
|
|
213
194
|
return decodeNdjson(response.body, {
|
|
214
195
|
config,
|
|
@@ -330,9 +311,6 @@ function parserError(message, context, cause) {
|
|
|
330
311
|
}
|
|
331
312
|
//#endregion
|
|
332
313
|
//#region src/utils/parseResponse.ts
|
|
333
|
-
/**
|
|
334
|
-
* Bound a Fetch response before it can be cloned into multiple body branches.
|
|
335
|
-
*/
|
|
336
314
|
function limitResponseSize(response, config) {
|
|
337
315
|
const maxSize = config.maxResponseSize;
|
|
338
316
|
if (maxSize === void 0 || !Number.isFinite(maxSize) || !response.body) return response;
|
|
@@ -350,10 +328,6 @@ function limitResponseSize(response, config) {
|
|
|
350
328
|
copyResponseMetadata(response, limited);
|
|
351
329
|
return limited;
|
|
352
330
|
}
|
|
353
|
-
/**
|
|
354
|
-
* Keep request cancellation resources alive until a streaming response body
|
|
355
|
-
* settles instead of releasing them as soon as response headers arrive.
|
|
356
|
-
*/
|
|
357
331
|
function finalizeStreamingResponse(response, signal, config, finalize) {
|
|
358
332
|
if (!response.body) {
|
|
359
333
|
finalize();
|
|
@@ -426,11 +400,8 @@ function finalizeStreamingResponse(response, signal, config, finalize) {
|
|
|
426
400
|
copyResponseMetadata(response, wrapped);
|
|
427
401
|
return wrapped;
|
|
428
402
|
}
|
|
429
|
-
/**
|
|
430
|
-
* Parse a Fetch response according to the request configuration.
|
|
431
|
-
*/
|
|
432
403
|
async function parseResponse(response, config) {
|
|
433
|
-
if (config.method === "HEAD" || response.status === 204 || response.status === 205) return;
|
|
404
|
+
if (config.method === "HEAD" || response.status === 204 || response.status === 205 || response.status === 304) return;
|
|
434
405
|
const responseType = resolveResponseType(response, config);
|
|
435
406
|
const maxSize = config.maxResponseSize;
|
|
436
407
|
try {
|
|
@@ -470,7 +441,7 @@ async function rejectOversizedContentLength(response, maxSize, config) {
|
|
|
470
441
|
throw error;
|
|
471
442
|
}
|
|
472
443
|
async function readLimitedBody(response, maxSize, config) {
|
|
473
|
-
if (!response.body) return
|
|
444
|
+
if (!response.body) return new Uint8Array();
|
|
474
445
|
const reader = response.body.getReader();
|
|
475
446
|
const chunks = [];
|
|
476
447
|
let size = 0;
|
|
@@ -630,18 +601,29 @@ const BODY_FIELDS = [
|
|
|
630
601
|
"form",
|
|
631
602
|
"formData"
|
|
632
603
|
];
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
604
|
+
const RESPONSE_TYPES = [
|
|
605
|
+
"json",
|
|
606
|
+
"text",
|
|
607
|
+
"blob",
|
|
608
|
+
"arrayBuffer",
|
|
609
|
+
"stream",
|
|
610
|
+
"sse",
|
|
611
|
+
"ndjson"
|
|
612
|
+
];
|
|
636
613
|
function validateRequestConfig(config) {
|
|
637
614
|
validateURL(config);
|
|
638
615
|
validateTimeout(config);
|
|
639
616
|
validateLimits(config);
|
|
617
|
+
validateResponseOptions(config);
|
|
640
618
|
validateSchema(config);
|
|
641
619
|
const headers = validateHeaders(config);
|
|
642
620
|
validateBody(config);
|
|
643
621
|
return headers;
|
|
644
622
|
}
|
|
623
|
+
function validateResponseOptions(config) {
|
|
624
|
+
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
625
|
+
if (config.validateStatus !== void 0 && typeof config.validateStatus !== "function") throw configError("Request validateStatus must be a function", config);
|
|
626
|
+
}
|
|
645
627
|
function validateSchema(config) {
|
|
646
628
|
if (config.schema === void 0) return;
|
|
647
629
|
try {
|
|
@@ -697,6 +679,20 @@ function configError(message, config, cause) {
|
|
|
697
679
|
});
|
|
698
680
|
}
|
|
699
681
|
//#endregion
|
|
682
|
+
//#region src/utils/validateResponseStatus.ts
|
|
683
|
+
function validateResponseStatus(status, config) {
|
|
684
|
+
try {
|
|
685
|
+
return config.validateStatus ? config.validateStatus(status) : status >= 200 && status < 300;
|
|
686
|
+
} catch (error) {
|
|
687
|
+
throw new RequestError("Request validateStatus callback failed", {
|
|
688
|
+
code: "CONFIG_ERROR",
|
|
689
|
+
status,
|
|
690
|
+
config,
|
|
691
|
+
cause: error
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
//#endregion
|
|
700
696
|
//#region src/adapters/FetchAdapter.ts
|
|
701
697
|
var FetchAdapter = class {
|
|
702
698
|
async request(config) {
|
|
@@ -712,7 +708,7 @@ var FetchAdapter = class {
|
|
|
712
708
|
try {
|
|
713
709
|
request = buildRequestWithHeaders(config, headers);
|
|
714
710
|
let response = limitResponseSize(await fetch(request.url, request.init), config);
|
|
715
|
-
const validStatus = (
|
|
711
|
+
const validStatus = validateResponseStatus(response.status, config);
|
|
716
712
|
const streaming = isStreamingResponseType(resolveResponseType(response, config));
|
|
717
713
|
if (streaming) {
|
|
718
714
|
response = finalizeStreamingResponse(response, request.init.signal, config, request.clear);
|
|
@@ -766,14 +762,11 @@ var FetchAdapter = class {
|
|
|
766
762
|
}
|
|
767
763
|
}
|
|
768
764
|
};
|
|
769
|
-
function defaultValidateStatus$2(status) {
|
|
770
|
-
return status >= 200 && status < 300;
|
|
771
|
-
}
|
|
772
765
|
//#endregion
|
|
773
766
|
//#region src/adapters/MockAdapter.ts
|
|
774
767
|
var MockAdapter = class {
|
|
775
768
|
constructor(options = {}) {
|
|
776
|
-
this.handlers =
|
|
769
|
+
this.handlers = new Map();
|
|
777
770
|
this.rules = [];
|
|
778
771
|
this.requests = [];
|
|
779
772
|
this.delay = normalizeDelay$1(options.delay);
|
|
@@ -784,9 +777,6 @@ var MockAdapter = class {
|
|
|
784
777
|
get history() {
|
|
785
778
|
return [...this.requests];
|
|
786
779
|
}
|
|
787
|
-
/**
|
|
788
|
-
* Register a legacy URL-only data handler.
|
|
789
|
-
*/
|
|
790
780
|
on(url, handler) {
|
|
791
781
|
this.handlers.set(url, handler);
|
|
792
782
|
return this;
|
|
@@ -964,15 +954,12 @@ function createMockResponse(config, reply) {
|
|
|
964
954
|
};
|
|
965
955
|
}
|
|
966
956
|
function validateMockResponse(response, config) {
|
|
967
|
-
if (!(
|
|
957
|
+
if (!validateResponseStatus(response.status, config)) throw new RequestError(response.statusText || "Mock request failed", {
|
|
968
958
|
code: "HTTP_ERROR",
|
|
969
959
|
response
|
|
970
960
|
});
|
|
971
961
|
return response;
|
|
972
962
|
}
|
|
973
|
-
function defaultValidateStatus$1(status) {
|
|
974
|
-
return status >= 200 && status < 300;
|
|
975
|
-
}
|
|
976
963
|
function defaultStatusText(status) {
|
|
977
964
|
return STATUS_TEXT[status] ?? "";
|
|
978
965
|
}
|
|
@@ -1044,22 +1031,10 @@ function createAbortError$3(reason, config) {
|
|
|
1044
1031
|
}
|
|
1045
1032
|
//#endregion
|
|
1046
1033
|
//#region src/core/ConfigMerger.ts
|
|
1047
|
-
/**
|
|
1048
|
-
* Request configuration merger.
|
|
1049
|
-
*
|
|
1050
|
-
* Responsible for merging client defaults
|
|
1051
|
-
* and request specific configuration.
|
|
1052
|
-
*/
|
|
1053
1034
|
var ConfigMerger = class {
|
|
1054
|
-
/**
|
|
1055
|
-
* Merge request configuration.
|
|
1056
|
-
*/
|
|
1057
1035
|
static merge(defaults, config) {
|
|
1058
1036
|
return this.mergeConfig(defaults, config);
|
|
1059
1037
|
}
|
|
1060
|
-
/**
|
|
1061
|
-
* Merge two sets of client defaults.
|
|
1062
|
-
*/
|
|
1063
1038
|
static mergeDefaults(defaults, overrides) {
|
|
1064
1039
|
return this.mergeConfig(defaults, overrides);
|
|
1065
1040
|
}
|
|
@@ -1082,9 +1057,6 @@ var ConfigMerger = class {
|
|
|
1082
1057
|
...options
|
|
1083
1058
|
};
|
|
1084
1059
|
}
|
|
1085
|
-
/**
|
|
1086
|
-
* Merge request headers.
|
|
1087
|
-
*/
|
|
1088
1060
|
static mergeHeaders(defaults, headers) {
|
|
1089
1061
|
if (!defaults && !headers) return;
|
|
1090
1062
|
const result = {};
|
|
@@ -1181,11 +1153,6 @@ var RequestContext = class {
|
|
|
1181
1153
|
};
|
|
1182
1154
|
//#endregion
|
|
1183
1155
|
//#region src/core/Pipeline.ts
|
|
1184
|
-
/**
|
|
1185
|
-
* Coordinates the request lifecycle.
|
|
1186
|
-
*
|
|
1187
|
-
* Pipeline does not implement business features.
|
|
1188
|
-
*/
|
|
1189
1156
|
var Pipeline = class {
|
|
1190
1157
|
constructor(adapter, interceptors, hooks) {
|
|
1191
1158
|
this.adapter = adapter;
|
|
@@ -1264,13 +1231,13 @@ var Pipeline = class {
|
|
|
1264
1231
|
} catch (error) {
|
|
1265
1232
|
throw new SchemaValidationError("Response schema validator failed", response, schemaVendor, [], error);
|
|
1266
1233
|
}
|
|
1267
|
-
if (typeof result !== "object" || result === null) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [],
|
|
1234
|
+
if (typeof result !== "object" || result === null) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [], new TypeError("Expected a Standard Schema result"));
|
|
1268
1235
|
const issues = "issues" in result ? result.issues : void 0;
|
|
1269
1236
|
if (issues !== void 0) {
|
|
1270
|
-
if (!Array.isArray(issues)) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [],
|
|
1237
|
+
if (!Array.isArray(issues)) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [], new TypeError("Expected Standard Schema issues to be an array"));
|
|
1271
1238
|
throw new SchemaValidationError("Response schema validation failed", response, schemaVendor, issues);
|
|
1272
1239
|
}
|
|
1273
|
-
if (!("value" in result)) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [],
|
|
1240
|
+
if (!("value" in result)) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [], new TypeError("Expected a Standard Schema value"));
|
|
1274
1241
|
context.response = {
|
|
1275
1242
|
...response,
|
|
1276
1243
|
data: result.value
|
|
@@ -1334,7 +1301,7 @@ function createAbortError$2(reason, config) {
|
|
|
1334
1301
|
var InterceptorManager = class {
|
|
1335
1302
|
constructor() {
|
|
1336
1303
|
this.id = 0;
|
|
1337
|
-
this.interceptors =
|
|
1304
|
+
this.interceptors = new Map();
|
|
1338
1305
|
this.orderedInterceptors = [];
|
|
1339
1306
|
}
|
|
1340
1307
|
get active() {
|
|
@@ -1373,12 +1340,6 @@ function normalizePriority$1(priority) {
|
|
|
1373
1340
|
}
|
|
1374
1341
|
//#endregion
|
|
1375
1342
|
//#region src/interceptors/PluginHooks.ts
|
|
1376
|
-
/**
|
|
1377
|
-
* Internal plugin lifecycle manager.
|
|
1378
|
-
*
|
|
1379
|
-
* Hook order is calculated when registrations change so the hot request path
|
|
1380
|
-
* can iterate a stable array without allocating and sorting per request.
|
|
1381
|
-
*/
|
|
1382
1343
|
var PluginHooks = class {
|
|
1383
1344
|
constructor() {
|
|
1384
1345
|
this.requestHooks = new HookRegistry();
|
|
@@ -1445,7 +1406,7 @@ var PluginHooks = class {
|
|
|
1445
1406
|
var HookRegistry = class {
|
|
1446
1407
|
constructor() {
|
|
1447
1408
|
this.id = 0;
|
|
1448
|
-
this.hooks =
|
|
1409
|
+
this.hooks = new Map();
|
|
1449
1410
|
this.orderedHooks = [];
|
|
1450
1411
|
}
|
|
1451
1412
|
get active() {
|
|
@@ -1477,9 +1438,6 @@ function normalizePriority(priority) {
|
|
|
1477
1438
|
}
|
|
1478
1439
|
//#endregion
|
|
1479
1440
|
//#region src/plugins/PluginError.ts
|
|
1480
|
-
/**
|
|
1481
|
-
* Stable error thrown for invalid plugin lifecycle operations.
|
|
1482
|
-
*/
|
|
1483
1441
|
var PluginError = class extends Error {
|
|
1484
1442
|
constructor(message, options) {
|
|
1485
1443
|
super(message);
|
|
@@ -1492,9 +1450,6 @@ var PluginError = class extends Error {
|
|
|
1492
1450
|
};
|
|
1493
1451
|
//#endregion
|
|
1494
1452
|
//#region src/plugins/PluginScope.ts
|
|
1495
|
-
/**
|
|
1496
|
-
* Create an isolated registration scope for one plugin.
|
|
1497
|
-
*/
|
|
1498
1453
|
function createPluginScope(interceptors, hooks, priority = 0) {
|
|
1499
1454
|
const cleanups = [];
|
|
1500
1455
|
return {
|
|
@@ -1522,7 +1477,7 @@ function createPluginScope(interceptors, hooks, priority = 0) {
|
|
|
1522
1477
|
};
|
|
1523
1478
|
}
|
|
1524
1479
|
function createScopedInterceptor(manager, pluginPriority, cleanups) {
|
|
1525
|
-
const ids =
|
|
1480
|
+
const ids = new Set();
|
|
1526
1481
|
cleanups.push(() => {
|
|
1527
1482
|
for (const id of ids) manager.eject(id);
|
|
1528
1483
|
ids.clear();
|
|
@@ -1578,7 +1533,7 @@ function once(cleanup) {
|
|
|
1578
1533
|
//#region src/client/Client.ts
|
|
1579
1534
|
var Client = class Client {
|
|
1580
1535
|
constructor(options = {}) {
|
|
1581
|
-
this.installedPlugins =
|
|
1536
|
+
this.installedPlugins = new Map();
|
|
1582
1537
|
this.hooks = new PluginHooks();
|
|
1583
1538
|
this.interceptors = {
|
|
1584
1539
|
request: new InterceptorManager(),
|
|
@@ -1590,9 +1545,6 @@ var Client = class Client {
|
|
|
1590
1545
|
this.adapter = adapter;
|
|
1591
1546
|
this.pipeline = this.createPipeline(adapter);
|
|
1592
1547
|
}
|
|
1593
|
-
/**
|
|
1594
|
-
* Create an isolated client that inherits this client's defaults.
|
|
1595
|
-
*/
|
|
1596
1548
|
extend(options = {}) {
|
|
1597
1549
|
const { adapter = this.adapter, ...overrides } = options;
|
|
1598
1550
|
const defaults = ConfigMerger.mergeDefaults(this.defaults, overrides);
|
|
@@ -1745,36 +1697,24 @@ var Client = class Client {
|
|
|
1745
1697
|
method: "OPTIONS"
|
|
1746
1698
|
});
|
|
1747
1699
|
}
|
|
1748
|
-
/**
|
|
1749
|
-
* Consume a server-sent event response as a lazy async iterable.
|
|
1750
|
-
*/
|
|
1751
1700
|
sse(url, config = {}) {
|
|
1752
1701
|
return this.get(url, {
|
|
1753
1702
|
...config,
|
|
1754
1703
|
responseType: "sse"
|
|
1755
1704
|
});
|
|
1756
1705
|
}
|
|
1757
|
-
/**
|
|
1758
|
-
* Consume a server-sent event response with complete response metadata.
|
|
1759
|
-
*/
|
|
1760
1706
|
sseResponse(url, config = {}) {
|
|
1761
1707
|
return this.getResponse(url, {
|
|
1762
1708
|
...config,
|
|
1763
1709
|
responseType: "sse"
|
|
1764
1710
|
});
|
|
1765
1711
|
}
|
|
1766
|
-
/**
|
|
1767
|
-
* Consume newline-delimited JSON records as a lazy async iterable.
|
|
1768
|
-
*/
|
|
1769
1712
|
ndjson(url, config = {}) {
|
|
1770
1713
|
return this.get(url, {
|
|
1771
1714
|
...config,
|
|
1772
1715
|
responseType: "ndjson"
|
|
1773
1716
|
});
|
|
1774
1717
|
}
|
|
1775
|
-
/**
|
|
1776
|
-
* Consume newline-delimited JSON with complete response metadata.
|
|
1777
|
-
*/
|
|
1778
1718
|
ndjsonResponse(url, config = {}) {
|
|
1779
1719
|
return this.getResponse(url, {
|
|
1780
1720
|
...config,
|
|
@@ -1813,28 +1753,19 @@ var Client = class Client {
|
|
|
1813
1753
|
};
|
|
1814
1754
|
//#endregion
|
|
1815
1755
|
//#region src/client/createClient.ts
|
|
1816
|
-
/**
|
|
1817
|
-
* Create an HTTP client instance.
|
|
1818
|
-
*/
|
|
1819
1756
|
function createClient(options = {}) {
|
|
1820
1757
|
return new Client(options);
|
|
1821
1758
|
}
|
|
1822
1759
|
//#endregion
|
|
1823
1760
|
//#region src/plugins/resolveExtensionConfig.ts
|
|
1824
|
-
/**
|
|
1825
|
-
* Resolve namespaced plugin configuration.
|
|
1826
|
-
*/
|
|
1827
1761
|
function resolveExtensionConfig(config, key) {
|
|
1828
1762
|
return config.extensions?.[key];
|
|
1829
1763
|
}
|
|
1830
1764
|
//#endregion
|
|
1831
1765
|
//#region src/plugins/authPlugin.ts
|
|
1832
|
-
/**
|
|
1833
|
-
* Authentication plugin.
|
|
1834
|
-
*/
|
|
1835
1766
|
function authPlugin(options = {}) {
|
|
1836
1767
|
let refreshPromise;
|
|
1837
|
-
const refreshedContexts =
|
|
1768
|
+
const refreshedContexts = new WeakSet();
|
|
1838
1769
|
return {
|
|
1839
1770
|
name: "auth",
|
|
1840
1771
|
install(context) {
|
|
@@ -1907,7 +1838,7 @@ function defaultShouldRefresh(error) {
|
|
|
1907
1838
|
//#region src/plugins/cachePlugin.ts
|
|
1908
1839
|
var MemoryCacheStore = class {
|
|
1909
1840
|
constructor(options = {}) {
|
|
1910
|
-
this.entries =
|
|
1841
|
+
this.entries = new Map();
|
|
1911
1842
|
this.maxEntries = normalizeMaxEntries(options.maxEntries);
|
|
1912
1843
|
}
|
|
1913
1844
|
get(key) {
|
|
@@ -1947,11 +1878,11 @@ const DEFAULT_VARY_HEADERS = [
|
|
|
1947
1878
|
];
|
|
1948
1879
|
function cachePlugin(options = {}) {
|
|
1949
1880
|
const store = options.store ?? new MemoryCacheStore({ maxEntries: options.maxEntries });
|
|
1950
|
-
const cacheHits =
|
|
1951
|
-
const leaders =
|
|
1952
|
-
const completedRecords =
|
|
1953
|
-
const uncacheableLeaders =
|
|
1954
|
-
const inFlight =
|
|
1881
|
+
const cacheHits = new WeakSet();
|
|
1882
|
+
const leaders = new WeakMap();
|
|
1883
|
+
const completedRecords = new WeakMap();
|
|
1884
|
+
const uncacheableLeaders = new WeakSet();
|
|
1885
|
+
const inFlight = new Map();
|
|
1955
1886
|
const methods = new Set(options.methods ?? DEFAULT_CACHE_METHODS);
|
|
1956
1887
|
const varyHeaders = options.varyHeaders ?? DEFAULT_VARY_HEADERS;
|
|
1957
1888
|
return {
|
|
@@ -1960,7 +1891,7 @@ function cachePlugin(options = {}) {
|
|
|
1960
1891
|
return store.clear();
|
|
1961
1892
|
},
|
|
1962
1893
|
install(context) {
|
|
1963
|
-
const ownedLeaders =
|
|
1894
|
+
const ownedLeaders = new Set();
|
|
1964
1895
|
let active = true;
|
|
1965
1896
|
context.hooks.onRequest(async (requestContext) => {
|
|
1966
1897
|
const cache = resolveExtensionConfig(requestContext.config, "cache");
|
|
@@ -2180,7 +2111,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
2180
2111
|
});
|
|
2181
2112
|
}
|
|
2182
2113
|
function normalizeCacheHeaders(headers, varyHeaders) {
|
|
2183
|
-
const values =
|
|
2114
|
+
const values = new Map();
|
|
2184
2115
|
headers.forEach((value, name) => {
|
|
2185
2116
|
values.set(name.toLowerCase(), value);
|
|
2186
2117
|
});
|
|
@@ -2246,13 +2177,24 @@ function normalizeCacheTtl(value, config) {
|
|
|
2246
2177
|
return ttl;
|
|
2247
2178
|
}
|
|
2248
2179
|
function isExpired(expiresAt) {
|
|
2249
|
-
return Number.isNaN(expiresAt) || Date.now()
|
|
2180
|
+
return Number.isNaN(expiresAt) || Date.now() >= expiresAt;
|
|
2181
|
+
}
|
|
2182
|
+
//#endregion
|
|
2183
|
+
//#region src/utils/resolveRequestOrigin.ts
|
|
2184
|
+
function resolveRequestOrigin(config) {
|
|
2185
|
+
try {
|
|
2186
|
+
return new URL(config.url).origin;
|
|
2187
|
+
} catch {}
|
|
2188
|
+
try {
|
|
2189
|
+
if (config.baseURL) return new URL(config.url, config.baseURL).origin;
|
|
2190
|
+
} catch {}
|
|
2191
|
+
return "default";
|
|
2250
2192
|
}
|
|
2251
2193
|
//#endregion
|
|
2252
2194
|
//#region src/plugins/circuitBreakerPlugin.ts
|
|
2253
2195
|
function circuitBreakerPlugin(options = {}) {
|
|
2254
2196
|
const normalized = normalizeOptions$1(options);
|
|
2255
|
-
const circuits =
|
|
2197
|
+
const circuits = new Map();
|
|
2256
2198
|
return {
|
|
2257
2199
|
name: "circuit-breaker",
|
|
2258
2200
|
getState(key) {
|
|
@@ -2266,7 +2208,7 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2266
2208
|
circuits.delete(key);
|
|
2267
2209
|
},
|
|
2268
2210
|
install(context) {
|
|
2269
|
-
const admissions =
|
|
2211
|
+
const admissions = new WeakMap();
|
|
2270
2212
|
let active = true;
|
|
2271
2213
|
context.hooks.onRequest((requestContext) => {
|
|
2272
2214
|
const requestOptions = resolveExtensionConfig(requestContext.config, "circuitBreaker");
|
|
@@ -2335,7 +2277,7 @@ function normalizeOptions$1(options) {
|
|
|
2335
2277
|
resetTimeout: normalizeDuration$1(options.resetTimeout, 3e4),
|
|
2336
2278
|
halfOpenMaxRequests: normalizePositiveInteger$1(options.halfOpenMaxRequests, 1),
|
|
2337
2279
|
maxCircuits: normalizePositiveInteger$1(options.maxCircuits, 1e3),
|
|
2338
|
-
createKey: options.createKey ??
|
|
2280
|
+
createKey: options.createKey ?? resolveRequestOrigin,
|
|
2339
2281
|
shouldCountFailure: options.shouldCountFailure ?? defaultShouldCountFailure,
|
|
2340
2282
|
onStateChange: options.onStateChange
|
|
2341
2283
|
};
|
|
@@ -2353,15 +2295,6 @@ function normalizeDuration$1(value, fallback) {
|
|
|
2353
2295
|
function normalizeKey$1(key) {
|
|
2354
2296
|
return key || "default";
|
|
2355
2297
|
}
|
|
2356
|
-
function createDefaultKey$1(config) {
|
|
2357
|
-
try {
|
|
2358
|
-
return new URL(config.url).origin;
|
|
2359
|
-
} catch {}
|
|
2360
|
-
try {
|
|
2361
|
-
if (config.baseURL) return new URL(config.url, config.baseURL).origin;
|
|
2362
|
-
} catch {}
|
|
2363
|
-
return "default";
|
|
2364
|
-
}
|
|
2365
2298
|
function getCircuit(circuits, key, maxCircuits) {
|
|
2366
2299
|
let record = circuits.get(key);
|
|
2367
2300
|
if (record) {
|
|
@@ -2443,7 +2376,7 @@ function defaultShouldCountFailure(error) {
|
|
|
2443
2376
|
//#region src/plugins/concurrencyPlugin.ts
|
|
2444
2377
|
function concurrencyPlugin(options = {}) {
|
|
2445
2378
|
const normalized = normalizeOptions(options);
|
|
2446
|
-
const records =
|
|
2379
|
+
const records = new Map();
|
|
2447
2380
|
return {
|
|
2448
2381
|
name: "concurrency",
|
|
2449
2382
|
getState(key) {
|
|
@@ -2454,7 +2387,7 @@ function concurrencyPlugin(options = {}) {
|
|
|
2454
2387
|
};
|
|
2455
2388
|
},
|
|
2456
2389
|
install(context) {
|
|
2457
|
-
const admissions =
|
|
2390
|
+
const admissions = new WeakMap();
|
|
2458
2391
|
let active = true;
|
|
2459
2392
|
context.hooks.onRequest(async (requestContext) => {
|
|
2460
2393
|
const requestOptions = resolveExtensionConfig(requestContext.config, "concurrency");
|
|
@@ -2586,7 +2519,7 @@ function normalizeOptions(options) {
|
|
|
2586
2519
|
maxQueue: normalizeNonNegativeInteger(options.maxQueue, 1e3),
|
|
2587
2520
|
queueTimeout: normalizeDuration(options.queueTimeout, 3e4),
|
|
2588
2521
|
maxKeys: normalizePositiveInteger(options.maxKeys, 1e3),
|
|
2589
|
-
createKey: options.createKey ??
|
|
2522
|
+
createKey: options.createKey ?? resolveRequestOrigin
|
|
2590
2523
|
};
|
|
2591
2524
|
}
|
|
2592
2525
|
function normalizePositiveInteger(value, fallback) {
|
|
@@ -2605,15 +2538,6 @@ function normalizeDuration(value, fallback) {
|
|
|
2605
2538
|
function normalizeKey(key) {
|
|
2606
2539
|
return String(key).trim() || "default";
|
|
2607
2540
|
}
|
|
2608
|
-
function createDefaultKey(config) {
|
|
2609
|
-
try {
|
|
2610
|
-
return new URL(config.url).origin;
|
|
2611
|
-
} catch {}
|
|
2612
|
-
try {
|
|
2613
|
-
if (config.baseURL) return new URL(config.url, config.baseURL).origin;
|
|
2614
|
-
} catch {}
|
|
2615
|
-
return "default";
|
|
2616
|
-
}
|
|
2617
2541
|
function createLimitError(message, config) {
|
|
2618
2542
|
return new RequestError(message, {
|
|
2619
2543
|
code: "CONCURRENCY_LIMIT",
|
|
@@ -2737,7 +2661,7 @@ async function processResponse(xhr, config) {
|
|
|
2737
2661
|
config,
|
|
2738
2662
|
raw
|
|
2739
2663
|
};
|
|
2740
|
-
if (!(
|
|
2664
|
+
if (!validateResponseStatus(xhr.status, config)) throw new RequestError(xhr.statusText || "Request failed", {
|
|
2741
2665
|
code: "HTTP_ERROR",
|
|
2742
2666
|
response
|
|
2743
2667
|
});
|
|
@@ -2813,13 +2737,10 @@ function createConfigError(config, cause) {
|
|
|
2813
2737
|
function isNullBodyStatus(status) {
|
|
2814
2738
|
return status === 204 || status === 205 || status === 304;
|
|
2815
2739
|
}
|
|
2816
|
-
function defaultValidateStatus(status) {
|
|
2817
|
-
return status >= 200 && status < 300;
|
|
2818
|
-
}
|
|
2819
2740
|
//#endregion
|
|
2820
2741
|
//#region src/plugins/downloadPlugin.ts
|
|
2821
2742
|
function downloadPlugin(options = {}) {
|
|
2822
|
-
const xhrContexts =
|
|
2743
|
+
const xhrContexts = new WeakSet();
|
|
2823
2744
|
return {
|
|
2824
2745
|
name: "download",
|
|
2825
2746
|
install(context) {
|
|
@@ -2897,10 +2818,6 @@ async function consumeDownloadStream(stream, response, onProgress) {
|
|
|
2897
2818
|
}
|
|
2898
2819
|
return new Blob(chunks, { type: response.headers.get("content-type") ?? "" });
|
|
2899
2820
|
}
|
|
2900
|
-
/**
|
|
2901
|
-
* Reuses complete ArrayBuffer chunks and copies only sliced or
|
|
2902
|
-
* SharedArrayBuffer-backed views before passing them to Blob.
|
|
2903
|
-
*/
|
|
2904
2821
|
function toDownloadBuffer(chunk) {
|
|
2905
2822
|
if (chunk.buffer instanceof ArrayBuffer) {
|
|
2906
2823
|
if (chunk.byteOffset === 0 && chunk.byteLength === chunk.buffer.byteLength) return chunk.buffer;
|
|
@@ -2931,7 +2848,7 @@ function createProgress(loaded, total) {
|
|
|
2931
2848
|
//#region src/plugins/loggerPlugin.ts
|
|
2932
2849
|
function loggerPlugin(defaultOptions = {}) {
|
|
2933
2850
|
let nextRequestId = 0;
|
|
2934
|
-
const states =
|
|
2851
|
+
const states = new WeakMap();
|
|
2935
2852
|
return {
|
|
2936
2853
|
name: "logger",
|
|
2937
2854
|
install(context) {
|
|
@@ -3012,7 +2929,7 @@ function emitError(options, entry) {
|
|
|
3012
2929
|
} catch {}
|
|
3013
2930
|
}
|
|
3014
2931
|
function ignoreLoggerError() {}
|
|
3015
|
-
const SENSITIVE_QUERY_KEYS =
|
|
2932
|
+
const SENSITIVE_QUERY_KEYS = new Set([
|
|
3016
2933
|
"access_token",
|
|
3017
2934
|
"api-key",
|
|
3018
2935
|
"api_key",
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
//#region src/errors/RequestError.ts
|
|
2
|
-
/**
|
|
3
|
-
* Unified public error for all request failures.
|
|
4
|
-
*/
|
|
5
2
|
var RequestError = class extends Error {
|
|
6
3
|
constructor(message, options) {
|
|
7
4
|
super(message);
|
|
@@ -17,9 +14,6 @@ var RequestError = class extends Error {
|
|
|
17
14
|
};
|
|
18
15
|
//#endregion
|
|
19
16
|
//#region src/errors/SchemaValidationError.ts
|
|
20
|
-
/**
|
|
21
|
-
* Response failure reported by a Standard Schema compatible validator.
|
|
22
|
-
*/
|
|
23
17
|
var SchemaValidationError = class extends RequestError {
|
|
24
18
|
constructor(message, response, schemaVendor, issues = [], cause) {
|
|
25
19
|
super(message, {
|
|
@@ -75,15 +69,9 @@ function createTimeoutSignal(signal, timeout) {
|
|
|
75
69
|
function noop() {}
|
|
76
70
|
//#endregion
|
|
77
71
|
//#region src/utils/buildRequest.ts
|
|
78
|
-
/**
|
|
79
|
-
* Build a Fetch-compatible request from RequestConfig.
|
|
80
|
-
*/
|
|
81
72
|
function buildRequest(config) {
|
|
82
73
|
return buildRequestWithHeaders(config, new Headers(config.headers));
|
|
83
74
|
}
|
|
84
|
-
/**
|
|
85
|
-
* Internal fast path for a Headers instance already created by validation.
|
|
86
|
-
*/
|
|
87
75
|
function buildRequestWithHeaders(config, headers) {
|
|
88
76
|
const body = buildBody(config, headers);
|
|
89
77
|
const timeoutSignal = createTimeoutSignal(config.signal, config.timeout);
|
|
@@ -163,7 +151,7 @@ function buildURLSearchParams(form) {
|
|
|
163
151
|
function buildFormData(input, maxDepth = 32) {
|
|
164
152
|
if (input instanceof FormData) return input;
|
|
165
153
|
const formData = new FormData();
|
|
166
|
-
const ancestors =
|
|
154
|
+
const ancestors = new WeakSet();
|
|
167
155
|
for (const key in input) if (Object.prototype.hasOwnProperty.call(input, key)) appendFormDataValue(formData, key, input[key], 0, maxDepth, ancestors);
|
|
168
156
|
return formData;
|
|
169
157
|
}
|
|
@@ -195,19 +183,12 @@ function isPlainObject$1(value) {
|
|
|
195
183
|
}
|
|
196
184
|
//#endregion
|
|
197
185
|
//#region src/utils/parseStreamingResponse.ts
|
|
198
|
-
/**
|
|
199
|
-
* Decode a server-sent event response without buffering the complete body.
|
|
200
|
-
*/
|
|
201
186
|
function parseServerSentEvents(response, config) {
|
|
202
187
|
return decodeServerSentEvents(response.body, {
|
|
203
188
|
config,
|
|
204
189
|
response
|
|
205
190
|
});
|
|
206
191
|
}
|
|
207
|
-
/**
|
|
208
|
-
* Decode a newline-delimited JSON response without buffering the complete
|
|
209
|
-
* body.
|
|
210
|
-
*/
|
|
211
192
|
function parseNdjson(response, config) {
|
|
212
193
|
return decodeNdjson(response.body, {
|
|
213
194
|
config,
|
|
@@ -329,9 +310,6 @@ function parserError(message, context, cause) {
|
|
|
329
310
|
}
|
|
330
311
|
//#endregion
|
|
331
312
|
//#region src/utils/parseResponse.ts
|
|
332
|
-
/**
|
|
333
|
-
* Bound a Fetch response before it can be cloned into multiple body branches.
|
|
334
|
-
*/
|
|
335
313
|
function limitResponseSize(response, config) {
|
|
336
314
|
const maxSize = config.maxResponseSize;
|
|
337
315
|
if (maxSize === void 0 || !Number.isFinite(maxSize) || !response.body) return response;
|
|
@@ -349,10 +327,6 @@ function limitResponseSize(response, config) {
|
|
|
349
327
|
copyResponseMetadata(response, limited);
|
|
350
328
|
return limited;
|
|
351
329
|
}
|
|
352
|
-
/**
|
|
353
|
-
* Keep request cancellation resources alive until a streaming response body
|
|
354
|
-
* settles instead of releasing them as soon as response headers arrive.
|
|
355
|
-
*/
|
|
356
330
|
function finalizeStreamingResponse(response, signal, config, finalize) {
|
|
357
331
|
if (!response.body) {
|
|
358
332
|
finalize();
|
|
@@ -425,11 +399,8 @@ function finalizeStreamingResponse(response, signal, config, finalize) {
|
|
|
425
399
|
copyResponseMetadata(response, wrapped);
|
|
426
400
|
return wrapped;
|
|
427
401
|
}
|
|
428
|
-
/**
|
|
429
|
-
* Parse a Fetch response according to the request configuration.
|
|
430
|
-
*/
|
|
431
402
|
async function parseResponse(response, config) {
|
|
432
|
-
if (config.method === "HEAD" || response.status === 204 || response.status === 205) return;
|
|
403
|
+
if (config.method === "HEAD" || response.status === 204 || response.status === 205 || response.status === 304) return;
|
|
433
404
|
const responseType = resolveResponseType(response, config);
|
|
434
405
|
const maxSize = config.maxResponseSize;
|
|
435
406
|
try {
|
|
@@ -469,7 +440,7 @@ async function rejectOversizedContentLength(response, maxSize, config) {
|
|
|
469
440
|
throw error;
|
|
470
441
|
}
|
|
471
442
|
async function readLimitedBody(response, maxSize, config) {
|
|
472
|
-
if (!response.body) return
|
|
443
|
+
if (!response.body) return new Uint8Array();
|
|
473
444
|
const reader = response.body.getReader();
|
|
474
445
|
const chunks = [];
|
|
475
446
|
let size = 0;
|
|
@@ -629,18 +600,29 @@ const BODY_FIELDS = [
|
|
|
629
600
|
"form",
|
|
630
601
|
"formData"
|
|
631
602
|
];
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
603
|
+
const RESPONSE_TYPES = [
|
|
604
|
+
"json",
|
|
605
|
+
"text",
|
|
606
|
+
"blob",
|
|
607
|
+
"arrayBuffer",
|
|
608
|
+
"stream",
|
|
609
|
+
"sse",
|
|
610
|
+
"ndjson"
|
|
611
|
+
];
|
|
635
612
|
function validateRequestConfig(config) {
|
|
636
613
|
validateURL(config);
|
|
637
614
|
validateTimeout(config);
|
|
638
615
|
validateLimits(config);
|
|
616
|
+
validateResponseOptions(config);
|
|
639
617
|
validateSchema(config);
|
|
640
618
|
const headers = validateHeaders(config);
|
|
641
619
|
validateBody(config);
|
|
642
620
|
return headers;
|
|
643
621
|
}
|
|
622
|
+
function validateResponseOptions(config) {
|
|
623
|
+
if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
|
|
624
|
+
if (config.validateStatus !== void 0 && typeof config.validateStatus !== "function") throw configError("Request validateStatus must be a function", config);
|
|
625
|
+
}
|
|
644
626
|
function validateSchema(config) {
|
|
645
627
|
if (config.schema === void 0) return;
|
|
646
628
|
try {
|
|
@@ -696,6 +678,20 @@ function configError(message, config, cause) {
|
|
|
696
678
|
});
|
|
697
679
|
}
|
|
698
680
|
//#endregion
|
|
681
|
+
//#region src/utils/validateResponseStatus.ts
|
|
682
|
+
function validateResponseStatus(status, config) {
|
|
683
|
+
try {
|
|
684
|
+
return config.validateStatus ? config.validateStatus(status) : status >= 200 && status < 300;
|
|
685
|
+
} catch (error) {
|
|
686
|
+
throw new RequestError("Request validateStatus callback failed", {
|
|
687
|
+
code: "CONFIG_ERROR",
|
|
688
|
+
status,
|
|
689
|
+
config,
|
|
690
|
+
cause: error
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
//#endregion
|
|
699
695
|
//#region src/adapters/FetchAdapter.ts
|
|
700
696
|
var FetchAdapter = class {
|
|
701
697
|
async request(config) {
|
|
@@ -711,7 +707,7 @@ var FetchAdapter = class {
|
|
|
711
707
|
try {
|
|
712
708
|
request = buildRequestWithHeaders(config, headers);
|
|
713
709
|
let response = limitResponseSize(await fetch(request.url, request.init), config);
|
|
714
|
-
const validStatus = (
|
|
710
|
+
const validStatus = validateResponseStatus(response.status, config);
|
|
715
711
|
const streaming = isStreamingResponseType(resolveResponseType(response, config));
|
|
716
712
|
if (streaming) {
|
|
717
713
|
response = finalizeStreamingResponse(response, request.init.signal, config, request.clear);
|
|
@@ -765,14 +761,11 @@ var FetchAdapter = class {
|
|
|
765
761
|
}
|
|
766
762
|
}
|
|
767
763
|
};
|
|
768
|
-
function defaultValidateStatus$2(status) {
|
|
769
|
-
return status >= 200 && status < 300;
|
|
770
|
-
}
|
|
771
764
|
//#endregion
|
|
772
765
|
//#region src/adapters/MockAdapter.ts
|
|
773
766
|
var MockAdapter = class {
|
|
774
767
|
constructor(options = {}) {
|
|
775
|
-
this.handlers =
|
|
768
|
+
this.handlers = new Map();
|
|
776
769
|
this.rules = [];
|
|
777
770
|
this.requests = [];
|
|
778
771
|
this.delay = normalizeDelay$1(options.delay);
|
|
@@ -783,9 +776,6 @@ var MockAdapter = class {
|
|
|
783
776
|
get history() {
|
|
784
777
|
return [...this.requests];
|
|
785
778
|
}
|
|
786
|
-
/**
|
|
787
|
-
* Register a legacy URL-only data handler.
|
|
788
|
-
*/
|
|
789
779
|
on(url, handler) {
|
|
790
780
|
this.handlers.set(url, handler);
|
|
791
781
|
return this;
|
|
@@ -963,15 +953,12 @@ function createMockResponse(config, reply) {
|
|
|
963
953
|
};
|
|
964
954
|
}
|
|
965
955
|
function validateMockResponse(response, config) {
|
|
966
|
-
if (!(
|
|
956
|
+
if (!validateResponseStatus(response.status, config)) throw new RequestError(response.statusText || "Mock request failed", {
|
|
967
957
|
code: "HTTP_ERROR",
|
|
968
958
|
response
|
|
969
959
|
});
|
|
970
960
|
return response;
|
|
971
961
|
}
|
|
972
|
-
function defaultValidateStatus$1(status) {
|
|
973
|
-
return status >= 200 && status < 300;
|
|
974
|
-
}
|
|
975
962
|
function defaultStatusText(status) {
|
|
976
963
|
return STATUS_TEXT[status] ?? "";
|
|
977
964
|
}
|
|
@@ -1043,22 +1030,10 @@ function createAbortError$3(reason, config) {
|
|
|
1043
1030
|
}
|
|
1044
1031
|
//#endregion
|
|
1045
1032
|
//#region src/core/ConfigMerger.ts
|
|
1046
|
-
/**
|
|
1047
|
-
* Request configuration merger.
|
|
1048
|
-
*
|
|
1049
|
-
* Responsible for merging client defaults
|
|
1050
|
-
* and request specific configuration.
|
|
1051
|
-
*/
|
|
1052
1033
|
var ConfigMerger = class {
|
|
1053
|
-
/**
|
|
1054
|
-
* Merge request configuration.
|
|
1055
|
-
*/
|
|
1056
1034
|
static merge(defaults, config) {
|
|
1057
1035
|
return this.mergeConfig(defaults, config);
|
|
1058
1036
|
}
|
|
1059
|
-
/**
|
|
1060
|
-
* Merge two sets of client defaults.
|
|
1061
|
-
*/
|
|
1062
1037
|
static mergeDefaults(defaults, overrides) {
|
|
1063
1038
|
return this.mergeConfig(defaults, overrides);
|
|
1064
1039
|
}
|
|
@@ -1081,9 +1056,6 @@ var ConfigMerger = class {
|
|
|
1081
1056
|
...options
|
|
1082
1057
|
};
|
|
1083
1058
|
}
|
|
1084
|
-
/**
|
|
1085
|
-
* Merge request headers.
|
|
1086
|
-
*/
|
|
1087
1059
|
static mergeHeaders(defaults, headers) {
|
|
1088
1060
|
if (!defaults && !headers) return;
|
|
1089
1061
|
const result = {};
|
|
@@ -1180,11 +1152,6 @@ var RequestContext = class {
|
|
|
1180
1152
|
};
|
|
1181
1153
|
//#endregion
|
|
1182
1154
|
//#region src/core/Pipeline.ts
|
|
1183
|
-
/**
|
|
1184
|
-
* Coordinates the request lifecycle.
|
|
1185
|
-
*
|
|
1186
|
-
* Pipeline does not implement business features.
|
|
1187
|
-
*/
|
|
1188
1155
|
var Pipeline = class {
|
|
1189
1156
|
constructor(adapter, interceptors, hooks) {
|
|
1190
1157
|
this.adapter = adapter;
|
|
@@ -1263,13 +1230,13 @@ var Pipeline = class {
|
|
|
1263
1230
|
} catch (error) {
|
|
1264
1231
|
throw new SchemaValidationError("Response schema validator failed", response, schemaVendor, [], error);
|
|
1265
1232
|
}
|
|
1266
|
-
if (typeof result !== "object" || result === null) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [],
|
|
1233
|
+
if (typeof result !== "object" || result === null) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [], new TypeError("Expected a Standard Schema result"));
|
|
1267
1234
|
const issues = "issues" in result ? result.issues : void 0;
|
|
1268
1235
|
if (issues !== void 0) {
|
|
1269
|
-
if (!Array.isArray(issues)) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [],
|
|
1236
|
+
if (!Array.isArray(issues)) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [], new TypeError("Expected Standard Schema issues to be an array"));
|
|
1270
1237
|
throw new SchemaValidationError("Response schema validation failed", response, schemaVendor, issues);
|
|
1271
1238
|
}
|
|
1272
|
-
if (!("value" in result)) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [],
|
|
1239
|
+
if (!("value" in result)) throw new SchemaValidationError("Response schema validator returned an invalid result", response, schemaVendor, [], new TypeError("Expected a Standard Schema value"));
|
|
1273
1240
|
context.response = {
|
|
1274
1241
|
...response,
|
|
1275
1242
|
data: result.value
|
|
@@ -1333,7 +1300,7 @@ function createAbortError$2(reason, config) {
|
|
|
1333
1300
|
var InterceptorManager = class {
|
|
1334
1301
|
constructor() {
|
|
1335
1302
|
this.id = 0;
|
|
1336
|
-
this.interceptors =
|
|
1303
|
+
this.interceptors = new Map();
|
|
1337
1304
|
this.orderedInterceptors = [];
|
|
1338
1305
|
}
|
|
1339
1306
|
get active() {
|
|
@@ -1372,12 +1339,6 @@ function normalizePriority$1(priority) {
|
|
|
1372
1339
|
}
|
|
1373
1340
|
//#endregion
|
|
1374
1341
|
//#region src/interceptors/PluginHooks.ts
|
|
1375
|
-
/**
|
|
1376
|
-
* Internal plugin lifecycle manager.
|
|
1377
|
-
*
|
|
1378
|
-
* Hook order is calculated when registrations change so the hot request path
|
|
1379
|
-
* can iterate a stable array without allocating and sorting per request.
|
|
1380
|
-
*/
|
|
1381
1342
|
var PluginHooks = class {
|
|
1382
1343
|
constructor() {
|
|
1383
1344
|
this.requestHooks = new HookRegistry();
|
|
@@ -1444,7 +1405,7 @@ var PluginHooks = class {
|
|
|
1444
1405
|
var HookRegistry = class {
|
|
1445
1406
|
constructor() {
|
|
1446
1407
|
this.id = 0;
|
|
1447
|
-
this.hooks =
|
|
1408
|
+
this.hooks = new Map();
|
|
1448
1409
|
this.orderedHooks = [];
|
|
1449
1410
|
}
|
|
1450
1411
|
get active() {
|
|
@@ -1476,9 +1437,6 @@ function normalizePriority(priority) {
|
|
|
1476
1437
|
}
|
|
1477
1438
|
//#endregion
|
|
1478
1439
|
//#region src/plugins/PluginError.ts
|
|
1479
|
-
/**
|
|
1480
|
-
* Stable error thrown for invalid plugin lifecycle operations.
|
|
1481
|
-
*/
|
|
1482
1440
|
var PluginError = class extends Error {
|
|
1483
1441
|
constructor(message, options) {
|
|
1484
1442
|
super(message);
|
|
@@ -1491,9 +1449,6 @@ var PluginError = class extends Error {
|
|
|
1491
1449
|
};
|
|
1492
1450
|
//#endregion
|
|
1493
1451
|
//#region src/plugins/PluginScope.ts
|
|
1494
|
-
/**
|
|
1495
|
-
* Create an isolated registration scope for one plugin.
|
|
1496
|
-
*/
|
|
1497
1452
|
function createPluginScope(interceptors, hooks, priority = 0) {
|
|
1498
1453
|
const cleanups = [];
|
|
1499
1454
|
return {
|
|
@@ -1521,7 +1476,7 @@ function createPluginScope(interceptors, hooks, priority = 0) {
|
|
|
1521
1476
|
};
|
|
1522
1477
|
}
|
|
1523
1478
|
function createScopedInterceptor(manager, pluginPriority, cleanups) {
|
|
1524
|
-
const ids =
|
|
1479
|
+
const ids = new Set();
|
|
1525
1480
|
cleanups.push(() => {
|
|
1526
1481
|
for (const id of ids) manager.eject(id);
|
|
1527
1482
|
ids.clear();
|
|
@@ -1577,7 +1532,7 @@ function once(cleanup) {
|
|
|
1577
1532
|
//#region src/client/Client.ts
|
|
1578
1533
|
var Client = class Client {
|
|
1579
1534
|
constructor(options = {}) {
|
|
1580
|
-
this.installedPlugins =
|
|
1535
|
+
this.installedPlugins = new Map();
|
|
1581
1536
|
this.hooks = new PluginHooks();
|
|
1582
1537
|
this.interceptors = {
|
|
1583
1538
|
request: new InterceptorManager(),
|
|
@@ -1589,9 +1544,6 @@ var Client = class Client {
|
|
|
1589
1544
|
this.adapter = adapter;
|
|
1590
1545
|
this.pipeline = this.createPipeline(adapter);
|
|
1591
1546
|
}
|
|
1592
|
-
/**
|
|
1593
|
-
* Create an isolated client that inherits this client's defaults.
|
|
1594
|
-
*/
|
|
1595
1547
|
extend(options = {}) {
|
|
1596
1548
|
const { adapter = this.adapter, ...overrides } = options;
|
|
1597
1549
|
const defaults = ConfigMerger.mergeDefaults(this.defaults, overrides);
|
|
@@ -1744,36 +1696,24 @@ var Client = class Client {
|
|
|
1744
1696
|
method: "OPTIONS"
|
|
1745
1697
|
});
|
|
1746
1698
|
}
|
|
1747
|
-
/**
|
|
1748
|
-
* Consume a server-sent event response as a lazy async iterable.
|
|
1749
|
-
*/
|
|
1750
1699
|
sse(url, config = {}) {
|
|
1751
1700
|
return this.get(url, {
|
|
1752
1701
|
...config,
|
|
1753
1702
|
responseType: "sse"
|
|
1754
1703
|
});
|
|
1755
1704
|
}
|
|
1756
|
-
/**
|
|
1757
|
-
* Consume a server-sent event response with complete response metadata.
|
|
1758
|
-
*/
|
|
1759
1705
|
sseResponse(url, config = {}) {
|
|
1760
1706
|
return this.getResponse(url, {
|
|
1761
1707
|
...config,
|
|
1762
1708
|
responseType: "sse"
|
|
1763
1709
|
});
|
|
1764
1710
|
}
|
|
1765
|
-
/**
|
|
1766
|
-
* Consume newline-delimited JSON records as a lazy async iterable.
|
|
1767
|
-
*/
|
|
1768
1711
|
ndjson(url, config = {}) {
|
|
1769
1712
|
return this.get(url, {
|
|
1770
1713
|
...config,
|
|
1771
1714
|
responseType: "ndjson"
|
|
1772
1715
|
});
|
|
1773
1716
|
}
|
|
1774
|
-
/**
|
|
1775
|
-
* Consume newline-delimited JSON with complete response metadata.
|
|
1776
|
-
*/
|
|
1777
1717
|
ndjsonResponse(url, config = {}) {
|
|
1778
1718
|
return this.getResponse(url, {
|
|
1779
1719
|
...config,
|
|
@@ -1812,28 +1752,19 @@ var Client = class Client {
|
|
|
1812
1752
|
};
|
|
1813
1753
|
//#endregion
|
|
1814
1754
|
//#region src/client/createClient.ts
|
|
1815
|
-
/**
|
|
1816
|
-
* Create an HTTP client instance.
|
|
1817
|
-
*/
|
|
1818
1755
|
function createClient(options = {}) {
|
|
1819
1756
|
return new Client(options);
|
|
1820
1757
|
}
|
|
1821
1758
|
//#endregion
|
|
1822
1759
|
//#region src/plugins/resolveExtensionConfig.ts
|
|
1823
|
-
/**
|
|
1824
|
-
* Resolve namespaced plugin configuration.
|
|
1825
|
-
*/
|
|
1826
1760
|
function resolveExtensionConfig(config, key) {
|
|
1827
1761
|
return config.extensions?.[key];
|
|
1828
1762
|
}
|
|
1829
1763
|
//#endregion
|
|
1830
1764
|
//#region src/plugins/authPlugin.ts
|
|
1831
|
-
/**
|
|
1832
|
-
* Authentication plugin.
|
|
1833
|
-
*/
|
|
1834
1765
|
function authPlugin(options = {}) {
|
|
1835
1766
|
let refreshPromise;
|
|
1836
|
-
const refreshedContexts =
|
|
1767
|
+
const refreshedContexts = new WeakSet();
|
|
1837
1768
|
return {
|
|
1838
1769
|
name: "auth",
|
|
1839
1770
|
install(context) {
|
|
@@ -1906,7 +1837,7 @@ function defaultShouldRefresh(error) {
|
|
|
1906
1837
|
//#region src/plugins/cachePlugin.ts
|
|
1907
1838
|
var MemoryCacheStore = class {
|
|
1908
1839
|
constructor(options = {}) {
|
|
1909
|
-
this.entries =
|
|
1840
|
+
this.entries = new Map();
|
|
1910
1841
|
this.maxEntries = normalizeMaxEntries(options.maxEntries);
|
|
1911
1842
|
}
|
|
1912
1843
|
get(key) {
|
|
@@ -1946,11 +1877,11 @@ const DEFAULT_VARY_HEADERS = [
|
|
|
1946
1877
|
];
|
|
1947
1878
|
function cachePlugin(options = {}) {
|
|
1948
1879
|
const store = options.store ?? new MemoryCacheStore({ maxEntries: options.maxEntries });
|
|
1949
|
-
const cacheHits =
|
|
1950
|
-
const leaders =
|
|
1951
|
-
const completedRecords =
|
|
1952
|
-
const uncacheableLeaders =
|
|
1953
|
-
const inFlight =
|
|
1880
|
+
const cacheHits = new WeakSet();
|
|
1881
|
+
const leaders = new WeakMap();
|
|
1882
|
+
const completedRecords = new WeakMap();
|
|
1883
|
+
const uncacheableLeaders = new WeakSet();
|
|
1884
|
+
const inFlight = new Map();
|
|
1954
1885
|
const methods = new Set(options.methods ?? DEFAULT_CACHE_METHODS);
|
|
1955
1886
|
const varyHeaders = options.varyHeaders ?? DEFAULT_VARY_HEADERS;
|
|
1956
1887
|
return {
|
|
@@ -1959,7 +1890,7 @@ function cachePlugin(options = {}) {
|
|
|
1959
1890
|
return store.clear();
|
|
1960
1891
|
},
|
|
1961
1892
|
install(context) {
|
|
1962
|
-
const ownedLeaders =
|
|
1893
|
+
const ownedLeaders = new Set();
|
|
1963
1894
|
let active = true;
|
|
1964
1895
|
context.hooks.onRequest(async (requestContext) => {
|
|
1965
1896
|
const cache = resolveExtensionConfig(requestContext.config, "cache");
|
|
@@ -2179,7 +2110,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
2179
2110
|
});
|
|
2180
2111
|
}
|
|
2181
2112
|
function normalizeCacheHeaders(headers, varyHeaders) {
|
|
2182
|
-
const values =
|
|
2113
|
+
const values = new Map();
|
|
2183
2114
|
headers.forEach((value, name) => {
|
|
2184
2115
|
values.set(name.toLowerCase(), value);
|
|
2185
2116
|
});
|
|
@@ -2245,13 +2176,24 @@ function normalizeCacheTtl(value, config) {
|
|
|
2245
2176
|
return ttl;
|
|
2246
2177
|
}
|
|
2247
2178
|
function isExpired(expiresAt) {
|
|
2248
|
-
return Number.isNaN(expiresAt) || Date.now()
|
|
2179
|
+
return Number.isNaN(expiresAt) || Date.now() >= expiresAt;
|
|
2180
|
+
}
|
|
2181
|
+
//#endregion
|
|
2182
|
+
//#region src/utils/resolveRequestOrigin.ts
|
|
2183
|
+
function resolveRequestOrigin(config) {
|
|
2184
|
+
try {
|
|
2185
|
+
return new URL(config.url).origin;
|
|
2186
|
+
} catch {}
|
|
2187
|
+
try {
|
|
2188
|
+
if (config.baseURL) return new URL(config.url, config.baseURL).origin;
|
|
2189
|
+
} catch {}
|
|
2190
|
+
return "default";
|
|
2249
2191
|
}
|
|
2250
2192
|
//#endregion
|
|
2251
2193
|
//#region src/plugins/circuitBreakerPlugin.ts
|
|
2252
2194
|
function circuitBreakerPlugin(options = {}) {
|
|
2253
2195
|
const normalized = normalizeOptions$1(options);
|
|
2254
|
-
const circuits =
|
|
2196
|
+
const circuits = new Map();
|
|
2255
2197
|
return {
|
|
2256
2198
|
name: "circuit-breaker",
|
|
2257
2199
|
getState(key) {
|
|
@@ -2265,7 +2207,7 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2265
2207
|
circuits.delete(key);
|
|
2266
2208
|
},
|
|
2267
2209
|
install(context) {
|
|
2268
|
-
const admissions =
|
|
2210
|
+
const admissions = new WeakMap();
|
|
2269
2211
|
let active = true;
|
|
2270
2212
|
context.hooks.onRequest((requestContext) => {
|
|
2271
2213
|
const requestOptions = resolveExtensionConfig(requestContext.config, "circuitBreaker");
|
|
@@ -2334,7 +2276,7 @@ function normalizeOptions$1(options) {
|
|
|
2334
2276
|
resetTimeout: normalizeDuration$1(options.resetTimeout, 3e4),
|
|
2335
2277
|
halfOpenMaxRequests: normalizePositiveInteger$1(options.halfOpenMaxRequests, 1),
|
|
2336
2278
|
maxCircuits: normalizePositiveInteger$1(options.maxCircuits, 1e3),
|
|
2337
|
-
createKey: options.createKey ??
|
|
2279
|
+
createKey: options.createKey ?? resolveRequestOrigin,
|
|
2338
2280
|
shouldCountFailure: options.shouldCountFailure ?? defaultShouldCountFailure,
|
|
2339
2281
|
onStateChange: options.onStateChange
|
|
2340
2282
|
};
|
|
@@ -2352,15 +2294,6 @@ function normalizeDuration$1(value, fallback) {
|
|
|
2352
2294
|
function normalizeKey$1(key) {
|
|
2353
2295
|
return key || "default";
|
|
2354
2296
|
}
|
|
2355
|
-
function createDefaultKey$1(config) {
|
|
2356
|
-
try {
|
|
2357
|
-
return new URL(config.url).origin;
|
|
2358
|
-
} catch {}
|
|
2359
|
-
try {
|
|
2360
|
-
if (config.baseURL) return new URL(config.url, config.baseURL).origin;
|
|
2361
|
-
} catch {}
|
|
2362
|
-
return "default";
|
|
2363
|
-
}
|
|
2364
2297
|
function getCircuit(circuits, key, maxCircuits) {
|
|
2365
2298
|
let record = circuits.get(key);
|
|
2366
2299
|
if (record) {
|
|
@@ -2442,7 +2375,7 @@ function defaultShouldCountFailure(error) {
|
|
|
2442
2375
|
//#region src/plugins/concurrencyPlugin.ts
|
|
2443
2376
|
function concurrencyPlugin(options = {}) {
|
|
2444
2377
|
const normalized = normalizeOptions(options);
|
|
2445
|
-
const records =
|
|
2378
|
+
const records = new Map();
|
|
2446
2379
|
return {
|
|
2447
2380
|
name: "concurrency",
|
|
2448
2381
|
getState(key) {
|
|
@@ -2453,7 +2386,7 @@ function concurrencyPlugin(options = {}) {
|
|
|
2453
2386
|
};
|
|
2454
2387
|
},
|
|
2455
2388
|
install(context) {
|
|
2456
|
-
const admissions =
|
|
2389
|
+
const admissions = new WeakMap();
|
|
2457
2390
|
let active = true;
|
|
2458
2391
|
context.hooks.onRequest(async (requestContext) => {
|
|
2459
2392
|
const requestOptions = resolveExtensionConfig(requestContext.config, "concurrency");
|
|
@@ -2585,7 +2518,7 @@ function normalizeOptions(options) {
|
|
|
2585
2518
|
maxQueue: normalizeNonNegativeInteger(options.maxQueue, 1e3),
|
|
2586
2519
|
queueTimeout: normalizeDuration(options.queueTimeout, 3e4),
|
|
2587
2520
|
maxKeys: normalizePositiveInteger(options.maxKeys, 1e3),
|
|
2588
|
-
createKey: options.createKey ??
|
|
2521
|
+
createKey: options.createKey ?? resolveRequestOrigin
|
|
2589
2522
|
};
|
|
2590
2523
|
}
|
|
2591
2524
|
function normalizePositiveInteger(value, fallback) {
|
|
@@ -2604,15 +2537,6 @@ function normalizeDuration(value, fallback) {
|
|
|
2604
2537
|
function normalizeKey(key) {
|
|
2605
2538
|
return String(key).trim() || "default";
|
|
2606
2539
|
}
|
|
2607
|
-
function createDefaultKey(config) {
|
|
2608
|
-
try {
|
|
2609
|
-
return new URL(config.url).origin;
|
|
2610
|
-
} catch {}
|
|
2611
|
-
try {
|
|
2612
|
-
if (config.baseURL) return new URL(config.url, config.baseURL).origin;
|
|
2613
|
-
} catch {}
|
|
2614
|
-
return "default";
|
|
2615
|
-
}
|
|
2616
2540
|
function createLimitError(message, config) {
|
|
2617
2541
|
return new RequestError(message, {
|
|
2618
2542
|
code: "CONCURRENCY_LIMIT",
|
|
@@ -2736,7 +2660,7 @@ async function processResponse(xhr, config) {
|
|
|
2736
2660
|
config,
|
|
2737
2661
|
raw
|
|
2738
2662
|
};
|
|
2739
|
-
if (!(
|
|
2663
|
+
if (!validateResponseStatus(xhr.status, config)) throw new RequestError(xhr.statusText || "Request failed", {
|
|
2740
2664
|
code: "HTTP_ERROR",
|
|
2741
2665
|
response
|
|
2742
2666
|
});
|
|
@@ -2812,13 +2736,10 @@ function createConfigError(config, cause) {
|
|
|
2812
2736
|
function isNullBodyStatus(status) {
|
|
2813
2737
|
return status === 204 || status === 205 || status === 304;
|
|
2814
2738
|
}
|
|
2815
|
-
function defaultValidateStatus(status) {
|
|
2816
|
-
return status >= 200 && status < 300;
|
|
2817
|
-
}
|
|
2818
2739
|
//#endregion
|
|
2819
2740
|
//#region src/plugins/downloadPlugin.ts
|
|
2820
2741
|
function downloadPlugin(options = {}) {
|
|
2821
|
-
const xhrContexts =
|
|
2742
|
+
const xhrContexts = new WeakSet();
|
|
2822
2743
|
return {
|
|
2823
2744
|
name: "download",
|
|
2824
2745
|
install(context) {
|
|
@@ -2896,10 +2817,6 @@ async function consumeDownloadStream(stream, response, onProgress) {
|
|
|
2896
2817
|
}
|
|
2897
2818
|
return new Blob(chunks, { type: response.headers.get("content-type") ?? "" });
|
|
2898
2819
|
}
|
|
2899
|
-
/**
|
|
2900
|
-
* Reuses complete ArrayBuffer chunks and copies only sliced or
|
|
2901
|
-
* SharedArrayBuffer-backed views before passing them to Blob.
|
|
2902
|
-
*/
|
|
2903
2820
|
function toDownloadBuffer(chunk) {
|
|
2904
2821
|
if (chunk.buffer instanceof ArrayBuffer) {
|
|
2905
2822
|
if (chunk.byteOffset === 0 && chunk.byteLength === chunk.buffer.byteLength) return chunk.buffer;
|
|
@@ -2930,7 +2847,7 @@ function createProgress(loaded, total) {
|
|
|
2930
2847
|
//#region src/plugins/loggerPlugin.ts
|
|
2931
2848
|
function loggerPlugin(defaultOptions = {}) {
|
|
2932
2849
|
let nextRequestId = 0;
|
|
2933
|
-
const states =
|
|
2850
|
+
const states = new WeakMap();
|
|
2934
2851
|
return {
|
|
2935
2852
|
name: "logger",
|
|
2936
2853
|
install(context) {
|
|
@@ -3011,7 +2928,7 @@ function emitError(options, entry) {
|
|
|
3011
2928
|
} catch {}
|
|
3012
2929
|
}
|
|
3013
2930
|
function ignoreLoggerError() {}
|
|
3014
|
-
const SENSITIVE_QUERY_KEYS =
|
|
2931
|
+
const SENSITIVE_QUERY_KEYS = new Set([
|
|
3015
2932
|
"access_token",
|
|
3016
2933
|
"api-key",
|
|
3017
2934
|
"api_key",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@npora/request",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.3",
|
|
4
4
|
"description": "A modern, TypeScript-first HTTP client powered by the Fetch API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"npora",
|
|
@@ -8,7 +8,14 @@
|
|
|
8
8
|
"fetch",
|
|
9
9
|
"http",
|
|
10
10
|
"http-client",
|
|
11
|
-
"typescript"
|
|
11
|
+
"typescript",
|
|
12
|
+
"retry",
|
|
13
|
+
"cache",
|
|
14
|
+
"sse",
|
|
15
|
+
"ndjson",
|
|
16
|
+
"standard-schema",
|
|
17
|
+
"circuit-breaker",
|
|
18
|
+
"web-worker"
|
|
12
19
|
],
|
|
13
20
|
"homepage": "https://github.com/npora/request",
|
|
14
21
|
"repository": {
|
|
@@ -44,10 +51,10 @@
|
|
|
44
51
|
"devDependencies": {
|
|
45
52
|
"@changesets/cli": "^2.31.0",
|
|
46
53
|
"@playwright/test": "^1.61.1",
|
|
47
|
-
"@types/node": "^26.
|
|
54
|
+
"@types/node": "^26.2.0",
|
|
48
55
|
"@vitest/coverage-v8": "^4.1.10",
|
|
49
56
|
"tsdown": "^0.22.14",
|
|
50
|
-
"tsx": "^4.23.
|
|
57
|
+
"tsx": "^4.23.11",
|
|
51
58
|
"typescript": "^7.0.2",
|
|
52
59
|
"typescript-api": "npm:@typescript/typescript6@^6.0.2",
|
|
53
60
|
"vitest": "^4.1.10"
|