@naturalcycles/js-lib 14.124.1 → 14.126.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/dist/http/fetcher.d.ts +18 -0
- package/dist/http/fetcher.js +21 -4
- package/dist-esm/http/fetcher.js +22 -5
- package/package.json +1 -1
- package/src/http/fetcher.ts +42 -4
package/dist/http/fetcher.d.ts
CHANGED
|
@@ -43,6 +43,16 @@ export interface FetcherCfg {
|
|
|
43
43
|
logRequestBody?: boolean;
|
|
44
44
|
logResponse?: boolean;
|
|
45
45
|
logResponseBody?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Default to true.
|
|
48
|
+
* Set to false to exclude `prefixUrl` from logs (both success and error)
|
|
49
|
+
*/
|
|
50
|
+
logWithPrefixUrl?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Default to true.
|
|
53
|
+
* Set to false to strip searchParams from url when logging (both success and error)
|
|
54
|
+
*/
|
|
55
|
+
logWithSearchParams?: boolean;
|
|
46
56
|
/**
|
|
47
57
|
* Defaults to `console`.
|
|
48
58
|
*/
|
|
@@ -89,6 +99,10 @@ export interface FetcherOptions {
|
|
|
89
99
|
*/
|
|
90
100
|
body?: Blob | BufferSource | FormData | URLSearchParams | string;
|
|
91
101
|
credentials?: RequestCredentials;
|
|
102
|
+
/**
|
|
103
|
+
* Default to true.
|
|
104
|
+
*/
|
|
105
|
+
followRedirects?: boolean;
|
|
92
106
|
headers?: Record<string, any>;
|
|
93
107
|
mode?: FetcherMode;
|
|
94
108
|
searchParams?: Record<string, any>;
|
|
@@ -166,6 +180,10 @@ export declare class Fetcher {
|
|
|
166
180
|
deleteVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
|
|
167
181
|
headVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
|
|
168
182
|
fetch<T = unknown>(url: string, opt?: FetcherOptions): Promise<T>;
|
|
183
|
+
/**
|
|
184
|
+
* Returns FetcherResponse.
|
|
185
|
+
* Never throws, returns `err` property in the response instead.
|
|
186
|
+
*/
|
|
169
187
|
rawFetch<T = unknown>(url: string, rawOpt?: FetcherOptions): Promise<FetcherResponse<T>>;
|
|
170
188
|
private processRetry;
|
|
171
189
|
/**
|
package/dist/http/fetcher.js
CHANGED
|
@@ -88,6 +88,10 @@ class Fetcher {
|
|
|
88
88
|
}
|
|
89
89
|
return res.body;
|
|
90
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Returns FetcherResponse.
|
|
93
|
+
* Never throws, returns `err` property in the response instead.
|
|
94
|
+
*/
|
|
91
95
|
async rawFetch(url, rawOpt = {}) {
|
|
92
96
|
const { logger } = this.cfg;
|
|
93
97
|
const req = this.normalizeOptions(url, rawOpt);
|
|
@@ -112,7 +116,8 @@ class Fetcher {
|
|
|
112
116
|
retryTimeout: req.retry.timeout,
|
|
113
117
|
},
|
|
114
118
|
};
|
|
115
|
-
const
|
|
119
|
+
const fullUrl = new URL(req.url);
|
|
120
|
+
const shortUrl = this.getShortUrl(fullUrl);
|
|
116
121
|
const signature = [method.toUpperCase(), shortUrl].join(' ');
|
|
117
122
|
/* eslint-disable no-await-in-loop */
|
|
118
123
|
while (!res.retryStatus.retryStopped) {
|
|
@@ -276,9 +281,18 @@ class Fetcher {
|
|
|
276
281
|
*/
|
|
277
282
|
getShortUrl(url) {
|
|
278
283
|
const { baseUrl } = this.cfg;
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
|
|
284
|
+
if (url.password) {
|
|
285
|
+
url = new URL(url.toString()); // prevent original url mutation
|
|
286
|
+
url.password = '[redacted]';
|
|
287
|
+
}
|
|
288
|
+
let shortUrl = url.toString();
|
|
289
|
+
if (!this.cfg.logWithSearchParams) {
|
|
290
|
+
shortUrl = shortUrl.split('?')[0];
|
|
291
|
+
}
|
|
292
|
+
if (!this.cfg.logWithPrefixUrl && baseUrl && shortUrl.startsWith(baseUrl)) {
|
|
293
|
+
shortUrl = shortUrl.slice(baseUrl.length);
|
|
294
|
+
}
|
|
295
|
+
return shortUrl;
|
|
282
296
|
}
|
|
283
297
|
normalizeCfg(cfg) {
|
|
284
298
|
if (cfg.baseUrl?.endsWith('/')) {
|
|
@@ -302,6 +316,8 @@ class Fetcher {
|
|
|
302
316
|
logRequestBody: debug,
|
|
303
317
|
logResponse: debug,
|
|
304
318
|
logResponseBody: debug,
|
|
319
|
+
logWithPrefixUrl: true,
|
|
320
|
+
logWithSearchParams: true,
|
|
305
321
|
retry: { ...defRetryOptions },
|
|
306
322
|
init: {
|
|
307
323
|
method: cfg.method || 'get',
|
|
@@ -332,6 +348,7 @@ class Fetcher {
|
|
|
332
348
|
...this.cfg.init,
|
|
333
349
|
method: opt.method || this.cfg.init.method,
|
|
334
350
|
credentials: opt.credentials || this.cfg.init.credentials,
|
|
351
|
+
redirect: opt.followRedirects ?? this.cfg.followRedirects ?? true ? 'follow' : 'error',
|
|
335
352
|
}, {
|
|
336
353
|
headers: (0, object_util_1._mapKeys)(opt.headers || {}, k => k.toLowerCase()),
|
|
337
354
|
}),
|
package/dist-esm/http/fetcher.js
CHANGED
|
@@ -77,6 +77,10 @@ export class Fetcher {
|
|
|
77
77
|
}
|
|
78
78
|
return res.body;
|
|
79
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns FetcherResponse.
|
|
82
|
+
* Never throws, returns `err` property in the response instead.
|
|
83
|
+
*/
|
|
80
84
|
async rawFetch(url, rawOpt = {}) {
|
|
81
85
|
var _a, e_1, _b, _c, _d, e_2, _e, _f;
|
|
82
86
|
var _g, _h, _j;
|
|
@@ -120,7 +124,8 @@ export class Fetcher {
|
|
|
120
124
|
retryTimeout: req.retry.timeout,
|
|
121
125
|
},
|
|
122
126
|
};
|
|
123
|
-
const
|
|
127
|
+
const fullUrl = new URL(req.url);
|
|
128
|
+
const shortUrl = this.getShortUrl(fullUrl);
|
|
124
129
|
const signature = [method.toUpperCase(), shortUrl].join(' ');
|
|
125
130
|
/* eslint-disable no-await-in-loop */
|
|
126
131
|
while (!res.retryStatus.retryStopped) {
|
|
@@ -316,9 +321,18 @@ export class Fetcher {
|
|
|
316
321
|
*/
|
|
317
322
|
getShortUrl(url) {
|
|
318
323
|
const { baseUrl } = this.cfg;
|
|
319
|
-
if (
|
|
320
|
-
|
|
321
|
-
|
|
324
|
+
if (url.password) {
|
|
325
|
+
url = new URL(url.toString()); // prevent original url mutation
|
|
326
|
+
url.password = '[redacted]';
|
|
327
|
+
}
|
|
328
|
+
let shortUrl = url.toString();
|
|
329
|
+
if (!this.cfg.logWithSearchParams) {
|
|
330
|
+
shortUrl = shortUrl.split('?')[0];
|
|
331
|
+
}
|
|
332
|
+
if (!this.cfg.logWithPrefixUrl && baseUrl && shortUrl.startsWith(baseUrl)) {
|
|
333
|
+
shortUrl = shortUrl.slice(baseUrl.length);
|
|
334
|
+
}
|
|
335
|
+
return shortUrl;
|
|
322
336
|
}
|
|
323
337
|
normalizeCfg(cfg) {
|
|
324
338
|
var _a;
|
|
@@ -343,6 +357,8 @@ export class Fetcher {
|
|
|
343
357
|
logRequestBody: debug,
|
|
344
358
|
logResponse: debug,
|
|
345
359
|
logResponseBody: debug,
|
|
360
|
+
logWithPrefixUrl: true,
|
|
361
|
+
logWithSearchParams: true,
|
|
346
362
|
retry: Object.assign({}, defRetryOptions),
|
|
347
363
|
init: {
|
|
348
364
|
method: cfg.method || 'get',
|
|
@@ -355,6 +371,7 @@ export class Fetcher {
|
|
|
355
371
|
return norm;
|
|
356
372
|
}
|
|
357
373
|
normalizeOptions(url, opt) {
|
|
374
|
+
var _a, _b;
|
|
358
375
|
const { baseUrl, timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode } = this.cfg;
|
|
359
376
|
const req = Object.assign(Object.assign({ mode,
|
|
360
377
|
url,
|
|
@@ -362,7 +379,7 @@ export class Fetcher {
|
|
|
362
379
|
throwHttpErrors,
|
|
363
380
|
retryPost,
|
|
364
381
|
retry4xx,
|
|
365
|
-
retry5xx }, _omit(opt, ['method', 'headers', 'credentials'])), { retry: Object.assign(Object.assign({}, retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials }), {
|
|
382
|
+
retry5xx }, _omit(opt, ['method', 'headers', 'credentials'])), { retry: Object.assign(Object.assign({}, retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials, redirect: ((_b = (_a = opt.followRedirects) !== null && _a !== void 0 ? _a : this.cfg.followRedirects) !== null && _b !== void 0 ? _b : true) ? 'follow' : 'error' }), {
|
|
366
383
|
headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),
|
|
367
384
|
}) });
|
|
368
385
|
// setup url
|
package/package.json
CHANGED
package/src/http/fetcher.ts
CHANGED
|
@@ -65,6 +65,18 @@ export interface FetcherCfg {
|
|
|
65
65
|
logResponse?: boolean
|
|
66
66
|
logResponseBody?: boolean
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Default to true.
|
|
70
|
+
* Set to false to exclude `prefixUrl` from logs (both success and error)
|
|
71
|
+
*/
|
|
72
|
+
logWithPrefixUrl?: boolean
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Default to true.
|
|
76
|
+
* Set to false to strip searchParams from url when logging (both success and error)
|
|
77
|
+
*/
|
|
78
|
+
logWithSearchParams?: boolean
|
|
79
|
+
|
|
68
80
|
/**
|
|
69
81
|
* Defaults to `console`.
|
|
70
82
|
*/
|
|
@@ -117,6 +129,10 @@ export interface FetcherOptions {
|
|
|
117
129
|
body?: Blob | BufferSource | FormData | URLSearchParams | string
|
|
118
130
|
|
|
119
131
|
credentials?: RequestCredentials
|
|
132
|
+
/**
|
|
133
|
+
* Default to true.
|
|
134
|
+
*/
|
|
135
|
+
followRedirects?: boolean
|
|
120
136
|
|
|
121
137
|
// Removing RequestInit from options to simplify FetcherOptions interface.
|
|
122
138
|
// Will instead only add hand-picked useful options, such as `credentials`.
|
|
@@ -287,6 +303,10 @@ export class Fetcher {
|
|
|
287
303
|
return res.body!
|
|
288
304
|
}
|
|
289
305
|
|
|
306
|
+
/**
|
|
307
|
+
* Returns FetcherResponse.
|
|
308
|
+
* Never throws, returns `err` property in the response instead.
|
|
309
|
+
*/
|
|
290
310
|
async rawFetch<T = unknown>(
|
|
291
311
|
url: string,
|
|
292
312
|
rawOpt: FetcherOptions = {},
|
|
@@ -323,7 +343,8 @@ export class Fetcher {
|
|
|
323
343
|
},
|
|
324
344
|
}
|
|
325
345
|
|
|
326
|
-
const
|
|
346
|
+
const fullUrl = new URL(req.url)
|
|
347
|
+
const shortUrl = this.getShortUrl(fullUrl)
|
|
327
348
|
const signature = [method.toUpperCase(), shortUrl].join(' ')
|
|
328
349
|
|
|
329
350
|
/* eslint-disable no-await-in-loop */
|
|
@@ -506,11 +527,25 @@ export class Fetcher {
|
|
|
506
527
|
/**
|
|
507
528
|
* Returns url without baseUrl and before ?queryString
|
|
508
529
|
*/
|
|
509
|
-
private getShortUrl(url:
|
|
530
|
+
private getShortUrl(url: URL): string {
|
|
510
531
|
const { baseUrl } = this.cfg
|
|
511
|
-
if (!baseUrl) return url
|
|
512
532
|
|
|
513
|
-
|
|
533
|
+
if (url.password) {
|
|
534
|
+
url = new URL(url.toString()) // prevent original url mutation
|
|
535
|
+
url.password = '[redacted]'
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
let shortUrl = url.toString()
|
|
539
|
+
|
|
540
|
+
if (!this.cfg.logWithSearchParams) {
|
|
541
|
+
shortUrl = shortUrl.split('?')[0]!
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (!this.cfg.logWithPrefixUrl && baseUrl && shortUrl.startsWith(baseUrl)) {
|
|
545
|
+
shortUrl = shortUrl.slice(baseUrl.length)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
return shortUrl
|
|
514
549
|
}
|
|
515
550
|
|
|
516
551
|
private normalizeCfg(cfg: FetcherCfg & FetcherOptions): FetcherNormalizedCfg {
|
|
@@ -537,6 +572,8 @@ export class Fetcher {
|
|
|
537
572
|
logRequestBody: debug,
|
|
538
573
|
logResponse: debug,
|
|
539
574
|
logResponseBody: debug,
|
|
575
|
+
logWithPrefixUrl: true,
|
|
576
|
+
logWithSearchParams: true,
|
|
540
577
|
retry: { ...defRetryOptions },
|
|
541
578
|
init: {
|
|
542
579
|
method: cfg.method || 'get',
|
|
@@ -575,6 +612,7 @@ export class Fetcher {
|
|
|
575
612
|
...this.cfg.init,
|
|
576
613
|
method: opt.method || this.cfg.init.method,
|
|
577
614
|
credentials: opt.credentials || this.cfg.init.credentials,
|
|
615
|
+
redirect: opt.followRedirects ?? this.cfg.followRedirects ?? true ? 'follow' : 'error',
|
|
578
616
|
},
|
|
579
617
|
{
|
|
580
618
|
headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),
|