@naturalcycles/js-lib 15.33.0 → 15.33.1
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/dist/http/fetcher.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import type { ReadableStream as WebReadableStream } from 'node:stream/web';
|
|
5
5
|
import { HttpRequestError } from '../error/error.util.js';
|
|
6
6
|
import type { ErrorDataTuple } from '../types.js';
|
|
7
|
-
import type { FetcherAfterResponseHook, FetcherBeforeRequestHook, FetcherBeforeRetryHook, FetcherCfg, FetcherGraphQLOptions, FetcherNormalizedCfg, FetcherOnErrorHook, FetcherOptions, FetcherResponse, RequestInitNormalized } from './fetcher.model.js';
|
|
7
|
+
import type { FetcherAfterResponseHook, FetcherBeforeRequestHook, FetcherBeforeRetryHook, FetcherCfg, FetcherGraphQLOptions, FetcherNormalizedCfg, FetcherOnErrorHook, FetcherOptions, FetcherResponse, FetchFunction, RequestInitNormalized } from './fetcher.model.js';
|
|
8
8
|
/**
|
|
9
9
|
* Experimental wrapper around Fetch.
|
|
10
10
|
* Works in both Browser and Node, using `globalThis.fetch`.
|
|
@@ -92,7 +92,7 @@ export declare class Fetcher {
|
|
|
92
92
|
* This method exists to be able to easily mock it.
|
|
93
93
|
* It is static, so mocking applies to ALL instances (even future ones) of Fetcher at once.
|
|
94
94
|
*/
|
|
95
|
-
static callNativeFetch(url: string, init: RequestInitNormalized): Promise<Response>;
|
|
95
|
+
static callNativeFetch(url: string, init: RequestInitNormalized, fetchFn?: FetchFunction): Promise<Response>;
|
|
96
96
|
private onNotOkResponse;
|
|
97
97
|
private processRetry;
|
|
98
98
|
private getRetryTimeout;
|
package/dist/http/fetcher.js
CHANGED
|
@@ -283,8 +283,7 @@ export class Fetcher {
|
|
|
283
283
|
}
|
|
284
284
|
}
|
|
285
285
|
try {
|
|
286
|
-
|
|
287
|
-
res.fetchResponse = await (this.cfg.fetchFn || Fetcher.callNativeFetch)(req.fullUrl, req.init);
|
|
286
|
+
res.fetchResponse = await Fetcher.callNativeFetch(req.fullUrl, req.init, this.cfg.fetchFn);
|
|
288
287
|
res.ok = res.fetchResponse.ok;
|
|
289
288
|
// important to set it to undefined, otherwise it can keep the previous value (from previous try)
|
|
290
289
|
res.err = undefined;
|
|
@@ -400,8 +399,8 @@ export class Fetcher {
|
|
|
400
399
|
* This method exists to be able to easily mock it.
|
|
401
400
|
* It is static, so mocking applies to ALL instances (even future ones) of Fetcher at once.
|
|
402
401
|
*/
|
|
403
|
-
static async callNativeFetch(url, init) {
|
|
404
|
-
return await
|
|
402
|
+
static async callNativeFetch(url, init, fetchFn = globalThis.fetch) {
|
|
403
|
+
return (await fetchFn(url, init));
|
|
405
404
|
}
|
|
406
405
|
async onNotOkResponse(res) {
|
|
407
406
|
let cause;
|
|
@@ -284,7 +284,26 @@ export type FetcherResponseType = 'json' | 'text' | 'void' | 'arrayBuffer' | 'bl
|
|
|
284
284
|
* Used to be able to override and provide a different implementation,
|
|
285
285
|
* e.g when mocking.
|
|
286
286
|
*/
|
|
287
|
-
export type FetchFunction = (url: string, init:
|
|
287
|
+
export type FetchFunction = (url: string, init: RequestInitCrossPlatform) => Promise<ResponseCrossPlatform>;
|
|
288
|
+
/**
|
|
289
|
+
* A subset of RequestInit that would match both:
|
|
290
|
+
*
|
|
291
|
+
* 1. RequestInit from dom types
|
|
292
|
+
* 2. RequestInit from undici types
|
|
293
|
+
*/
|
|
294
|
+
export interface RequestInitCrossPlatform {
|
|
295
|
+
method?: string;
|
|
296
|
+
referrer?: string;
|
|
297
|
+
keepalive?: boolean;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* A subset of Response type that matches both dom and undici types.
|
|
301
|
+
*/
|
|
302
|
+
export interface ResponseCrossPlatform {
|
|
303
|
+
ok: boolean;
|
|
304
|
+
status: number;
|
|
305
|
+
statusText: string;
|
|
306
|
+
}
|
|
288
307
|
export type GraphQLResponse<DATA> = GraphQLSuccessResponse<DATA> | GraphQLErrorResponse;
|
|
289
308
|
export interface GraphQLSuccessResponse<DATA> {
|
|
290
309
|
data: DATA;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.33.
|
|
4
|
+
"version": "15.33.1",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "^2",
|
|
7
7
|
"undici": "^7",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"@types/semver": "^7",
|
|
14
14
|
"crypto-js": "^4",
|
|
15
15
|
"dayjs": "^1",
|
|
16
|
-
"@naturalcycles/dev-lib": "
|
|
16
|
+
"@naturalcycles/dev-lib": "18.4.2"
|
|
17
17
|
},
|
|
18
18
|
"exports": {
|
|
19
19
|
".": "./dist/index.js",
|
|
@@ -364,7 +364,31 @@ export type FetcherResponseType =
|
|
|
364
364
|
* Used to be able to override and provide a different implementation,
|
|
365
365
|
* e.g when mocking.
|
|
366
366
|
*/
|
|
367
|
-
export type FetchFunction = (
|
|
367
|
+
export type FetchFunction = (
|
|
368
|
+
url: string,
|
|
369
|
+
init: RequestInitCrossPlatform,
|
|
370
|
+
) => Promise<ResponseCrossPlatform>
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* A subset of RequestInit that would match both:
|
|
374
|
+
*
|
|
375
|
+
* 1. RequestInit from dom types
|
|
376
|
+
* 2. RequestInit from undici types
|
|
377
|
+
*/
|
|
378
|
+
export interface RequestInitCrossPlatform {
|
|
379
|
+
method?: string
|
|
380
|
+
referrer?: string
|
|
381
|
+
keepalive?: boolean
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* A subset of Response type that matches both dom and undici types.
|
|
386
|
+
*/
|
|
387
|
+
export interface ResponseCrossPlatform {
|
|
388
|
+
ok: boolean
|
|
389
|
+
status: number
|
|
390
|
+
statusText: string
|
|
391
|
+
}
|
|
368
392
|
|
|
369
393
|
export type GraphQLResponse<DATA> = GraphQLSuccessResponse<DATA> | GraphQLErrorResponse
|
|
370
394
|
|
package/src/http/fetcher.ts
CHANGED
|
@@ -49,6 +49,7 @@ import type {
|
|
|
49
49
|
FetcherResponse,
|
|
50
50
|
FetcherResponseType,
|
|
51
51
|
FetcherRetryOptions,
|
|
52
|
+
FetchFunction,
|
|
52
53
|
GraphQLResponse,
|
|
53
54
|
RequestInitNormalized,
|
|
54
55
|
} from './fetcher.model.js'
|
|
@@ -369,11 +370,7 @@ export class Fetcher {
|
|
|
369
370
|
}
|
|
370
371
|
|
|
371
372
|
try {
|
|
372
|
-
|
|
373
|
-
res.fetchResponse = await (this.cfg.fetchFn || Fetcher.callNativeFetch)(
|
|
374
|
-
req.fullUrl,
|
|
375
|
-
req.init,
|
|
376
|
-
)
|
|
373
|
+
res.fetchResponse = await Fetcher.callNativeFetch(req.fullUrl, req.init, this.cfg.fetchFn)
|
|
377
374
|
res.ok = res.fetchResponse.ok
|
|
378
375
|
// important to set it to undefined, otherwise it can keep the previous value (from previous try)
|
|
379
376
|
res.err = undefined
|
|
@@ -502,8 +499,12 @@ export class Fetcher {
|
|
|
502
499
|
* This method exists to be able to easily mock it.
|
|
503
500
|
* It is static, so mocking applies to ALL instances (even future ones) of Fetcher at once.
|
|
504
501
|
*/
|
|
505
|
-
static async callNativeFetch(
|
|
506
|
-
|
|
502
|
+
static async callNativeFetch(
|
|
503
|
+
url: string,
|
|
504
|
+
init: RequestInitNormalized,
|
|
505
|
+
fetchFn: FetchFunction = globalThis.fetch,
|
|
506
|
+
): Promise<Response> {
|
|
507
|
+
return (await fetchFn(url, init)) as Response
|
|
507
508
|
}
|
|
508
509
|
|
|
509
510
|
private async onNotOkResponse(res: FetcherResponse): Promise<void> {
|