@naturalcycles/js-lib 14.124.0 → 14.125.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.
@@ -11,12 +11,23 @@ const __1 = require("..");
11
11
  * Alternatively, if you're sure it's Error - you can use `_assertIsError(err)`.
12
12
  */
13
13
  function _anyToError(o, errorClass = Error, errorData, opt) {
14
- if (o instanceof errorClass)
15
- return o;
16
- // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
17
- const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt);
18
- Object.assign(errorObject.data, errorData);
19
- return _errorObjectToError(errorObject, errorClass);
14
+ let e;
15
+ if (o instanceof errorClass) {
16
+ e = o;
17
+ }
18
+ else {
19
+ // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
20
+ const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt);
21
+ e = _errorObjectToError(errorObject, errorClass);
22
+ }
23
+ if (errorData) {
24
+ ;
25
+ e.data = {
26
+ ...e.data,
27
+ ...errorData,
28
+ };
29
+ }
30
+ return e;
20
31
  }
21
32
  exports._anyToError = _anyToError;
22
33
  /**
@@ -89,6 +89,10 @@ export interface FetcherOptions {
89
89
  */
90
90
  body?: Blob | BufferSource | FormData | URLSearchParams | string;
91
91
  credentials?: RequestCredentials;
92
+ /**
93
+ * Default to true.
94
+ */
95
+ followRedirects?: boolean;
92
96
  headers?: Record<string, any>;
93
97
  mode?: FetcherMode;
94
98
  searchParams?: Record<string, any>;
@@ -166,6 +170,10 @@ export declare class Fetcher {
166
170
  deleteVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
167
171
  headVoid: (url: string, opt?: FetcherOptions) => Promise<void>;
168
172
  fetch<T = unknown>(url: string, opt?: FetcherOptions): Promise<T>;
173
+ /**
174
+ * Returns FetcherResponse.
175
+ * Never throws, returns `err` property in the response instead.
176
+ */
169
177
  rawFetch<T = unknown>(url: string, rawOpt?: FetcherOptions): Promise<FetcherResponse<T>>;
170
178
  private processRetry;
171
179
  /**
@@ -88,6 +88,10 @@ class Fetcher {
88
88
  }
89
89
  return res.body;
90
90
  }
91
+ /**
92
+ * Returns FetcherResponse.
93
+ * Never throws, returns `err` property in the response instead.
94
+ */
91
95
  async rawFetch(url, rawOpt = {}) {
92
96
  const { logger } = this.cfg;
93
97
  const req = this.normalizeOptions(url, rawOpt);
@@ -332,6 +336,7 @@ class Fetcher {
332
336
  ...this.cfg.init,
333
337
  method: opt.method || this.cfg.init.method,
334
338
  credentials: opt.credentials || this.cfg.init.credentials,
339
+ redirect: opt.followRedirects ?? this.cfg.followRedirects ?? true ? 'follow' : 'error',
335
340
  }, {
336
341
  headers: (0, object_util_1._mapKeys)(opt.headers || {}, k => k.toLowerCase()),
337
342
  }),
@@ -8,12 +8,20 @@ import { AppError, _jsonParseIfPossible, _stringifyAny } from '..';
8
8
  * Alternatively, if you're sure it's Error - you can use `_assertIsError(err)`.
9
9
  */
10
10
  export function _anyToError(o, errorClass = Error, errorData, opt) {
11
- if (o instanceof errorClass)
12
- return o;
13
- // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
14
- const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt);
15
- Object.assign(errorObject.data, errorData);
16
- return _errorObjectToError(errorObject, errorClass);
11
+ let e;
12
+ if (o instanceof errorClass) {
13
+ e = o;
14
+ }
15
+ else {
16
+ // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
17
+ const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt);
18
+ e = _errorObjectToError(errorObject, errorClass);
19
+ }
20
+ if (errorData) {
21
+ ;
22
+ e.data = Object.assign(Object.assign({}, e.data), errorData);
23
+ }
24
+ return e;
17
25
  }
