@naturalcycles/js-lib 15.51.0 → 15.53.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.
@@ -95,6 +95,9 @@ export interface HttpRequestErrorData extends ErrorData {
95
95
  * Non-enumerable.
96
96
  */
97
97
  response?: Response;
98
+ /**
99
+ * Full request url, inclusing baseUrl, inputUrl, searchParams, etc.
100
+ */
98
101
  requestUrl: string;
99
102
  requestBaseUrl?: string;
100
103
  requestMethod: HttpMethod;
@@ -109,6 +112,10 @@ export interface HttpRequestErrorData extends ErrorData {
109
112
  * Allows to construct errorGroup/fingerprint.
110
113
  */
111
114
  requestName?: string;
115
+ /**
116
+ * FetcherCfg.name if it was provided, for the purpose of constructing an error fingerprint.
117
+ */
118
+ fetcherName?: string;
112
119
  /**
113
120
  * Can be set to 0 if request "failed to start" or "failed to reach the server".
114
121
  */
@@ -113,6 +113,7 @@ export declare class Fetcher {
113
113
  */
114
114
  private getShortUrl;
115
115
  private normalizeCfg;
116
+ private getFetcherName;
116
117
  private normalizeOptions;
117
118
  }
118
119
  export declare function getFetcher(cfg?: FetcherCfg & FetcherOptions): Fetcher;
@@ -160,6 +160,8 @@ export class Fetcher {
160
160
  requestBaseUrl: this.cfg.baseUrl,
161
161
  requestMethod: res.req.init.method,
162
162
  requestSignature: res.signature,
163
+ requestName: res.req.requestName,
164
+ fetcherName: this.cfg.name,
163
165
  requestDuration: Date.now() - res.req.started,
164
166
  });
165
167
  }
@@ -441,6 +443,7 @@ export class Fetcher {
441
443
  requestMethod: res.req.init.method,
442
444
  requestSignature: res.signature,
443
445
  requestName: res.req.requestName,
446
+ fetcherName: this.cfg.name,
444
447
  requestDuration: Date.now() - res.req.started,
445
448
  }), {
446
449
  cause,
@@ -588,6 +591,7 @@ export class Fetcher {
588
591
  }
589
592
  const norm = _merge({
590
593
  baseUrl: '',
594
+ name: this.getFetcherName(cfg),
591
595
  inputUrl: '',
592
596
  responseType: 'json',
593
597
  searchParams: {},
@@ -618,10 +622,21 @@ export class Fetcher {
618
622
  hooks: {},
619
623
  throwHttpErrors: true,
620
624
  errorData: {},
621
- }, _omit(cfg, ['method', 'credentials', 'headers', 'redirect', 'logger']));
625
+ }, _omit(cfg, ['method', 'credentials', 'headers', 'redirect', 'logger', 'name']));
622
626
  norm.init.headers = _mapKeys(norm.init.headers, k => k.toLowerCase());
623
627
  return norm;
624
628
  }
