@naturalcycles/js-lib 14.131.2 → 14.133.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/fetcher.model.d.ts +2 -1
- package/dist/http/http.model.d.ts +1 -1
- package/dist/http/http.model.js +1 -1
- package/dist-esm/http/fetcher.js +10 -10
- package/dist-esm/http/http.model.js +1 -1
- package/package.json +1 -1
- package/src/http/fetcher.model.ts +4 -1
- package/src/http/fetcher.ts +10 -11
- package/src/http/http.model.ts +3 -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 {
|
|
348
|
+
const { timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode, jsonReviver, } = this.cfg;
|
|
350
349
|
const req = {
|
|
351
350
|
mode,
|
|
352
351
|
url,
|
|
@@ -371,6 +370,7 @@ class Fetcher {
|
|
|
371
370
|
}),
|
|
372
371
|
};
|
|
373
372
|
// setup url
|
|
373
|
+
const baseUrl = opt.baseUrl || this.cfg.baseUrl;
|
|
374
374
|
if (baseUrl) {
|
|
375
375
|
if (url.startsWith('/')) {
|
|
376
376
|
console.warn(`Fetcher: url should not start with / when baseUrl is specified`);
|
|
@@ -68,7 +68,7 @@ export interface FetcherRetryOptions {
|
|
|
68
68
|
timeoutMax: number;
|
|
69
69
|
timeoutMultiplier: number;
|
|
70
70
|
}
|
|
71
|
-
export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers'> {
|
|
71
|
+
export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers' | 'baseUrl'> {
|
|
72
72
|
url: string;
|
|
73
73
|
init: RequestInitNormalized;
|
|
74
74
|
mode: FetcherMode;
|
|
@@ -81,6 +81,7 @@ export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers
|
|
|
81
81
|
}
|
|
82
82
|
export interface FetcherOptions {
|
|
83
83
|
method?: HttpMethod;
|
|
84
|
+
baseUrl?: string;
|
|
84
85
|
throwHttpErrors?: boolean;
|
|
85
86
|
/**
|
|
86
87
|
* Default: 30.
|
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,7 +387,7 @@ export class Fetcher {
|
|
|
388
387
|
}
|
|
389
388
|
normalizeOptions(url, opt) {
|
|
390
389
|
var _a, _b;
|
|
391
|
-
const {
|
|
390
|
+
const { 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,
|
|
@@ -400,6 +399,7 @@ export class Fetcher {
|
|
|
400
399
|
headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),
|
|
401
400
|
}) });
|
|
402
401
|
// setup url
|
|
402
|
+
const baseUrl = opt.baseUrl || this.cfg.baseUrl;
|
|
403
403
|
if (baseUrl) {
|
|
404
404
|
if (url.startsWith('/')) {
|
|
405
405
|
console.warn(`Fetcher: url should not start with / when baseUrl is specified`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const HTTP_METHODS = ['
|
|
1
|
+
export const HTTP_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'];
|
package/package.json
CHANGED
|
@@ -79,7 +79,7 @@ export interface FetcherRetryOptions {
|
|
|
79
79
|
timeoutMultiplier: number
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers'> {
|
|
82
|
+
export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers' | 'baseUrl'> {
|
|
83
83
|
url: string
|
|
84
84
|
init: RequestInitNormalized
|
|
85
85
|
mode: FetcherMode
|
|
@@ -93,6 +93,9 @@ export interface FetcherRequest extends Omit<FetcherOptions, 'method' | 'headers
|
|
|
93
93
|
|
|
94
94
|
export interface FetcherOptions {
|
|
95
95
|
method?: HttpMethod
|
|
96
|
+
|
|
97
|
+
baseUrl?: string
|
|
98
|
+
|
|
96
99
|
throwHttpErrors?: boolean
|
|
97
100
|
/**
|
|
98
101
|
* Default: 30.
|
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
|
},
|
|
@@ -437,7 +436,6 @@ export class Fetcher {
|
|
|
437
436
|
|
|
438
437
|
private normalizeOptions(url: string, opt: FetcherOptions): FetcherRequest {
|
|
439
438
|
const {
|
|
440
|
-
baseUrl,
|
|
441
439
|
timeoutSeconds,
|
|
442
440
|
throwHttpErrors,
|
|
443
441
|
retryPost,
|
|
@@ -476,6 +474,7 @@ export class Fetcher {
|
|
|
476
474
|
}
|
|
477
475
|
|
|
478
476
|
// setup url
|
|
477
|
+
const baseUrl = opt.baseUrl || this.cfg.baseUrl
|
|
479
478
|
if (baseUrl) {
|
|
480
479
|
if (url.startsWith('/')) {
|
|
481
480
|
console.warn(`Fetcher: url should not start with / when baseUrl is specified`)
|
package/src/http/http.model.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
// Should be uppercase, because e.g this browser issue: https://github.com/axios/axios/issues/26
|
|
2
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD'
|
|
2
3
|
|
|
3
4
|
export type HttpStatusFamily = 5 | 4 | 3 | 2 | 1
|
|
4
5
|
|
|
5
|
-
export const HTTP_METHODS: HttpMethod[] = ['
|
|
6
|
+
export const HTTP_METHODS: HttpMethod[] = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD']
|