18
26
  /**
19
27
  * Converts "anything" to ErrorObject.
@@ -77,6 +77,10 @@ export class Fetcher {
77
77
  }
78
78
  return res.body;
79
79
  }
80
+ /**
81
+ * Returns FetcherResponse.
82
+ * Never throws, returns `err` property in the response instead.
83
+ */
80
84
  async rawFetch(url, rawOpt = {}) {
81
85
  var _a, e_1, _b, _c, _d, e_2, _e, _f;
82
86
  var _g, _h, _j;
@@ -355,6 +359,7 @@ export class Fetcher {
355
359
  return norm;
356
360
  }
357
361
  normalizeOptions(url, opt) {
362
+ var _a, _b;
358
363
  const { baseUrl, timeoutSeconds, throwHttpErrors, retryPost, retry4xx, retry5xx, retry, mode } = this.cfg;
359
364
  const req = Object.assign(Object.assign({ mode,
360
365
  url,
@@ -362,7 +367,7 @@ export class Fetcher {
362
367
  throwHttpErrors,
363
368
  retryPost,
364
369
  retry4xx,
365
- retry5xx }, _omit(opt, ['method', 'headers', 'credentials'])), { retry: Object.assign(Object.assign({}, retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials }), {
370
+ retry5xx }, _omit(opt, ['method', 'headers', 'credentials'])), { retry: Object.assign(Object.assign({}, retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials, redirect: ((_b = (_a = opt.followRedirects) !== null && _a !== void 0 ? _a : this.cfg.followRedirects) !== null && _b !== void 0 ? _b : true) ? 'follow' : 'error' }), {
366
371
  headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),
367
372
  }) });
368
373
  // setup url
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.124.0",
3
+ "version": "14.125.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -22,13 +22,25 @@ export function _anyToError<ERROR_TYPE extends Error = Error>(
22
22
  errorData?: ErrorData,
23
23
  opt?: StringifyAnyOptions,
24
24
  ): ERROR_TYPE {
25
- if (o instanceof errorClass) return o
25
+ let e: ERROR_TYPE
26
+
27
+ if (o instanceof errorClass) {
28
+ e = o
29
+ } else {
30
+ // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
26
31
 
27
- // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
32
+ const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt)
33
+ e = _errorObjectToError(errorObject, errorClass) as any
34
+ }
35
+
36
+ if (errorData) {
37
+ ;(e as any).data = {
38
+ ...(e as any).data,
39
+ ...errorData,
40
+ }
41
+ }
28
42
 
29
- const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt)
30
- Object.assign(errorObject.data, errorData)
31
- return _errorObjectToError(errorObject, errorClass)
43
+ return e
32
44
  }
33
45
 
34
46
  /**
@@ -117,6 +117,10 @@ export interface FetcherOptions {
117
117
  body?: Blob | BufferSource | FormData | URLSearchParams | string
118
118
 
119
119
  credentials?: RequestCredentials
120
+ /**
121
+ * Default to true.
122
+ */
123
+ followRedirects?: boolean
120
124
 
121
125
  // Removing RequestInit from options to simplify FetcherOptions interface.
122
126
  // Will instead only add hand-picked useful options, such as `credentials`.
@@ -287,6 +291,10 @@ export class Fetcher {
287
291
  return res.body!
288
292
  }
289
293
 
294
+ /**
295
+ * Returns FetcherResponse.
296
+ * Never throws, returns `err` property in the response instead.
297
+ */
290
298
  async rawFetch<T = unknown>(
291
299
  url: string,
292
300
  rawOpt: FetcherOptions = {},
@@ -575,6 +583,7 @@ export class Fetcher {
575
583
  ...this.cfg.init,
576
584
  method: opt.method || this.cfg.init.method,
577
585
  credentials: opt.credentials || this.cfg.init.credentials,
586
+ redirect: opt.followRedirects ?? this.cfg.followRedirects ?? true ? 'follow' : 'error',
578
587
  },
579
588
  {
580
589
  headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),