629
+ getFetcherName(cfg) {
630
+ let { name } = cfg;
631
+ if (!name && cfg.baseUrl) {
632
+ // derive FetcherName from baseUrl
633
+ const url = URL.parse(cfg.baseUrl);
634
+ if (url) {
635
+ name = url.hostname;
636
+ }
637
+ }
638
+ return name;
639
+ }
625
640
  normalizeOptions(opt) {
626
641
  const req = {
627
642
  ..._pick(this.cfg, [
@@ -5,9 +5,10 @@ import type { ErrorData } from '../error/error.model.js';
5
5
  import type { CommonLogger } from '../log/commonLogger.js';
6
6
  import type { AnyObject, NumberOfMilliseconds, Promisable, Reviver, UnixTimestampMillis } from '../types.js';
7
7
  import type { HttpMethod, HttpStatusFamily } from './http.model.js';
8
- export interface FetcherNormalizedCfg extends Required<Omit<FetcherCfg, 'dispatcher'>>, Omit<FetcherRequest, 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody' | 'debug' | 'redirect' | 'credentials' | 'throwHttpErrors' | 'errorData'> {
8
+ export interface FetcherNormalizedCfg extends Required<Omit<FetcherCfg, 'dispatcher' | 'name'>>, Omit<FetcherRequest, 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody' | 'debug' | 'redirect' | 'credentials' | 'throwHttpErrors' | 'errorData'> {
9
9
  logger: CommonLogger;
10
10
  searchParams: Record<string, any>;
11
+ name?: string;
11
12
  }
12
13
  export type FetcherBeforeRequestHook = (req: FetcherRequest) => Promisable<void>;
13
14
  export type FetcherAfterResponseHook = <BODY = unknown>(res: FetcherResponse<BODY>) => Promisable<void>;
@@ -26,6 +27,12 @@ export interface FetcherCfg {
26
27
  * Should **not** contain trailing slash.
27
28
  */
28
29
  baseUrl?: string;
30
+ /**
31
+ * "Name" of the fetcher.
32
+ * Accessible inside HttpRequestError, to be able to construct a good fingerprint.
33
+ * If name is not provided - baseUrl is used to identify a Fetcher.
34
+ */
35
+ name?: string;
29
36
  /**
30
37
  * Default rule is that you **are allowed** to mutate req, res, res.retryStatus
31
38
  * properties of hook function arguments.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.51.0",
4
+ "version": "15.53.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "undici": "^7",
@@ -13,7 +13,7 @@
13
13
  "@types/semver": "^7",
14
14
  "crypto-js": "^4",
15
15
  "dayjs": "^1",
16
- "@naturalcycles/dev-lib": "20.12.12"
16
+ "@naturalcycles/dev-lib": "18.4.2"
17
17
  },
18
18
  "exports": {
19
19
  ".": "./dist/index.js",
@@ -110,7 +110,9 @@ export interface HttpRequestErrorData extends ErrorData {
110
110
  * Non-enumerable.
111
111
  */
112
112
  response?: Response
113
-
113
+ /**
114
+ * Full request url, inclusing baseUrl, inputUrl, searchParams, etc.
115
+ */
114
116
  requestUrl: string
115
117
  requestBaseUrl?: string
116
118
  requestMethod: HttpMethod
@@ -125,6 +127,10 @@ export interface HttpRequestErrorData extends ErrorData {
125
127
  * Allows to construct errorGroup/fingerprint.
126
128
  */
127
129
  requestName?: string
130
+ /**
131
+ * FetcherCfg.name if it was provided, for the purpose of constructing an error fingerprint.
132
+ */
133
+ fetcherName?: string
128
134
  /**
129
135
  * Can be set to 0 if request "failed to start" or "failed to reach the server".
130
136
  */
@@ -15,7 +15,7 @@ import type { HttpMethod, HttpStatusFamily } from './http.model.js'
15
15
 
16
16
  export interface FetcherNormalizedCfg
17
17
  extends
18
- Required<Omit<FetcherCfg, 'dispatcher'>>,
18
+ Required<Omit<FetcherCfg, 'dispatcher' | 'name'>>,
19
19
  Omit<
20
20
  FetcherRequest,
21
21
  | 'started'
@@ -32,6 +32,7 @@ export interface FetcherNormalizedCfg
32
32
  > {
33
33
  logger: CommonLogger
34
34
  searchParams: Record<string, any>
35
+ name?: string
35
36
  }
36
37
 
37
38
  export type FetcherBeforeRequestHook = (req: FetcherRequest) => Promisable<void>
@@ -57,6 +58,13 @@ export interface FetcherCfg {
57
58
  */
58
59
  baseUrl?: string
59
60
 
61
+ /**
62
+ * "Name" of the fetcher.
63
+ * Accessible inside HttpRequestError, to be able to construct a good fingerprint.
64
+ * If name is not provided - baseUrl is used to identify a Fetcher.
65
+ */
66
+ name?: string
67
+
60
68
  /**
61
69
  * Default rule is that you **are allowed** to mutate req, res, res.retryStatus
62
70
  * properties of hook function arguments.
@@ -221,6 +221,8 @@ export class Fetcher {
221
221
  requestBaseUrl: this.cfg.baseUrl,
222
222
  requestMethod: res.req.init.method,
223
223
  requestSignature: res.signature,
224
+ requestName: res.req.requestName,
225
+ fetcherName: this.cfg.name,
224
226
  requestDuration: Date.now() - res.req.started,
225
227
  })
226
228
  }
@@ -552,6 +554,7 @@ export class Fetcher {
552
554
  requestMethod: res.req.init.method,
553
555
  requestSignature: res.signature,
554
556
  requestName: res.req.requestName,
557
+ fetcherName: this.cfg.name,
555
558
  requestDuration: Date.now() - res.req.started,
556
559
  }),
557
560
  {
@@ -723,6 +726,7 @@ export class Fetcher {
723
726
  const norm: FetcherNormalizedCfg = _merge(
724
727
  {
725
728
  baseUrl: '',
729
+ name: this.getFetcherName(cfg),
726
730
  inputUrl: '',
727
731
  responseType: 'json',
728
732
  searchParams: {},
@@ -754,7 +758,7 @@ export class Fetcher {
754
758
  throwHttpErrors: true,
755
759
  errorData: {},
756
760
  },
757
- _omit(cfg, ['method', 'credentials', 'headers', 'redirect', 'logger']),
761
+ _omit(cfg, ['method', 'credentials', 'headers', 'redirect', 'logger', 'name']),
758
762
  )
759
763
 
760
764
  norm.init.headers = _mapKeys(norm.init.headers, k => k.toLowerCase())
@@ -762,6 +766,18 @@ export class Fetcher {
762
766
  return norm
763
767
  }
764
768
 
769
+ private getFetcherName(cfg: FetcherCfg): string | undefined {
770
+ let { name } = cfg
771
+ if (!name && cfg.baseUrl) {
772
+ // derive FetcherName from baseUrl
773
+ const url = URL.parse(cfg.baseUrl)
774
+ if (url) {
775
+ name = url.hostname
776
+ }
777
+ }
778
+ return name
779
+ }
780
+
765
781
  private normalizeOptions(opt: FetcherOptions): FetcherRequest {
766
782
  const req: FetcherRequest = {
767
783
  ..._pick(this.cfg, [