@npora/request 1.11.0 → 1.12.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 CHANGED
@@ -182,6 +182,10 @@ The body options `body`, `json`, `form`, and `formData` are mutually exclusive.
182
182
  `GET` and `HEAD` requests cannot contain a body. Invalid configuration throws a
183
183
  `RequestError` before the adapter sends a request.
184
184
 
185
+ See the complete [configuration reference](https://github.com/npora/request/blob/main/docs/configuration.md)
186
+ for every option, default, merge rule, plugin extension, environment limit,
187
+ and transfer-progress behavior.
188
+
185
189
  Create isolated clients with inherited defaults:
186
190
 
187
191
  ```ts
@@ -420,6 +424,7 @@ allowlist, and size budgets.
420
424
 
421
425
  ## Documentation
422
426
 
427
+ - [Configuration reference](https://github.com/npora/request/blob/main/docs/configuration.md)
423
428
  - [API reference](https://github.com/npora/request/blob/main/docs/api.md)
424
429
  - [Architecture](https://github.com/npora/request/blob/main/docs/architecture.md)
425
430
  - [Migration from 0.x](https://github.com/npora/request/blob/main/docs/migration.md)
package/dist/index.cjs CHANGED
@@ -69,6 +69,17 @@ function createTimeoutSignal(signal, timeout) {
69
69
  }
70
70
  function noop() {}
71
71
  //#endregion
72
+ //#region src/utils/isURLSearchParams.ts
73
+ function isURLSearchParams(value) {
74
+ if (typeof URLSearchParams === "undefined") return false;
75
+ try {
76
+ URLSearchParams.prototype.entries.call(value);
77
+ return true;
78
+ } catch {
79
+ return false;
80
+ }
81
+ }
82
+ //#endregion
72
83
  //#region src/utils/buildRequest.ts
73
84
  function buildRequest(config) {
74
85
  return buildRequestWithHeaders(config, new Headers(config.headers));
@@ -144,7 +155,7 @@ function buildBody(config, headers) {
144
155
  return config.body;
145
156
  }
146
157
  function buildURLSearchParams(form) {
147
- if (form instanceof URLSearchParams) return form;
158
+ if (isURLSearchParams(form)) return form;
148
159
  const params = new URLSearchParams();
149
160
  for (const key in form) if (Object.prototype.hasOwnProperty.call(form, key)) appendQuery(params, key, form[key]);
150
161
  return params;
@@ -610,8 +621,18 @@ const RESPONSE_TYPES = [
610
621
  "sse",
611
622
  "ndjson"
612
623
  ];
624
+ const HTTP_METHODS = [
625
+ "GET",
626
+ "POST",
627
+ "PUT",
628
+ "PATCH",
629
+ "DELETE",
630
+ "HEAD",
631
+ "OPTIONS"
632
+ ];
613
633
  function validateRequestConfig(config) {
614
634
  validateURL(config);
635
+ validateMethod(config);
615
636
  validateTimeout(config);
616
637
  validateLimits(config);
617
638
  validateQuery(config);
@@ -623,7 +644,10 @@ function validateRequestConfig(config) {
623
644
  }
624
645
  function validateQuery(config) {
625
646
  if (config.query !== void 0 && config.searchParams !== void 0) throw configError("Request query and searchParams are mutually exclusive", config);
626
- if (config.searchParams !== void 0 && !(config.searchParams instanceof URLSearchParams)) throw configError("Request searchParams must be URLSearchParams", 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);
627
651
  }
628
652
  function validateResponseOptions(config) {
629
653
  if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
@@ -997,7 +1021,7 @@ function createRawBody(data, headers, status) {
997
1021
  return JSON.stringify(data);
998
1022
  }
999
1023
  function isBodyInit(value) {
1000
- return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData || typeof URLSearchParams !== "undefined" && value instanceof URLSearchParams || typeof ReadableStream !== "undefined" && value instanceof ReadableStream || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
1024
+ return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData || isURLSearchParams(value) || typeof ReadableStream !== "undefined" && value instanceof ReadableStream || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
1001
1025
  }
1002
1026
  function normalizeDelay$1(delay) {
1003
1027
  if (!Number.isFinite(delay)) return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
@@ -1062,7 +1086,7 @@ var ConfigMerger = class {
1062
1086
  static mergeQueryConfig(defaults, config) {
1063
1087
  if (config.searchParams !== void 0) return {
1064
1088
  query: config.query,
1065
- searchParams: config.searchParams instanceof URLSearchParams ? new URLSearchParams(config.searchParams) : config.searchParams
1089
+ searchParams: isURLSearchParams(config.searchParams) ? new URLSearchParams(config.searchParams) : config.searchParams
1066
1090
  };
1067
1091
  if (config.query !== void 0) return {
1068
1092
  query: this.mergeObject(defaults.searchParams ? void 0 : defaults.query, config.query),
@@ -1070,7 +1094,7 @@ var ConfigMerger = class {
1070
1094
  };
1071
1095
  return {
1072
1096
  query: defaults.query ? { ...defaults.query } : void 0,
1073
- searchParams: defaults.searchParams instanceof URLSearchParams ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
1097
+ searchParams: isURLSearchParams(defaults.searchParams) ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
1074
1098
  };
1075
1099
  }
1076
1100
  static mergeFetchOptions(defaults, options) {
@@ -1206,7 +1230,8 @@ var Pipeline = class {
1206
1230
  while (true) try {
1207
1231
  const headers = validatedHeaders;
1208
1232
  validatedHeaders = void 0;
1209
- context.response = headers && this.adapter.requestValidated ? await this.adapter.requestValidated(context.config, headers, preserveRaw || this.hooks.hasResponseHooks || this.interceptors.response.active) : await this.adapter.request(context.config);
1233
+ if (this.hooks.hasTransportHooks) await this.hooks.runTransport(context);
1234
+ if (!context.response) context.response = headers && this.adapter.requestValidated ? await this.adapter.requestValidated(context.config, headers, preserveRaw || this.hooks.hasResponseHooks || this.interceptors.response.active) : await this.adapter.request(context.config);
1210
1235
  return await this.processResponse(context);
1211
1236
  } catch (error) {
1212
1237
  if (!await this.notifyError(context, error)) return this.fail(context, context.error, false);
@@ -1366,6 +1391,7 @@ function normalizePriority$1(priority) {
1366
1391
  var PluginHooks = class {
1367
1392
  constructor() {
1368
1393
  this.requestHooks = new HookRegistry();
1394
+ this.transportHooks = new HookRegistry();
1369
1395
  this.responseHooks = new HookRegistry();
1370
1396
  this.errorHooks = new HookRegistry();
1371
1397
  this.settledHooks = new HookRegistry();
@@ -1377,6 +1403,9 @@ var PluginHooks = class {
1377
1403
  get hasResponseHooks() {
1378
1404
  return this.responseHooks.active;
1379
1405
  }
1406
+ get hasTransportHooks() {
1407
+ return this.transportHooks.active;
1408
+ }
1380
1409
  get hasErrorHooks() {
1381
1410
  return this.errorHooks.active;
1382
1411
  }
@@ -1392,6 +1421,9 @@ var PluginHooks = class {
1392
1421
  onResponse(hook, options = {}) {
1393
1422
  return this.responseHooks.register(hook, options);
1394
1423
  }
1424
+ onTransport(hook, options = {}) {
1425
+ return this.transportHooks.register(hook, options);
1426
+ }
1395
1427
  onError(hook, options = {}) {
1396
1428
  return this.errorHooks.register(hook, options);
1397
1429
  }
@@ -1407,6 +1439,12 @@ var PluginHooks = class {
1407
1439
  async runResponse(context) {
1408
1440
  for (const hook of this.responseHooks.values()) await hook(context);
1409
1441
  }
1442
+ async runTransport(context) {
1443
+ for (const hook of this.transportHooks.values()) {
1444
+ await hook(context);
1445
+ if (context.response) return;
1446
+ }
1447
+ }
1410
1448
  async runError(context) {
1411
1449
  for (const hook of this.errorHooks.values()) await hook(context);
1412
1450
  }
@@ -1522,6 +1560,9 @@ function createScopedHooks(hooks, pluginPriority, cleanups) {
1522
1560
  onRequest(hook, options) {
1523
1561
  return trackHook(hooks.onRequest(hook, withPriority(options, pluginPriority)), cleanups);
1524
1562
  },
1563
+ onTransport(hook, options) {
1564
+ return trackHook(hooks.onTransport(hook, withPriority(options, pluginPriority)), cleanups);
1565
+ },
1525
1566
  onResponse(hook, options) {
1526
1567
  return trackHook(hooks.onResponse(hook, withPriority(options, pluginPriority)), cleanups);
1527
1568
  },
@@ -2148,7 +2189,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
2148
2189
  }
2149
2190
  function normalizeQuery(query) {
2150
2191
  if (!query) return [];
2151
- if (query instanceof URLSearchParams) return [...query.entries()];
2192
+ if (isURLSearchParams(query)) return [...query.entries()];
2152
2193
  const entries = [];
2153
2194
  for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
2154
2195
  return entries;
@@ -2777,7 +2818,7 @@ function downloadPlugin(options = {}) {
2777
2818
  responseType
2778
2819
  };
2779
2820
  });
2780
- context.hooks.onRequest(async (requestContext) => {
2821
+ context.hooks.onTransport(async (requestContext) => {
2781
2822
  if (requestContext.response) return;
2782
2823
  const onProgress = resolveExtensionConfig(requestContext.config, "download")?.onProgress;
2783
2824
  if (!onProgress || !shouldUseXHR(options.transport)) return;
@@ -3177,7 +3218,7 @@ function uploadPlugin() {
3177
3218
  formData: upload.data
3178
3219
  };
3179
3220
  });
3180
- context.hooks.onRequest(async (requestContext) => {
3221
+ context.hooks.onTransport(async (requestContext) => {
3181
3222
  if (requestContext.response) return;
3182
3223
  const upload = resolveExtensionConfig(requestContext.config, "upload");
3183
3224
  if (!upload?.onProgress) return;
package/dist/index.d.cts CHANGED
@@ -264,6 +264,11 @@ interface DownloadProgress {
264
264
  progress?: number;
265
265
  }
266
266
  interface DownloadOptions {
267
+ /**
268
+ * Reserved legacy field. It does not save or rename the returned Blob.
269
+ *
270
+ * @deprecated This field currently has no effect.
271
+ */
267
272
  filename?: string;
268
273
  /**
269
274
  * Called while the response stream is being consumed.
@@ -550,6 +555,14 @@ interface PluginInterceptorManager<T> {
550
555
  }
551
556
  interface PluginHookManager {
552
557
  onRequest(hook: RequestHook, options?: HookOptions): HookDisposer;
558
+ /**
559
+ * Provide an alternative transport for the current attempt.
560
+ *
561
+ * Transport hooks run inside the retry loop. A hook that handles the
562
+ * request assigns `context.response`; later transport hooks and the client
563
+ * adapter are then skipped for that attempt.
564
+ */
565
+ onTransport(hook: RequestHook, options?: HookOptions): HookDisposer;
553
566
  onResponse(hook: RequestHook, options?: HookOptions): HookDisposer;
554
567
  onError(hook: RequestHook, options?: HookOptions): HookDisposer;
555
568
  /**
package/dist/index.d.ts CHANGED
@@ -264,6 +264,11 @@ interface DownloadProgress {
264
264
  progress?: number;
265
265
  }
266
266
  interface DownloadOptions {
267
+ /**
268
+ * Reserved legacy field. It does not save or rename the returned Blob.
269
+ *
270
+ * @deprecated This field currently has no effect.
271
+ */
267
272
  filename?: string;
268
273
  /**
269
274
  * Called while the response stream is being consumed.
@@ -550,6 +555,14 @@ interface PluginInterceptorManager<T> {
550
555
  }
551
556
  interface PluginHookManager {
552
557
  onRequest(hook: RequestHook, options?: HookOptions): HookDisposer;
558
+ /**
559
+ * Provide an alternative transport for the current attempt.
560
+ *
561
+ * Transport hooks run inside the retry loop. A hook that handles the
562
+ * request assigns `context.response`; later transport hooks and the client
563
+ * adapter are then skipped for that attempt.
564
+ */
565
+ onTransport(hook: RequestHook, options?: HookOptions): HookDisposer;
553
566
  onResponse(hook: RequestHook, options?: HookOptions): HookDisposer;
554
567
  onError(hook: RequestHook, options?: HookOptions): HookDisposer;
555
568
  /**
package/dist/index.js CHANGED
@@ -68,6 +68,17 @@ function createTimeoutSignal(signal, timeout) {
68
68
  }
69
69
  function noop() {}
70
70
  //#endregion
71
+ //#region src/utils/isURLSearchParams.ts
72
+ function isURLSearchParams(value) {
73
+ if (typeof URLSearchParams === "undefined") return false;
74
+ try {
75
+ URLSearchParams.prototype.entries.call(value);
76
+ return true;
77
+ } catch {
78
+ return false;
79
+ }
80
+ }
81
+ //#endregion
71
82
  //#region src/utils/buildRequest.ts
72
83
  function buildRequest(config) {
73
84
  return buildRequestWithHeaders(config, new Headers(config.headers));
@@ -143,7 +154,7 @@ function buildBody(config, headers) {
143
154
  return config.body;
144
155
  }
145
156
  function buildURLSearchParams(form) {
146
- if (form instanceof URLSearchParams) return form;
157
+ if (isURLSearchParams(form)) return form;
147
158
  const params = new URLSearchParams();
148
159
  for (const key in form) if (Object.prototype.hasOwnProperty.call(form, key)) appendQuery(params, key, form[key]);
149
160
  return params;
@@ -609,8 +620,18 @@ const RESPONSE_TYPES = [
609
620
  "sse",
610
621
  "ndjson"
611
622
  ];
623
+ const HTTP_METHODS = [
624
+ "GET",
625
+ "POST",
626
+ "PUT",
627
+ "PATCH",
628
+ "DELETE",
629
+ "HEAD",
630
+ "OPTIONS"
631
+ ];
612
632
  function validateRequestConfig(config) {
613
633
  validateURL(config);
634
+ validateMethod(config);
614
635
  validateTimeout(config);
615
636
  validateLimits(config);
616
637
  validateQuery(config);
@@ -622,7 +643,10 @@ function validateRequestConfig(config) {
622
643
  }
623
644
  function validateQuery(config) {
624
645
  if (config.query !== void 0 && config.searchParams !== void 0) throw configError("Request query and searchParams are mutually exclusive", config);
625
- if (config.searchParams !== void 0 && !(config.searchParams instanceof URLSearchParams)) throw configError("Request searchParams must be URLSearchParams", 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);
626
650
  }
627
651
  function validateResponseOptions(config) {
628
652
  if (config.responseType !== void 0 && !RESPONSE_TYPES.includes(config.responseType)) throw configError("Request responseType is invalid", config);
@@ -996,7 +1020,7 @@ function createRawBody(data, headers, status) {
996
1020
  return JSON.stringify(data);
997
1021
  }
998
1022
  function isBodyInit(value) {
999
- return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData || typeof URLSearchParams !== "undefined" && value instanceof URLSearchParams || typeof ReadableStream !== "undefined" && value instanceof ReadableStream || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
1023
+ return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData || isURLSearchParams(value) || typeof ReadableStream !== "undefined" && value instanceof ReadableStream || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
1000
1024
  }
1001
1025
  function normalizeDelay$1(delay) {
1002
1026
  if (!Number.isFinite(delay)) return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
@@ -1061,7 +1085,7 @@ var ConfigMerger = class {
1061
1085
  static mergeQueryConfig(defaults, config) {
1062
1086
  if (config.searchParams !== void 0) return {
1063
1087
  query: config.query,
1064
- searchParams: config.searchParams instanceof URLSearchParams ? new URLSearchParams(config.searchParams) : config.searchParams
1088
+ searchParams: isURLSearchParams(config.searchParams) ? new URLSearchParams(config.searchParams) : config.searchParams
1065
1089
  };
1066
1090
  if (config.query !== void 0) return {
1067
1091
  query: this.mergeObject(defaults.searchParams ? void 0 : defaults.query, config.query),
@@ -1069,7 +1093,7 @@ var ConfigMerger = class {
1069
1093
  };
1070
1094
  return {
1071
1095
  query: defaults.query ? { ...defaults.query } : void 0,
1072
- searchParams: defaults.searchParams instanceof URLSearchParams ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
1096
+ searchParams: isURLSearchParams(defaults.searchParams) ? new URLSearchParams(defaults.searchParams) : defaults.searchParams
1073
1097
  };
1074
1098
  }
1075
1099
  static mergeFetchOptions(defaults, options) {
@@ -1205,7 +1229,8 @@ var Pipeline = class {
1205
1229
  while (true) try {
1206
1230
  const headers = validatedHeaders;
1207
1231
  validatedHeaders = void 0;
1208
- context.response = headers && this.adapter.requestValidated ? await this.adapter.requestValidated(context.config, headers, preserveRaw || this.hooks.hasResponseHooks || this.interceptors.response.active) : await this.adapter.request(context.config);
1232
+ if (this.hooks.hasTransportHooks) await this.hooks.runTransport(context);
1233
+ if (!context.response) context.response = headers && this.adapter.requestValidated ? await this.adapter.requestValidated(context.config, headers, preserveRaw || this.hooks.hasResponseHooks || this.interceptors.response.active) : await this.adapter.request(context.config);
1209
1234
  return await this.processResponse(context);
1210
1235
  } catch (error) {
1211
1236
  if (!await this.notifyError(context, error)) return this.fail(context, context.error, false);
@@ -1365,6 +1390,7 @@ function normalizePriority$1(priority) {
1365
1390
  var PluginHooks = class {
1366
1391
  constructor() {
1367
1392
  this.requestHooks = new HookRegistry();
1393
+ this.transportHooks = new HookRegistry();
1368
1394
  this.responseHooks = new HookRegistry();
1369
1395
  this.errorHooks = new HookRegistry();
1370
1396
  this.settledHooks = new HookRegistry();
@@ -1376,6 +1402,9 @@ var PluginHooks = class {
1376
1402
  get hasResponseHooks() {
1377
1403
  return this.responseHooks.active;
1378
1404
  }
1405
+ get hasTransportHooks() {
1406
+ return this.transportHooks.active;
1407
+ }
1379
1408
  get hasErrorHooks() {
1380
1409
  return this.errorHooks.active;
1381
1410
  }
@@ -1391,6 +1420,9 @@ var PluginHooks = class {
1391
1420
  onResponse(hook, options = {}) {
1392
1421
  return this.responseHooks.register(hook, options);
1393
1422
  }
1423
+ onTransport(hook, options = {}) {
1424
+ return this.transportHooks.register(hook, options);
1425
+ }
1394
1426
  onError(hook, options = {}) {
1395
1427
  return this.errorHooks.register(hook, options);
1396
1428
  }
@@ -1406,6 +1438,12 @@ var PluginHooks = class {
1406
1438
  async runResponse(context) {
1407
1439
  for (const hook of this.responseHooks.values()) await hook(context);
1408
1440
  }
1441
+ async runTransport(context) {
1442
+ for (const hook of this.transportHooks.values()) {
1443
+ await hook(context);
1444
+ if (context.response) return;
1445
+ }
1446
+ }
1409
1447
  async runError(context) {
1410
1448
  for (const hook of this.errorHooks.values()) await hook(context);
1411
1449
  }
@@ -1521,6 +1559,9 @@ function createScopedHooks(hooks, pluginPriority, cleanups) {
1521
1559
  onRequest(hook, options) {
1522
1560
  return trackHook(hooks.onRequest(hook, withPriority(options, pluginPriority)), cleanups);
1523
1561
  },
1562
+ onTransport(hook, options) {
1563
+ return trackHook(hooks.onTransport(hook, withPriority(options, pluginPriority)), cleanups);
1564
+ },
1524
1565
  onResponse(hook, options) {
1525
1566
  return trackHook(hooks.onResponse(hook, withPriority(options, pluginPriority)), cleanups);
1526
1567
  },
@@ -2147,7 +2188,7 @@ function normalizeCacheHeaders(headers, varyHeaders) {
2147
2188
  }
2148
2189
  function normalizeQuery(query) {
2149
2190
  if (!query) return [];
2150
- if (query instanceof URLSearchParams) return [...query.entries()];
2191
+ if (isURLSearchParams(query)) return [...query.entries()];
2151
2192
  const entries = [];
2152
2193
  for (const key of Object.keys(query).sort()) appendQueryValue(entries, key, query[key]);
2153
2194
  return entries;
@@ -2776,7 +2817,7 @@ function downloadPlugin(options = {}) {
2776
2817
  responseType
2777
2818
  };
2778
2819
  });
2779
- context.hooks.onRequest(async (requestContext) => {
2820
+ context.hooks.onTransport(async (requestContext) => {
2780
2821
  if (requestContext.response) return;
2781
2822
  const onProgress = resolveExtensionConfig(requestContext.config, "download")?.onProgress;
2782
2823
  if (!onProgress || !shouldUseXHR(options.transport)) return;
@@ -3176,7 +3217,7 @@ function uploadPlugin() {
3176
3217
  formData: upload.data
3177
3218
  };
3178
3219
  });
3179
- context.hooks.onRequest(async (requestContext) => {
3220
+ context.hooks.onTransport(async (requestContext) => {
3180
3221
  if (requestContext.response) return;
3181
3222
  const upload = resolveExtensionConfig(requestContext.config, "upload");
3182
3223
  if (!upload?.onProgress) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npora/request",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "A modern, TypeScript-first HTTP client powered by the Fetch API.",
5
5
  "keywords": [
6
6
  "npora",