@naturalcycles/js-lib 14.131.1 → 14.132.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.js +10 -10
- package/dist/http/http.model.d.ts +1 -1
- package/dist/http/http.model.js +1 -1
- package/dist-esm/http/fetcher.js +11 -11
- package/dist-esm/http/http.model.js +1 -1
- package/package.json +1 -1
- package/src/http/fetcher.ts +21 -12
- package/src/http/http.model.ts +2 -2
package/dist/http/fetcher.js
CHANGED
|
@@ -30,26 +30,25 @@ class Fetcher {
|
|
|
30
30
|
this.cfg = this.normalizeCfg(cfg);
|
|
31
31
|
// Dynamically create all helper methods
|
|
32
32
|
http_model_1.HTTP_METHODS.forEach(method => {
|
|
33
|
-
|
|
34
|
-
this[`${
|
|
33
|
+
const m = method.toLowerCase();
|
|
34
|
+
this[`${m}Void`] = async (url, opt) => {
|
|
35
35
|
return await this.fetch(url, {
|
|
36
36
|
method,
|
|
37
37
|
mode: 'void',
|
|
38
38
|
...opt,
|
|
39
39
|
});
|
|
40
40
|
};
|
|
41
|
-
if (method === '
|
|
41
|
+
if (method === 'HEAD')
|
|
42
42
|
return // mode=text
|
|
43
43
|
;
|
|
44
|
-
this[`${
|
|
44
|
+
this[`${m}Text`] = async (url, opt) => {
|
|
45
45
|
return await this.fetch(url, {
|
|
46
46
|
method,
|
|
47
47
|
mode: 'text',
|
|
48
48
|
...opt,
|
|
49
49
|
});
|
|
50
50
|
};
|
|
51
|
-
|
|
52
|
-
this[method] = async (url, opt) => {
|
|
51
|
+
this[m] = async (url, opt) => {
|
|
53
52
|
return await this.fetch(url, {
|
|
54
53
|
method,
|
|
55
54
|
mode: 'json',
|
|
@@ -118,7 +117,7 @@ class Fetcher {
|
|
|
118
117
|
};
|
|
119
118
|
const fullUrl = new URL(req.url);
|
|
120
119
|
const shortUrl = this.getShortUrl(fullUrl);
|
|
121
|
-
const signature = [method
|
|
120
|
+
const signature = [method, shortUrl].join(' ');
|
|
122
121
|
/* eslint-disable no-await-in-loop */
|
|
123
122
|
while (!res.retryStatus.retryStopped) {
|
|
124
123
|
const started = Date.now();
|
|
@@ -263,7 +262,7 @@ class Fetcher {
|
|
|
263
262
|
shouldRetry(res) {
|
|
264
263
|
const { retryPost, retry4xx, retry5xx } = res.req;
|
|
265
264
|
const { method } = res.req.init;
|
|
266
|
-
if (method === '
|
|
265
|
+
if (method === 'POST' && !retryPost)
|
|
267
266
|
return false;
|
|
268
267
|
const { statusFamily } = res;
|
|
269
268
|
const statusCode = res.fetchResponse?.status || 0;
|
|
@@ -336,7 +335,7 @@ class Fetcher {
|
|
|
336
335
|
logWithSearchParams: true,
|
|
337
336
|
retry: { ...defRetryOptions },
|
|
338
337
|
init: {
|
|
339
|
-
method: cfg.method || '
|
|
338
|
+
method: cfg.method || 'GET',
|
|
340
339
|
headers: cfg.headers || {},
|
|
341
340
|
credentials: cfg.credentials,
|
|
342
341
|
},
|
|
@@ -346,7 +345,7 @@ class Fetcher {
|
|
|
346
345
|
return norm;
|
|
347
346
|
}
|
|
348
347
|
normalizeOptions(url, opt) {
|
|
349
|
-
const { baseUrl, timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode } = this.cfg;
|
|
348
|
+
const { baseUrl, timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode, jsonReviver, } = this.cfg;
|
|
350
349
|
const req = {
|
|
351
350
|
mode,
|
|
352
351
|
url,
|
|
@@ -355,6 +354,7 @@ class Fetcher {
|
|
|
355
354
|
retryPost,
|
|
356
355
|
retry4xx,
|
|
357
356
|
retry5xx,
|
|
357
|
+
jsonReviver,
|
|
358
358
|
...(0, object_util_1._omit)(opt, ['method', 'headers', 'credentials']),
|
|
359
359
|
retry: {
|
|
360
360
|
...retry,
|
package/dist/http/http.model.js
CHANGED
package/dist-esm/http/fetcher.js
CHANGED
|
@@ -28,18 +28,17 @@ export class Fetcher {
|
|
|
28
28
|
this.cfg = this.normalizeCfg(cfg);
|
|
29
29
|
// Dynamically create all helper methods
|
|
30
30
|
HTTP_METHODS.forEach(method => {
|
|
31
|
-
|
|
32
|
-
this[`${
|
|
31
|
+
const m = method.toLowerCase();
|
|
32
|
+
this[`${m}Void`] = async (url, opt) => {
|
|
33
33
|
return await this.fetch(url, Object.assign({ method, mode: 'void' }, opt));
|
|
34
34
|
};
|
|
35
|
-
if (method === '
|
|
35
|
+
if (method === 'HEAD')
|
|
36
36
|
return // mode=text
|
|
37
37
|
;
|
|
38
|
-
this[`${
|
|
38
|
+
this[`${m}Text`] = async (url, opt) => {
|
|
39
39
|
return await this.fetch(url, Object.assign({ method, mode: 'text' }, opt));
|
|
40
40
|
};
|
|
41
|
-
|
|
42
|
-
this[method] = async (url, opt) => {
|
|
41
|
+
this[m] = async (url, opt) => {
|
|
43
42
|
return await this.fetch(url, Object.assign({ method, mode: 'json' }, opt));
|
|
44
43
|
};
|
|
45
44
|
});
|
|
@@ -126,7 +125,7 @@ export class Fetcher {
|
|
|
126
125
|
};
|
|
127
126
|
const fullUrl = new URL(req.url);
|
|
128
127
|
const shortUrl = this.getShortUrl(fullUrl);
|
|
129
|
-
const signature = [method
|
|
128
|
+
const signature = [method, shortUrl].join(' ');
|
|
130
129
|
/* eslint-disable no-await-in-loop */
|
|
131
130
|
while (!res.retryStatus.retryStopped) {
|
|
132
131
|
const started = Date.now();
|
|
@@ -302,7 +301,7 @@ export class Fetcher {
|
|
|
302
301
|
var _a;
|
|
303
302
|
const { retryPost, retry4xx, retry5xx } = res.req;
|
|
304
303
|
const { method } = res.req.init;
|
|
305
|
-
if (method === '
|
|
304
|
+
if (method === 'POST' && !retryPost)
|
|
306
305
|
return false;
|
|
307
306
|
const { statusFamily } = res;
|
|
308
307
|
const statusCode = ((_a = res.fetchResponse) === null || _a === void 0 ? void 0 : _a.status) || 0;
|
|
@@ -377,7 +376,7 @@ export class Fetcher {
|
|
|
377
376
|
logWithSearchParams: true,
|
|
378
377
|
retry: Object.assign({}, defRetryOptions),
|
|
379
378
|
init: {
|
|
380
|
-
method: cfg.method || '
|
|
379
|
+
method: cfg.method || 'GET',
|
|
381
380
|
headers: cfg.headers || {},
|
|
382
381
|
credentials: cfg.credentials,
|
|
383
382
|
},
|
|
@@ -388,14 +387,15 @@ export class Fetcher {
|
|
|
388
387
|
}
|
|
389
388
|
normalizeOptions(url, opt) {
|
|
390
389
|
var _a, _b;
|
|
391
|
-
const { baseUrl, timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode } = this.cfg;
|
|
390
|
+
const { baseUrl, timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode, jsonReviver, } = this.cfg;
|
|
392
391
|
const req = Object.assign(Object.assign({ mode,
|
|
393
392
|
url,
|
|
394
393
|
timeoutSeconds,
|
|
395
394
|
throwHttpErrors,
|
|
396
395
|
retryPost,
|
|
397
396
|
retry4xx,
|
|
398
|
-
retry5xx
|
|
397
|
+
retry5xx,
|
|
398
|
+
jsonReviver }, _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' }), {
|
|
399
399
|
headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),
|
|
400
400
|
}) });
|
|
401
401
|
// setup url
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const HTTP_METHODS = ['
|
|
1
|
+
export const HTTP_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'];
|
package/package.json
CHANGED
package/src/http/fetcher.ts
CHANGED
|
@@ -51,8 +51,10 @@ export class Fetcher {
|
|
|
51
51
|
|
|
52
52
|
// Dynamically create all helper methods
|
|
53
53
|
HTTP_METHODS.forEach(method => {
|
|
54
|
+
const m = method.toLowerCase()
|
|
55
|
+
|
|
54
56
|
// mode=void
|
|
55
|
-
this[`${
|
|
57
|
+
;(this as any)[`${m}Void`] = async (url: string, opt?: FetcherOptions): Promise<void> => {
|
|
56
58
|
return await this.fetch<void>(url, {
|
|
57
59
|
method,
|
|
58
60
|
mode: 'void',
|
|
@@ -60,11 +62,8 @@ export class Fetcher {
|
|
|
60
62
|
})
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
if (method === '
|
|
64
|
-
;(this as any)[`${
|
|
65
|
-
url: string,
|
|
66
|
-
opt?: FetcherOptions,
|
|
67
|
-
): Promise<string> => {
|
|
65
|
+
if (method === 'HEAD') return // mode=text
|
|
66
|
+
;(this as any)[`${m}Text`] = async (url: string, opt?: FetcherOptions): Promise<string> => {
|
|
68
67
|
return await this.fetch<string>(url, {
|
|
69
68
|
method,
|
|
70
69
|
mode: 'text',
|
|
@@ -73,7 +72,7 @@ export class Fetcher {
|
|
|
73
72
|
}
|
|
74
73
|
|
|
75
74
|
// Default mode=json, but overridable
|
|
76
|
-
this[
|
|
75
|
+
;(this as any)[m] = async <T = unknown>(url: string, opt?: FetcherOptions): Promise<T> => {
|
|
77
76
|
return await this.fetch<T>(url, {
|
|
78
77
|
method,
|
|
79
78
|
mode: 'json',
|
|
@@ -181,7 +180,7 @@ export class Fetcher {
|
|
|
181
180
|
|
|
182
181
|
const fullUrl = new URL(req.url)
|
|
183
182
|
const shortUrl = this.getShortUrl(fullUrl)
|
|
184
|
-
const signature = [method
|
|
183
|
+
const signature = [method, shortUrl].join(' ')
|
|
185
184
|
|
|
186
185
|
/* eslint-disable no-await-in-loop */
|
|
187
186
|
while (!res.retryStatus.retryStopped) {
|
|
@@ -347,7 +346,7 @@ export class Fetcher {
|
|
|
347
346
|
private shouldRetry(res: FetcherResponse): boolean {
|
|
348
347
|
const { retryPost, retry4xx, retry5xx } = res.req
|
|
349
348
|
const { method } = res.req.init
|
|
350
|
-
if (method === '
|
|
349
|
+
if (method === 'POST' && !retryPost) return false
|
|
351
350
|
const { statusFamily } = res
|
|
352
351
|
const statusCode = res.fetchResponse?.status || 0
|
|
353
352
|
if (statusFamily === 5 && !retry5xx) return false
|
|
@@ -421,7 +420,7 @@ export class Fetcher {
|
|
|
421
420
|
logWithSearchParams: true,
|
|
422
421
|
retry: { ...defRetryOptions },
|
|
423
422
|
init: {
|
|
424
|
-
method: cfg.method || '
|
|
423
|
+
method: cfg.method || 'GET',
|
|
425
424
|
headers: cfg.headers || {},
|
|
426
425
|
credentials: cfg.credentials,
|
|
427
426
|
},
|
|
@@ -436,8 +435,17 @@ export class Fetcher {
|
|
|
436
435
|
}
|
|
437
436
|
|
|
438
437
|
private normalizeOptions(url: string, opt: FetcherOptions): FetcherRequest {
|
|
439
|
-
const {
|
|
440
|
-
|
|
438
|
+
const {
|
|
439
|
+
baseUrl,
|
|
440
|
+
timeoutSeconds,
|
|
441
|
+
throwHttpErrors,
|
|
442
|
+
retryPost,
|
|
443
|
+
retry4xx,
|
|
444
|
+
retry5xx,
|
|
445
|
+
retry,
|
|
446
|
+
mode,
|
|
447
|
+
jsonReviver,
|
|
448
|
+
} = this.cfg
|
|
441
449
|
|
|
442
450
|
const req: FetcherRequest = {
|
|
443
451
|
mode,
|
|
@@ -447,6 +455,7 @@ export class Fetcher {
|
|
|
447
455
|
retryPost,
|
|
448
456
|
retry4xx,
|
|
449
457
|
retry5xx,
|
|
458
|
+
jsonReviver,
|
|
450
459
|
..._omit(opt, ['method', 'headers', 'credentials']),
|
|
451
460
|
retry: {
|
|
452
461
|
...retry,
|
package/src/http/http.model.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export type HttpMethod = '
|
|
1
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD'
|
|
2
2
|
|
|
3
3
|
export type HttpStatusFamily = 5 | 4 | 3 | 2 | 1
|
|
4
4
|
|
|
5
|
-
export const HTTP_METHODS: HttpMethod[] = ['
|
|
5
|
+
export const HTTP_METHODS: HttpMethod[] = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD']
|