@vpmedia/simplify 1.24.0 → 1.26.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.
- package/README.md +1 -1
- package/package.json +7 -7
- package/src/index.js +1 -0
- package/src/util/fetchRetry.js +9 -1
- package/src/util/getErrorDetails.js +17 -0
- package/src/util/getErrorDetails.test.js +9 -0
- package/src/util/serverDataToState.test.js +24 -19
- package/types/index.d.ts +1 -0
- package/types/util/fetchRetry.d.ts +1 -0
- package/types/util/fetchRetry.d.ts.map +1 -1
- package/types/util/getErrorDetails.d.ts +2 -0
- package/types/util/getErrorDetails.d.ts.map +1 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @vpmedia/simplify
|
|
2
2
|
|
|
3
|
-
[](https://badge.fury.io/js/@vpmedia%2Fsimplify)
|
|
4
4
|
[](https://github.com/vpmedia/simplify/actions/workflows/ci.yml)
|
|
5
5
|
|
|
6
6
|
@vpmedia/simplify TBD
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vpmedia/simplify",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.26.0",
|
|
4
4
|
"description": "@vpmedia/simplify",
|
|
5
5
|
"author": "Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
"types": "./types/index.d.ts",
|
|
20
20
|
"type": "module",
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@babel/preset-env": "^7.
|
|
23
|
-
"@eslint/js": "^9.
|
|
22
|
+
"@babel/preset-env": "^7.27.2",
|
|
23
|
+
"@eslint/js": "^9.25.1",
|
|
24
24
|
"@jest/globals": "^29.7.0",
|
|
25
25
|
"@types/jest": "^29.5.14",
|
|
26
|
-
"eslint": "^9.
|
|
27
|
-
"eslint-plugin-jsdoc": "^50.6.
|
|
28
|
-
"eslint-plugin-unicorn": "^
|
|
29
|
-
"globals": "^16.
|
|
26
|
+
"eslint": "^9.26.0",
|
|
27
|
+
"eslint-plugin-jsdoc": "^50.6.14",
|
|
28
|
+
"eslint-plugin-unicorn": "^59.0.1",
|
|
29
|
+
"globals": "^16.1.0",
|
|
30
30
|
"jest": "^29.7.0",
|
|
31
31
|
"jest-environment-jsdom": "^29.7.0",
|
|
32
32
|
"prettier": "^3.5.3",
|
package/src/index.js
CHANGED
|
@@ -12,6 +12,7 @@ export { deg2rad } from './util/deg2rad.js';
|
|
|
12
12
|
export { delayPromise } from './util/delayPromise.js';
|
|
13
13
|
export { FetchError, fetchRetry, HTTP_0_ANY } from './util/fetchRetry.js';
|
|
14
14
|
export { fixFloatPrecision } from './util/fixFloatPrecision.js';
|
|
15
|
+
export { getErrorDetails } from './util/getErrorDetails.js';
|
|
15
16
|
export { getObjValueByPath } from './util/getObjValueByPath.js';
|
|
16
17
|
export { getRandomInt } from './util/getRandomInt.js';
|
|
17
18
|
export { getURLParam } from './util/getURLParam.js';
|
package/src/util/fetchRetry.js
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from '../const/http_status.js';
|
|
7
7
|
import { Logger } from '../logging/Logger.js';
|
|
8
8
|
import { delayPromise } from './delayPromise.js';
|
|
9
|
+
import { getErrorDetails } from './getErrorDetails.js';
|
|
9
10
|
|
|
10
11
|
const logger = new Logger('fetch');
|
|
11
12
|
|
|
@@ -32,11 +33,12 @@ export class FetchError extends Error {
|
|
|
32
33
|
* Fetch with retry.
|
|
33
34
|
* @param {string | URL | Request} resource - Fetch URL.
|
|
34
35
|
* @param {RequestInit} [fetchOptions] - Fetch options.
|
|
35
|
-
* @param {{delay?: number, numTries?: number, statusExcludes?: number[]}} [retryOptions] - Retry options.
|
|
36
|
+
* @param {{delay?: number, numTries?: number, statusExcludes?: number[], timeout?: number}} [retryOptions] - Retry options.
|
|
36
37
|
* @returns {Promise<Response>} Fetch result.
|
|
37
38
|
*/
|
|
38
39
|
export const fetchRetry = async (resource, fetchOptions, retryOptions) => {
|
|
39
40
|
retryOptions = retryOptions ?? {};
|
|
41
|
+
retryOptions.timeout = Math.max(retryOptions.timeout ?? 10000, 1000);
|
|
40
42
|
retryOptions.delay = Math.max(retryOptions.delay ?? 500, 1);
|
|
41
43
|
retryOptions.numTries = Math.max(retryOptions.numTries ?? 3, 1);
|
|
42
44
|
retryOptions.statusExcludes = retryOptions.statusExcludes ?? [
|
|
@@ -47,6 +49,9 @@ export const fetchRetry = async (resource, fetchOptions, retryOptions) => {
|
|
|
47
49
|
];
|
|
48
50
|
while (retryOptions.numTries > 0) {
|
|
49
51
|
logger.info('request', { resource, fetchOptions, retryOptions });
|
|
52
|
+
const controller = new AbortController();
|
|
53
|
+
const timeoutId = setTimeout(() => controller.abort(), retryOptions.timeout);
|
|
54
|
+
fetchOptions.signal = controller.signal;
|
|
50
55
|
try {
|
|
51
56
|
const response = await fetch(resource, fetchOptions);
|
|
52
57
|
if (!response.ok) {
|
|
@@ -63,6 +68,7 @@ export const fetchRetry = async (resource, fetchOptions, retryOptions) => {
|
|
|
63
68
|
} catch (error) {
|
|
64
69
|
const typedError = error instanceof Error ? error : new Error(String(error));
|
|
65
70
|
logger.error('error', typedError);
|
|
71
|
+
logger.info('errorDetails', getErrorDetails(typedError));
|
|
66
72
|
retryOptions.numTries -= 1;
|
|
67
73
|
if (
|
|
68
74
|
retryOptions.numTries === 0 ||
|
|
@@ -74,6 +80,8 @@ export const fetchRetry = async (resource, fetchOptions, retryOptions) => {
|
|
|
74
80
|
}
|
|
75
81
|
await delayPromise(retryOptions.delay);
|
|
76
82
|
retryOptions.delay *= 2;
|
|
83
|
+
} finally {
|
|
84
|
+
clearTimeout(timeoutId);
|
|
77
85
|
}
|
|
78
86
|
}
|
|
79
87
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieves detailed information from an error object.
|
|
3
|
+
* @param {Error} error - The error to extract details from.
|
|
4
|
+
* @param {string[]} [excludes] - An array of property names to exclude from the result.
|
|
5
|
+
* @returns {object} - An object containing the error details.
|
|
6
|
+
*/
|
|
7
|
+
export const getErrorDetails = (error, excludes = ['stack']) => {
|
|
8
|
+
const errorDetails = {
|
|
9
|
+
type: error.constructor?.name || typeof error, // e.g., "TypeError"
|
|
10
|
+
};
|
|
11
|
+
for (const key of Object.getOwnPropertyNames(error)) {
|
|
12
|
+
if (!excludes.includes(key)) {
|
|
13
|
+
errorDetails[key] = error[key];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return errorDetails;
|
|
17
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getErrorDetails } from './getErrorDetails.js';
|
|
2
|
+
|
|
3
|
+
test('Tests getErrorDetails', () => {
|
|
4
|
+
const error = new Error('Test error', { cause: 'Test cause' });
|
|
5
|
+
const errorDetails = getErrorDetails(error);
|
|
6
|
+
expect(errorDetails.type).toBe('Error');
|
|
7
|
+
expect(errorDetails.message).toBe('Test error');
|
|
8
|
+
expect(errorDetails.cause).toBe('Test cause');
|
|
9
|
+
});
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { serverDataToState } from './serverDataToState.js';
|
|
2
2
|
|
|
3
3
|
test('serverDataToState() recursive', () => {
|
|
4
|
-
const state = serverDataToState(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
const state = serverDataToState(
|
|
5
|
+
{
|
|
6
|
+
my_array: [{ key_a: 'value1' }],
|
|
7
|
+
my_data: { key_a: 'value1' },
|
|
8
|
+
my_list: [1, 2, 3],
|
|
9
|
+
my_null: null,
|
|
10
|
+
my_number: 1000,
|
|
11
|
+
my_string: 'a',
|
|
12
|
+
my_var: 'test',
|
|
13
|
+
},
|
|
14
|
+
true
|
|
15
|
+
);
|
|
13
16
|
expect(state.myArray[0].keyA).toBe('value1');
|
|
14
17
|
expect(state.myData.keyA).toBe('value1');
|
|
15
18
|
expect(state.myList[0]).toBe(1);
|
|
@@ -20,15 +23,18 @@ test('serverDataToState() recursive', () => {
|
|
|
20
23
|
});
|
|
21
24
|
|
|
22
25
|
test('serverDataToState() non-recursive', () => {
|
|
23
|
-
const state = serverDataToState(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
const state = serverDataToState(
|
|
27
|
+
{
|
|
28
|
+
my_array: [{ key_a: 'value1' }],
|
|
29
|
+
my_data: { key_a: 'value1' },
|
|
30
|
+
my_list: [1, 2, 3],
|
|
31
|
+
my_null: null,
|
|
32
|
+
my_number: 1000,
|
|
33
|
+
my_string: 'a',
|
|
34
|
+
my_var: 'test',
|
|
35
|
+
},
|
|
36
|
+
false
|
|
37
|
+
);
|
|
32
38
|
expect(state.myArray[0].key_a).toBe('value1');
|
|
33
39
|
expect(state.myData.key_a).toBe('value1');
|
|
34
40
|
expect(state.myList[0]).toBe(1);
|
|
@@ -37,4 +43,3 @@ test('serverDataToState() non-recursive', () => {
|
|
|
37
43
|
expect(state.myString).toBe('a');
|
|
38
44
|
expect(state.myVar).toBe('test');
|
|
39
45
|
});
|
|
40
|
-
|
package/types/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { deepMerge } from "./util/deepMerge.js";
|
|
|
10
10
|
export { deg2rad } from "./util/deg2rad.js";
|
|
11
11
|
export { delayPromise } from "./util/delayPromise.js";
|
|
12
12
|
export { fixFloatPrecision } from "./util/fixFloatPrecision.js";
|
|
13
|
+
export { getErrorDetails } from "./util/getErrorDetails.js";
|
|
13
14
|
export { getObjValueByPath } from "./util/getObjValueByPath.js";
|
|
14
15
|
export { getRandomInt } from "./util/getRandomInt.js";
|
|
15
16
|
export { getURLParam } from "./util/getURLParam.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchRetry.d.ts","sourceRoot":"","sources":["../../src/util/fetchRetry.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetchRetry.d.ts","sourceRoot":"","sources":["../../src/util/fetchRetry.js"],"names":[],"mappings":"AAYA,yBAA0B,CAAC,CAAC;AAE5B;IACE;;;;;;OAMG;IACH,qBALW,MAAM,YACN,MAAM,GAAG,GAAG,GAAG,OAAO,gBACtB,WAAW,YACX,QAAQ,EAQlB;IAHC,iCAAwB;IACxB,0BAAgC;IAChC,mBAAwB;CAE3B;AASM,qCALI,MAAM,GAAG,GAAG,GAAG,OAAO,iBACtB,WAAW,iBACX;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAC,GAC9E,OAAO,CAAC,QAAQ,CAAC,CAkD7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getErrorDetails.d.ts","sourceRoot":"","sources":["../../src/util/getErrorDetails.js"],"names":[],"mappings":"AAMO,uCAJI,KAAK,aACL,MAAM,EAAE,GACN,MAAM,CAYlB"}
|