@naturalcycles/js-lib 15.39.0 → 15.40.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 +6 -2
- package/dist/http/fetcher.js +24 -17
- package/dist/object/map2.d.ts +4 -0
- package/dist/object/map2.js +9 -0
- package/dist/object/set2.d.ts +5 -0
- package/dist/object/set2.js +10 -0
- package/package.json +1 -1
- package/src/http/fetcher.ts +27 -20
- package/src/object/map2.ts +10 -0
- package/src/object/set2.ts +11 -0
package/dist/http/fetcher.d.ts
CHANGED
|
@@ -16,8 +16,12 @@ export declare class Fetcher {
|
|
|
16
16
|
*
|
|
17
17
|
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
18
18
|
*/
|
|
19
|
-
static readonly VERSION =
|
|
20
|
-
|
|
19
|
+
static readonly VERSION = 3;
|
|
20
|
+
/**
|
|
21
|
+
* userAgent is statically exposed as Fetcher.userAgent.
|
|
22
|
+
* It can be modified globally, and will be used (read) at the start of every request.
|
|
23
|
+
*/
|
|
24
|
+
static userAgent: string | undefined;
|
|
21
25
|
private constructor();
|
|
22
26
|
/**
|
|
23
27
|
* Add BeforeRequest hook at the end of the hooks list.
|
package/dist/http/fetcher.js
CHANGED
|
@@ -12,20 +12,6 @@ import { pTimeout } from '../promise/pTimeout.js';
|
|
|
12
12
|
import { _jsonParse, _jsonParseIfPossible } from '../string/json.util.js';
|
|
13
13
|
import { _stringify } from '../string/stringify.js';
|
|
14
14
|
import { HTTP_METHODS } from './http.model.js';
|
|
15
|
-
const acceptByResponseType = {
|
|
16
|
-
text: 'text/plain',
|
|
17
|
-
json: 'application/json',
|
|
18
|
-
void: '*/*',
|
|
19
|
-
readableStream: 'application/octet-stream',
|
|
20
|
-
arrayBuffer: 'application/octet-stream',
|
|
21
|
-
blob: 'application/octet-stream',
|
|
22
|
-
};
|
|
23
|
-
const defRetryOptions = {
|
|
24
|
-
count: 2,
|
|
25
|
-
timeout: 1000,
|
|
26
|
-
timeoutMax: 30_000,
|
|
27
|
-
timeoutMultiplier: 2,
|
|
28
|
-
};
|
|
29
15
|
/**
|
|
30
16
|
* Experimental wrapper around Fetch.
|
|
31
17
|
* Works in both Browser and Node, using `globalThis.fetch`.
|
|
@@ -37,7 +23,11 @@ export class Fetcher {
|
|
|
37
23
|
*
|
|
38
24
|
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
39
25
|
*/
|
|
40
|
-
static VERSION =
|
|
26
|
+
static VERSION = 3;
|
|
27
|
+
/**
|
|
28
|
+
* userAgent is statically exposed as Fetcher.userAgent.
|
|
29
|
+
* It can be modified globally, and will be used (read) at the start of every request.
|
|
30
|
+
*/
|
|
41
31
|
static userAgent = isServerSide() ? `fetcher/${this.VERSION}` : undefined;
|
|
42
32
|
constructor(cfg = {}) {
|
|
43
33
|
if (typeof globalThis.fetch !== 'function') {
|
|
@@ -614,7 +604,7 @@ export class Fetcher {
|
|
|
614
604
|
logResponseBody: debug,
|
|
615
605
|
logWithBaseUrl: isServerSide(),
|
|
616
606
|
logWithSearchParams: true,
|
|
617
|
-
retry: { ...
|
|
607
|
+
retry: { ...defaultRetryOptions },
|
|
618
608
|
init: {
|
|
619
609
|
method: cfg.method || 'GET',
|
|
620
610
|
headers: _filterNullishValues({
|
|
@@ -659,7 +649,10 @@ export class Fetcher {
|
|
|
659
649
|
},
|
|
660
650
|
init: _merge({
|
|
661
651
|
...this.cfg.init,
|
|
662
|
-
headers: {
|
|
652
|
+
headers: {
|
|
653
|
+
...this.cfg.init.headers, // this avoids mutation
|
|
654
|
+
'user-agent': Fetcher.userAgent, // re-load it here, to support setting it globally post-fetcher-creation
|
|
655
|
+
},
|
|
663
656
|
method: opt.method || this.cfg.init.method,
|
|
664
657
|
credentials: opt.credentials || this.cfg.init.credentials,
|
|
665
658
|
redirect: opt.redirect || this.cfg.init.redirect || 'follow',
|
|
@@ -716,3 +709,17 @@ export class Fetcher {
|
|
|
716
709
|
export function getFetcher(cfg = {}) {
|
|
717
710
|
return Fetcher.create(cfg);
|
|
718
711
|
}
|
|
712
|
+
const acceptByResponseType = {
|
|
713
|
+
text: 'text/plain',
|
|
714
|
+
json: 'application/json',
|
|
715
|
+
void: '*/*',
|
|
716
|
+
readableStream: 'application/octet-stream',
|
|
717
|
+
arrayBuffer: 'application/octet-stream',
|
|
718
|
+
blob: 'application/octet-stream',
|
|
719
|
+
};
|
|
720
|
+
const defaultRetryOptions = {
|
|
721
|
+
count: 2,
|
|
722
|
+
timeout: 1000,
|
|
723
|
+
timeoutMax: 30_000,
|
|
724
|
+
timeoutMultiplier: 2,
|
|
725
|
+
};
|
package/dist/object/map2.d.ts
CHANGED
|
@@ -10,6 +10,10 @@ export declare class Map2<K = any, V = any> extends Map<K, V> {
|
|
|
10
10
|
* Convenience way to create Map2 from object.
|
|
11
11
|
*/
|
|
12
12
|
static of<V>(obj: Record<any, V>): Map2<string, V>;
|
|
13
|
+
/**
|
|
14
|
+
* Allows to set multiple key-value pairs at once.
|
|
15
|
+
*/
|
|
16
|
+
setMany(obj: Record<any, V>): this;
|
|
13
17
|
toObject(): Record<string, V>;
|
|
14
18
|
toJSON(): Record<string, V>;
|
|
15
19
|
}
|
package/dist/object/map2.js
CHANGED
|
@@ -12,6 +12,15 @@ export class Map2 extends Map {
|
|
|
12
12
|
static of(obj) {
|
|
13
13
|
return new Map2(Object.entries(obj));
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Allows to set multiple key-value pairs at once.
|
|
17
|
+
*/
|
|
18
|
+
setMany(obj) {
|
|
19
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
20
|
+
this.set(k, v);
|
|
21
|
+
}
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
15
24
|
toObject() {
|
|
16
25
|
return Object.fromEntries(this);
|
|
17
26
|
}
|
package/dist/object/set2.d.ts
CHANGED
|
@@ -6,6 +6,11 @@
|
|
|
6
6
|
* @experimental
|
|
7
7
|
*/
|
|
8
8
|
export declare class Set2<T = any> extends Set<T> {
|
|
9
|
+
/**
|
|
10
|
+
* Like .add(), but allows to add multiple items at once.
|
|
11
|
+
* Mutates the Set, but also returns it conveniently.
|
|
12
|
+
*/
|
|
13
|
+
addMany(items: Iterable<T>): this;
|
|
9
14
|
toArray(): T[];
|
|
10
15
|
toJSON(): T[];
|
|
11
16
|
}
|
package/dist/object/set2.js
CHANGED
|
@@ -6,6 +6,16 @@
|
|
|
6
6
|
* @experimental
|
|
7
7
|
*/
|
|
8
8
|
export class Set2 extends Set {
|
|
9
|
+
/**
|
|
10
|
+
* Like .add(), but allows to add multiple items at once.
|
|
11
|
+
* Mutates the Set, but also returns it conveniently.
|
|
12
|
+
*/
|
|
13
|
+
addMany(items) {
|
|
14
|
+
for (const item of items) {
|
|
15
|
+
this.add(item);
|
|
16
|
+
}
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
9
19
|
toArray() {
|
|
10
20
|
return [...this];
|
|
11
21
|
}
|
package/package.json
CHANGED
package/src/http/fetcher.ts
CHANGED
|
@@ -56,22 +56,6 @@ import type {
|
|
|
56
56
|
import type { HttpStatusFamily } from './http.model.js'
|
|
57
57
|
import { HTTP_METHODS } from './http.model.js'
|
|
58
58
|
|
|
59
|
-
const acceptByResponseType: Record<FetcherResponseType, string> = {
|
|
60
|
-
text: 'text/plain',
|
|
61
|
-
json: 'application/json',
|
|
62
|
-
void: '*/*',
|
|
63
|
-
readableStream: 'application/octet-stream',
|
|
64
|
-
arrayBuffer: 'application/octet-stream',
|
|
65
|
-
blob: 'application/octet-stream',
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const defRetryOptions: FetcherRetryOptions = {
|
|
69
|
-
count: 2,
|
|
70
|
-
timeout: 1000,
|
|
71
|
-
timeoutMax: 30_000,
|
|
72
|
-
timeoutMultiplier: 2,
|
|
73
|
-
}
|
|
74
|
-
|
|
75
59
|
/**
|
|
76
60
|
* Experimental wrapper around Fetch.
|
|
77
61
|
* Works in both Browser and Node, using `globalThis.fetch`.
|
|
@@ -83,8 +67,12 @@ export class Fetcher {
|
|
|
83
67
|
*
|
|
84
68
|
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
85
69
|
*/
|
|
86
|
-
static readonly VERSION =
|
|
87
|
-
|
|
70
|
+
static readonly VERSION = 3
|
|
71
|
+
/**
|
|
72
|
+
* userAgent is statically exposed as Fetcher.userAgent.
|
|
73
|
+
* It can be modified globally, and will be used (read) at the start of every request.
|
|
74
|
+
*/
|
|
75
|
+
static userAgent = isServerSide() ? `fetcher/${this.VERSION}` : undefined
|
|
88
76
|
|
|
89
77
|
private constructor(cfg: FetcherCfg & FetcherOptions = {}) {
|
|
90
78
|
if (typeof globalThis.fetch !== 'function') {
|
|
@@ -750,7 +738,7 @@ export class Fetcher {
|
|
|
750
738
|
logResponseBody: debug,
|
|
751
739
|
logWithBaseUrl: isServerSide(),
|
|
752
740
|
logWithSearchParams: true,
|
|
753
|
-
retry: { ...
|
|
741
|
+
retry: { ...defaultRetryOptions },
|
|
754
742
|
init: {
|
|
755
743
|
method: cfg.method || 'GET',
|
|
756
744
|
headers: _filterNullishValues({
|
|
@@ -801,7 +789,10 @@ export class Fetcher {
|
|
|
801
789
|
init: _merge(
|
|
802
790
|
{
|
|
803
791
|
...this.cfg.init,
|
|
804
|
-
headers: {
|
|
792
|
+
headers: {
|
|
793
|
+
...this.cfg.init.headers, // this avoids mutation
|
|
794
|
+
'user-agent': Fetcher.userAgent, // re-load it here, to support setting it globally post-fetcher-creation
|
|
795
|
+
},
|
|
805
796
|
method: opt.method || this.cfg.init.method,
|
|
806
797
|
credentials: opt.credentials || this.cfg.init.credentials,
|
|
807
798
|
redirect: opt.redirect || this.cfg.init.redirect || 'follow',
|
|
@@ -864,3 +855,19 @@ export class Fetcher {
|
|
|
864
855
|
export function getFetcher(cfg: FetcherCfg & FetcherOptions = {}): Fetcher {
|
|
865
856
|
return Fetcher.create(cfg)
|
|
866
857
|
}
|
|
858
|
+
|
|
859
|
+
const acceptByResponseType: Record<FetcherResponseType, string> = {
|
|
860
|
+
text: 'text/plain',
|
|
861
|
+
json: 'application/json',
|
|
862
|
+
void: '*/*',
|
|
863
|
+
readableStream: 'application/octet-stream',
|
|
864
|
+
arrayBuffer: 'application/octet-stream',
|
|
865
|
+
blob: 'application/octet-stream',
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
const defaultRetryOptions: FetcherRetryOptions = {
|
|
869
|
+
count: 2,
|
|
870
|
+
timeout: 1000,
|
|
871
|
+
timeoutMax: 30_000,
|
|
872
|
+
timeoutMultiplier: 2,
|
|
873
|
+
}
|
package/src/object/map2.ts
CHANGED
|
@@ -13,6 +13,16 @@ export class Map2<K = any, V = any> extends Map<K, V> {
|
|
|
13
13
|
return new Map2(Object.entries(obj))
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Allows to set multiple key-value pairs at once.
|
|
18
|
+
*/
|
|
19
|
+
setMany(obj: Record<any, V>): this {
|
|
20
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
21
|
+
this.set(k as K, v)
|
|
22
|
+
}
|
|
23
|
+
return this
|
|
24
|
+
}
|
|
25
|
+
|
|
16
26
|
toObject(): Record<string, V> {
|
|
17
27
|
return Object.fromEntries(this)
|
|
18
28
|
}
|
package/src/object/set2.ts
CHANGED
|
@@ -6,6 +6,17 @@
|
|
|
6
6
|
* @experimental
|
|
7
7
|
*/
|
|
8
8
|
export class Set2<T = any> extends Set<T> {
|
|
9
|
+
/**
|
|
10
|
+
* Like .add(), but allows to add multiple items at once.
|
|
11
|
+
* Mutates the Set, but also returns it conveniently.
|
|
12
|
+
*/
|
|
13
|
+
addMany(items: Iterable<T>): this {
|
|
14
|
+
for (const item of items) {
|
|
15
|
+
this.add(item)
|
|
16
|
+
}
|
|
17
|
+
return this
|
|
18
|
+
}
|
|
19
|
+
|
|
9
20
|
toArray(): T[] {
|
|
10
21
|
return [...this]
|
|
11
22
|
}
|