@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.
@@ -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 = 2;
20
- static readonly userAgent: string | undefined;
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.
@@ -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 = 2;
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: { ...defRetryOptions },
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: { ...this.cfg.init.headers }, // this avoids mutation
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
+ };
@@ -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
  }
@@ -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
  }
@@ -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
  }
@@ -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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.39.0",
4
+ "version": "15.40.1",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "undici": "^7",
@@ -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 = 2
87
- static readonly userAgent = isServerSide() ? `fetcher/${this.VERSION}` : undefined
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: { ...defRetryOptions },
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: { ...this.cfg.init.headers }, // this avoids mutation
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
+ }
@@ -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
  }
@@ -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
  }