@naturalcycles/js-lib 14.175.0 → 14.176.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.
@@ -204,7 +204,8 @@ class Fetcher {
204
204
  }
205
205
  }
206
206
  try {
207
- res.fetchResponse = await Fetcher.callNativeFetch(req.fullUrl, req.init);
207
+ // Calls cfg.fetchFn if set, otherwise Fetcher.callNativeFetch
208
+ res.fetchResponse = await (this.cfg.fetchFn || Fetcher.callNativeFetch)(req.fullUrl, req.init);
208
209
  res.ok = res.fetchResponse.ok;
209
210
  // important to set it to undefined, otherwise it can keep the previous value (from previous try)
210
211
  res.err = undefined;
@@ -178,6 +178,10 @@ export interface FetcherOptions {
178
178
  * If true - enables all possible logging.
179
179
  */
180
180
  debug?: boolean;
181
+ /**
182
+ * If provided - will be used instead of static `Fetcher.callNativeFetch`.
183
+ */
184
+ fetchFn?: FetchFunction;
181
185
  }
182
186
  export type RequestInitNormalized = Omit<RequestInit, 'method' | 'headers'> & {
183
187
  method: HttpMethod;
@@ -207,3 +211,9 @@ export interface FetcherErrorResponse<BODY = unknown> {
207
211
  }
208
212
  export type FetcherResponse<BODY = unknown> = FetcherSuccessResponse<BODY> | FetcherErrorResponse<BODY>;
209
213
  export type FetcherResponseType = 'json' | 'text' | 'void' | 'arrayBuffer' | 'blob' | 'readableStream';
214
+ /**
215
+ * Signature for the `fetch` function.
216
+ * Used to be able to override and provide a different implementation,
217
+ * e.g when mocking.
218
+ */
219
+ export type FetchFunction = (url: string, init: RequestInitNormalized) => Promise<Response>;
@@ -182,7 +182,8 @@ export class Fetcher {
182
182
  }
183
183
  }
184
184
  try {
185
- res.fetchResponse = await Fetcher.callNativeFetch(req.fullUrl, req.init);
185
+ // Calls cfg.fetchFn if set, otherwise Fetcher.callNativeFetch
186
+ res.fetchResponse = await (this.cfg.fetchFn || Fetcher.callNativeFetch)(req.fullUrl, req.init);
186
187
  res.ok = res.fetchResponse.ok;
187
188
  // important to set it to undefined, otherwise it can keep the previous value (from previous try)
188
189
  res.err = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.175.0",
3
+ "version": "14.176.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -227,6 +227,11 @@ export interface FetcherOptions {
227
227
  * If true - enables all possible logging.
228
228
  */
229
229
  debug?: boolean
230
+
231
+ /**
232
+ * If provided - will be used instead of static `Fetcher.callNativeFetch`.
233
+ */
234
+ fetchFn?: FetchFunction
230
235
  }
231
236
 
232
237
  export type RequestInitNormalized = Omit<RequestInit, 'method' | 'headers'> & {
@@ -269,3 +274,10 @@ export type FetcherResponseType =
269
274
  | 'arrayBuffer'
270
275
  | 'blob'
271
276
  | 'readableStream'
277
+
278
+ /**
279
+ * Signature for the `fetch` function.
280
+ * Used to be able to override and provide a different implementation,
281
+ * e.g when mocking.
282
+ */
283
+ export type FetchFunction = (url: string, init: RequestInitNormalized) => Promise<Response>
@@ -286,7 +286,11 @@ export class Fetcher {
286
286
  }
287
287
 
288
288
  try {
289
- res.fetchResponse = await Fetcher.callNativeFetch(req.fullUrl, req.init)
289
+ // Calls cfg.fetchFn if set, otherwise Fetcher.callNativeFetch
290
+ res.fetchResponse = await (this.cfg.fetchFn || Fetcher.callNativeFetch)(
291
+ req.fullUrl,
292
+ req.init,
293
+ )
290
294
  res.ok = res.fetchResponse.ok
291
295
  // important to set it to undefined, otherwise it can keep the previous value (from previous try)
292
296
  res.err = undefined