@naturalcycles/js-lib 14.130.0 → 14.131.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.
@@ -10,5 +10,9 @@ import type { ErrorData } from './error.model';
10
10
  */
11
11
  export declare class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
12
12
  data: DATA_TYPE;
13
+ /**
14
+ * cause here is normalized to be instance of Error
15
+ */
16
+ cause?: Error;
13
17
  constructor(message: string, data?: DATA_TYPE, opt?: ErrorOptions);
14
18
  }
@@ -12,7 +12,7 @@ exports.AppError = void 0;
12
12
  */
13
13
  class AppError extends Error {
14
14
  constructor(message, data = {}, opt) {
15
- super(message, opt);
15
+ super(message);
16
16
  Object.defineProperty(this, 'name', {
17
17
  value: this.constructor.name,
18
18
  configurable: true,
@@ -23,6 +23,15 @@ class AppError extends Error {
23
23
  configurable: true,
24
24
  enumerable: false,
25
25
  });
26
+ if (opt?.cause) {
27
+ Object.defineProperty(this, 'cause', {
28
+ // I'd love to do _anyToError(opt.cause) here, but it causes circular dep ;(
29
+ value: opt.cause,
30
+ writable: true,
31
+ configurable: true,
32
+ enumerable: false,
33
+ });
34
+ }
26
35
  // todo: check if it's needed at all!
27
36
  // if (Error.captureStackTrace) {
28
37
  // Error.captureStackTrace(this, this.constructor)
@@ -93,7 +93,7 @@ export interface ErrorObject<DATA_TYPE extends ErrorData = ErrorData> {
93
93
  * It's non-optional, to save some null-checks.
94
94
  */
95
95
  data: DATA_TYPE;
96
- cause?: any;
96
+ cause?: ErrorObject;
97
97
  }
98
98
  /**
99
99
  * JSON HTTP response from the Backend that represents "Error".
@@ -17,7 +17,6 @@ export declare function _anyToError<ERROR_TYPE extends Error = Error>(o: any, er
17
17
  */
18
18
  export declare function _anyToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(o: any, errorData?: Partial<DATA_TYPE>): ErrorObject<DATA_TYPE>;
19
19
  export declare function _errorToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(e: AppError<DATA_TYPE> | Error): ErrorObject<DATA_TYPE>;
20
- export declare function _errorObjectToAppError<DATA_TYPE extends ErrorData>(o: ErrorObject<DATA_TYPE>): AppError<DATA_TYPE>;
21
20
  export declare function _errorObjectToError<DATA_TYPE extends ErrorData, ERROR_TYPE extends Error>(o: ErrorObject<DATA_TYPE>, errorClass?: Class<ERROR_TYPE>): ERROR_TYPE;
22
21
  export declare function _isHttpErrorResponse(o: any): o is HttpErrorResponse;
23
22
  export declare function _isHttpErrorObject(o: any): o is ErrorObject<HttpErrorData>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._errorDataAppend = exports._isErrorObject = exports._isHttpErrorObject = exports._isHttpErrorResponse = exports._errorObjectToError = exports._errorObjectToAppError = exports._errorToErrorObject = exports._anyToErrorObject = exports._anyToError = void 0;
3
+ exports._errorDataAppend = exports._isErrorObject = exports._isHttpErrorObject = exports._isHttpErrorResponse = exports._errorObjectToError = exports._errorToErrorObject = exports._anyToErrorObject = exports._anyToError = void 0;
4
4
  const __1 = require("..");
5
5
  /**
6
6
  * Useful to ensure that error in `catch (err) { ... }`
@@ -79,10 +79,6 @@ function _errorToErrorObject(e) {
79
79
  return obj;
80
80
  }
81
81
  exports._errorToErrorObject = _errorToErrorObject;
82
- function _errorObjectToAppError(o) {
83
- return _errorObjectToError(o, __1.AppError);
84
- }
85
- exports._errorObjectToAppError = _errorObjectToAppError;
86
82
  function _errorObjectToError(o, errorClass = Error) {
87
83
  if (o instanceof errorClass)
88
84
  return o;
@@ -173,6 +173,12 @@ class Fetcher {
173
173
  else if (mode === 'text') {
174
174
  res.body = res.fetchResponse.body ? await res.fetchResponse.text() : '';
175
175
  }
176
+ else if (mode === 'arrayBuffer') {
177
+ res.body = res.fetchResponse.body ? await res.fetchResponse.arrayBuffer() : {};
178
+ }
179
+ else if (mode === 'blob') {
180
+ res.body = res.fetchResponse.body ? await res.fetchResponse.blob() : {};
181
+ }
176
182
  clearTimeout(timeout);
177
183
  res.retryStatus.retryStopped = true;
178
184
  // res.err can happen on JSON.parse error
@@ -148,4 +148,4 @@ export interface FetcherErrorResponse<BODY = unknown> {
148
148
  retryStatus: FetcherRetryStatus;
149
149
  }
150
150
  export type FetcherResponse<BODY = unknown> = FetcherSuccessResponse<BODY> | FetcherErrorResponse<BODY>;
151
- export type FetcherMode = 'json' | 'text' | 'void';
151
+ export type FetcherMode = 'json' | 'text' | 'void' | 'arrayBuffer' | 'blob';
@@ -9,7 +9,7 @@
9
9
  */
10
10
  export class AppError extends Error {
11
11
  constructor(message, data = {}, opt) {
12
- super(message, opt);
12
+ super(message);
13
13
  Object.defineProperty(this, 'name', {
14
14
  value: this.constructor.name,
15
15
  configurable: true,
@@ -20,6 +20,15 @@ export class AppError extends Error {
20
20
  configurable: true,
21
21
  enumerable: false,
22
22
  });
23
+ if (opt === null || opt === void 0 ? void 0 : opt.cause) {
24
+ Object.defineProperty(this, 'cause', {
25
+ // I'd love to do _anyToError(opt.cause) here, but it causes circular dep ;(
26
+ value: opt.cause,
27
+ writable: true,
28
+ configurable: true,
29
+ enumerable: false,
30
+ });
31
+ }
23
32
  // todo: check if it's needed at all!
24
33
  // if (Error.captureStackTrace) {
25
34
  // Error.captureStackTrace(this, this.constructor)
@@ -1,4 +1,4 @@
1
- import { AppError, _jsonParseIfPossible, _stringifyAny } from '..';
1
+ import { _jsonParseIfPossible, _stringifyAny } from '..';
2
2
  /**
3
3
  * Useful to ensure that error in `catch (err) { ... }`
4
4
  * is indeed an Error (and not e.g `string` or `undefined`).
@@ -70,9 +70,6 @@ export function _errorToErrorObject(e) {
70
70
  }
71
71
  return obj;
72
72
  }
73
- export function _errorObjectToAppError(o) {
74
- return _errorObjectToError(o, AppError);
75
- }
76
73
  export function _errorObjectToError(o, errorClass = Error) {
77
74
  if (o instanceof errorClass)
78
75
  return o;
@@ -181,6 +181,12 @@ export class Fetcher {
181
181
  else if (mode === 'text') {
182
182
  res.body = res.fetchResponse.body ? await res.fetchResponse.text() : '';
183
183
  }
184
+ else if (mode === 'arrayBuffer') {
185
+ res.body = res.fetchResponse.body ? await res.fetchResponse.arrayBuffer() : {};
186
+ }
187
+ else if (mode === 'blob') {
188
+ res.body = res.fetchResponse.body ? await res.fetchResponse.blob() : {};
189
+ }
184
190
  clearTimeout(timeout);
185
191
  res.retryStatus.retryStopped = true;
186
192
  // res.err can happen on JSON.parse error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.130.0",
3
+ "version": "14.131.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -12,8 +12,13 @@ import type { ErrorData } from './error.model'
12
12
  export class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
13
13
  data!: DATA_TYPE
14
14
 
15
+ /**
16
+ * cause here is normalized to be instance of Error
17
+ */
18
+ override cause?: Error
19
+
15
20
  constructor(message: string, data = {} as DATA_TYPE, opt?: ErrorOptions) {
16
- super(message, opt)
21
+ super(message)
17
22
 
18
23
  Object.defineProperty(this, 'name', {
19
24
  value: this.constructor.name,
@@ -27,6 +32,16 @@ export class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
27
32
  enumerable: false,
28
33
  })
29
34
 
35
+ if (opt?.cause) {
36
+ Object.defineProperty(this, 'cause', {
37
+ // I'd love to do _anyToError(opt.cause) here, but it causes circular dep ;(
38
+ value: opt.cause,
39
+ writable: true,
40
+ configurable: true,
41
+ enumerable: false,
42
+ })
43
+ }
44
+
30
45
  // todo: check if it's needed at all!
31
46
  // if (Error.captureStackTrace) {
32
47
  // Error.captureStackTrace(this, this.constructor)
@@ -123,7 +123,7 @@ export interface ErrorObject<DATA_TYPE extends ErrorData = ErrorData> {
123
123
  */
124
124
  data: DATA_TYPE
125
125
 
126
- cause?: any
126
+ cause?: ErrorObject
127
127
  }
128
128
 
129
129
  /**
@@ -53,7 +53,7 @@ export function _anyToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(
53
53
  o = _jsonParseIfPossible(o)
54
54
 
55
55
  if (_isHttpErrorResponse(o)) {
56
- eo = o.error as any
56
+ eo = o.error as ErrorObject<DATA_TYPE>
57
57
  } else if (_isErrorObject(o)) {
58
58
  eo = o as ErrorObject<DATA_TYPE>
59
59
  } else {
@@ -92,12 +92,6 @@ export function _errorToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(
92
92
  return obj
93
93
  }
94
94
 
95
- export function _errorObjectToAppError<DATA_TYPE extends ErrorData>(
96
- o: ErrorObject<DATA_TYPE>,
97
- ): AppError<DATA_TYPE> {
98
- return _errorObjectToError(o, AppError)
99
- }
100
-
101
95
  export function _errorObjectToError<DATA_TYPE extends ErrorData, ERROR_TYPE extends Error>(
102
96
  o: ErrorObject<DATA_TYPE>,
103
97
  errorClass: Class<ERROR_TYPE> = Error as any,
@@ -178,4 +178,4 @@ export type FetcherResponse<BODY = unknown> =
178
178
  | FetcherSuccessResponse<BODY>
179
179
  | FetcherErrorResponse<BODY>
180
180
 
181
- export type FetcherMode = 'json' | 'text' | 'void'
181
+ export type FetcherMode = 'json' | 'text' | 'void' | 'arrayBuffer' | 'blob'
@@ -237,6 +237,10 @@ export class Fetcher {
237
237
  }
238
238
  } else if (mode === 'text') {
239
239
  res.body = res.fetchResponse.body ? await res.fetchResponse.text() : ''
240
+ } else if (mode === 'arrayBuffer') {
241
+ res.body = res.fetchResponse.body ? await res.fetchResponse.arrayBuffer() : {}
242
+ } else if (mode === 'blob') {
243
+ res.body = res.fetchResponse.body ? await res.fetchResponse.blob() : {}
240
244
  }
241
245
 
242
246
  clearTimeout(timeout)