@naturalcycles/js-lib 14.125.0 → 14.126.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.
@@ -43,6 +43,16 @@ export interface FetcherCfg {
43
43
  logRequestBody?: boolean;
44
44
  logResponse?: boolean;
45
45
  logResponseBody?: boolean;
46
+ /**
47
+ * Default to true.
48
+ * Set to false to exclude `prefixUrl` from logs (both success and error)
49
+ */
50
+ logWithPrefixUrl?: boolean;
51
+ /**
52
+ * Default to true.
53
+ * Set to false to strip searchParams from url when logging (both success and error)
54
+ */
55
+ logWithSearchParams?: boolean;
46
56
  /**
47
57
  * Defaults to `console`.
48
58
  */
@@ -116,7 +116,8 @@ class Fetcher {
116
116
  retryTimeout: req.retry.timeout,
117
117
  },
118
118
  };
119
- const shortUrl = this.getShortUrl(req.url);
119
+ const fullUrl = new URL(req.url);
120
+ const shortUrl = this.getShortUrl(fullUrl);
120
121
  const signature = [method.toUpperCase(), shortUrl].join(' ');
121
122
  /* eslint-disable no-await-in-loop */
122
123
  while (!res.retryStatus.retryStopped) {
@@ -280,9 +281,18 @@ class Fetcher {
280
281
  */
281
282
  getShortUrl(url) {
282
283
  const { baseUrl } = this.cfg;
283
- if (!baseUrl)
284
- return url;
285
- return url.split('?')[0].slice(baseUrl.length);
284
+ if (url.password) {
285
+ url = new URL(url.toString()); // prevent original url mutation
286
+ url.password = '[redacted]';
287
+ }
288
+ let shortUrl = url.toString();
289
+ if (!this.cfg.logWithSearchParams) {
290
+ shortUrl = shortUrl.split('?')[0];
291
+ }
292
+ if (!this.cfg.logWithPrefixUrl && baseUrl && shortUrl.startsWith(baseUrl)) {
293
+ shortUrl = shortUrl.slice(baseUrl.length);
294
+ }
295
+ return shortUrl;
286
296
  }
287
297
  normalizeCfg(cfg) {
288
298
  if (cfg.baseUrl?.endsWith('/')) {
@@ -306,6 +316,8 @@ class Fetcher {
306
316
  logRequestBody: debug,
307
317
  logResponse: debug,
308
318
  logResponseBody: debug,
319
+ logWithPrefixUrl: true,
320
+ logWithSearchParams: true,
309
321
  retry: { ...defRetryOptions },
310
322
  init: {
311
323
  method: cfg.method || 'get',
@@ -124,7 +124,8 @@ export class Fetcher {
124
124
  retryTimeout: req.retry.timeout,
125
125
  },
126
126
  };
127
- const shortUrl = this.getShortUrl(req.url);
127
+ const fullUrl = new URL(req.url);
128
+ const shortUrl = this.getShortUrl(fullUrl);
128
129
  const signature = [method.toUpperCase(), shortUrl].join(' ');
129
130
  /* eslint-disable no-await-in-loop */
130
131
  while (!res.retryStatus.retryStopped) {
@@ -320,9 +321,18 @@ export class Fetcher {
320
321
  */
321
322
  getShortUrl(url) {
322
323
  const { baseUrl } = this.cfg;
323
- if (!baseUrl)
324
- return url;
325
- return url.split('?')[0].slice(baseUrl.length);
324
+ if (url.password) {
325
+ url = new URL(url.toString()); // prevent original url mutation
326
+ url.password = '[redacted]';
327
+ }
328
+ let shortUrl = url.toString();
329
+ if (!this.cfg.logWithSearchParams) {
330
+ shortUrl = shortUrl.split('?')[0];
331
+ }
332
+ if (!this.cfg.logWithPrefixUrl && baseUrl && shortUrl.startsWith(baseUrl)) {
333
+ shortUrl = shortUrl.slice(baseUrl.length);
334
+ }
335
+ return shortUrl;
326
336
  }
327
337
  normalizeCfg(cfg) {
328
338
  var _a;
@@ -347,6 +357,8 @@ export class Fetcher {
347
357
  logRequestBody: debug,
348
358
  logResponse: debug,
349
359
  logResponseBody: debug,
360
+ logWithPrefixUrl: true,
361
+ logWithSearchParams: true,
350
362
  retry: Object.assign({}, defRetryOptions),
351
363
  init: {
352
364
  method: cfg.method || 'get',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.125.0",
3
+ "version": "14.126.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -65,6 +65,18 @@ export interface FetcherCfg {
65
65
  logResponse?: boolean
66
66
  logResponseBody?: boolean
67
67
 
68
+ /**
69
+ * Default to true.
70
+ * Set to false to exclude `prefixUrl` from logs (both success and error)
71
+ */
72
+ logWithPrefixUrl?: boolean
73
+
74
+ /**
75
+ * Default to true.
76
+ * Set to false to strip searchParams from url when logging (both success and error)
77
+ */
78
+ logWithSearchParams?: boolean
79
+
68
80
  /**
69
81
  * Defaults to `console`.
70
82
  */
@@ -331,7 +343,8 @@ export class Fetcher {
331
343
  },
332
344
  }
333
345
 
334
- const shortUrl = this.getShortUrl(req.url)
346
+ const fullUrl = new URL(req.url)
347
+ const shortUrl = this.getShortUrl(fullUrl)
335
348
  const signature = [method.toUpperCase(), shortUrl].join(' ')
336
349
 
337
350
  /* eslint-disable no-await-in-loop */
@@ -514,11 +527,25 @@ export class Fetcher {
514
527
  /**
515
528
  * Returns url without baseUrl and before ?queryString
516
529
  */
517
- private getShortUrl(url: string): string {
530
+ private getShortUrl(url: URL): string {
518
531
  const { baseUrl } = this.cfg
519
- if (!baseUrl) return url
520
532
 
521
- return url.split('?')[0]!.slice(baseUrl.length)
533
+ if (url.password) {
534
+ url = new URL(url.toString()) // prevent original url mutation
535
+ url.password = '[redacted]'
536
+ }
537
+
538
+ let shortUrl = url.toString()
539
+
540
+ if (!this.cfg.logWithSearchParams) {
541
+ shortUrl = shortUrl.split('?')[0]!
542
+ }
543
+
544
+ if (!this.cfg.logWithPrefixUrl && baseUrl && shortUrl.startsWith(baseUrl)) {
545
+ shortUrl = shortUrl.slice(baseUrl.length)
546
+ }
547
+
548
+ return shortUrl
522
549
  }
523
550
 
524
551
  private normalizeCfg(cfg: FetcherCfg & FetcherOptions): FetcherNormalizedCfg {
@@ -545,6 +572,8 @@ export class Fetcher {
545
572
  logRequestBody: debug,
546
573
  logResponse: debug,
547
574
  logResponseBody: debug,
575
+ logWithPrefixUrl: true,
576
+ logWithSearchParams: true,
548
577
  retry: { ...defRetryOptions },
549
578
  init: {
550
579
  method: cfg.method || 'get',