@vpmedia/simplify 1.3.0 → 1.4.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/CHANGES.md CHANGED
@@ -3,6 +3,10 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project adheres to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## 1.4.0
7
+
8
+ - Added fetch retry helper
9
+
6
10
  ## 1.3.0
7
11
 
8
12
  - Changed logger level initialization strategy
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @vpmedia/simplify
2
2
 
3
- [![npm version](https://badge.fury.io/js/@vpmedia%2Fsimplify.svg?v=1.3.0)](https://badge.fury.io/js/@vpmedia%2Fsimplify)
3
+ [![npm version](https://badge.fury.io/js/@vpmedia%2Fsimplify.svg?v=1.4.0)](https://badge.fury.io/js/@vpmedia%2Fsimplify)
4
4
  [![Node.js CI](https://github.com/vpmedia/simplify/actions/workflows/ci.yml/badge.svg)](https://github.com/vpmedia/simplify/actions/workflows/ci.yml)
5
5
 
6
6
  @vpmedia/simplify TBD
package/eslint.config.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import js from '@eslint/js';
2
- import importPlugin from 'eslint-plugin-import';
2
+ // import importPlugin from 'eslint-plugin-import';
3
3
  import jsdocPlugin from 'eslint-plugin-jsdoc';
4
4
  import unicornPlugin from 'eslint-plugin-unicorn';
5
5
  import globals from 'globals';
6
6
 
7
- /** @type { import('eslint').Linter.FlatConfig[] } */
7
+ /** @type { import('eslint').Linter.Config[] } */
8
8
  export default [
9
9
  {
10
10
  ignores: [
@@ -37,7 +37,7 @@ export default [
37
37
  },
38
38
  files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'],
39
39
  plugins: {
40
- import: importPlugin,
40
+ // import: importPlugin,
41
41
  jsdoc: jsdocPlugin,
42
42
  unicorn: unicornPlugin,
43
43
  },
@@ -52,15 +52,15 @@ export default [
52
52
  rules: {
53
53
  ...js.configs.recommended.rules,
54
54
  ...jsdocPlugin.configs['flat/recommended'].rules,
55
- ...importPlugin.configs.recommended.rules,
55
+ // ...importPlugin.configs.recommended.rules,
56
56
  ...unicornPlugin.configs['flat/recommended'].rules,
57
57
  'unicorn/filename-case': 'off',
58
58
  'unicorn/no-null': 'off',
59
59
  'unicorn/prevent-abbreviations': 'off',
60
- 'no-unused-vars': 'warn',
60
+ 'no-unused-vars': 'off',
61
61
  'prefer-arrow-callback': 'error',
62
62
  'prefer-template': 'error',
63
- 'import/extensions': ['warn', 'always'],
63
+ // 'import/extensions': ['warn', 'always'],
64
64
  'jsdoc/require-jsdoc': [
65
65
  'error',
66
66
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vpmedia/simplify",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "@vpmedia/simplify",
5
5
  "author": "Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)",
6
6
  "license": "MIT",
@@ -19,21 +19,19 @@
19
19
  "types": "./types/index.d.ts",
20
20
  "type": "module",
21
21
  "devDependencies": {
22
- "@babel/preset-env": "^7.24.4",
23
- "@eslint/js": "^8.57.0",
22
+ "@babel/preset-env": "^7.25.7",
23
+ "@eslint/js": "^9.10.0",
24
24
  "@jest/globals": "^29.7.0",
25
- "@types/jest": "^29.5.12",
26
- "eslint": "^8.57.0",
27
- "eslint-plugin-import": "^2.29.1",
28
- "eslint-plugin-jest": "^27.9.0",
29
- "eslint-plugin-jsdoc": "^48.2.3",
30
- "eslint-plugin-unicorn": "^52.0.0",
31
- "globals": "^15.0.0",
25
+ "@types/jest": "^29.5.13",
26
+ "eslint": "^9.11.1",
27
+ "eslint-plugin-jsdoc": "^50.3.1",
28
+ "eslint-plugin-unicorn": "^55.0.0",
29
+ "globals": "^15.10.0",
32
30
  "jest": "^29.7.0",
33
31
  "jest-environment-jsdom": "^29.7.0",
34
- "lefthook": "^1.6.10",
35
- "prettier": "^3.2.5",
36
- "typescript": "^5.4.5"
32
+ "lefthook": "^1.7.18",
33
+ "prettier": "^3.3.3",
34
+ "typescript": "^5.6.2"
37
35
  },
38
36
  "scripts": {
39
37
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests",
@@ -44,12 +42,8 @@
44
42
  "lefthook:uninstall": "lefthook uninstall"
45
43
  },
46
44
  "browserslist": [
47
- "> 1%",
45
+ "> 0.5%",
48
46
  "not dead",
49
47
  "not op_mini all"
50
- ],
51
- "engines": {
52
- "npm": ">=8.0.0",
53
- "node": ">=18.0.0"
54
- }
48
+ ]
55
49
  }
package/src/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
  export { Logger } from './logging/Logger.js';
3
3
  export { addLeadingZero } from './util/addLeadingZero.js';
4
4
  export { capitalize } from './util/capitalize.js';
5
+ export { delayPromise } from './util/delayPromise.js';
6
+ export { fetchRetry } from './util/fetchRetry.js';
5
7
  export { getObjValueByPath } from './util/getObjValueByPath.js';
6
8
  export { getRandomInt } from './util/getRandomInt.js';
7
9
  export { getURLParam } from './util/getURLParam.js';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Returns a promise with delayed resolve.
3
+ * @param {number} delayMS - Promise resolve delay in milliseconds.
4
+ * @returns {Promise<void>} Delayed resolve promise.
5
+ */
6
+ export const delayPromise = (delayMS) => new Promise((resolve) => setTimeout(resolve, delayMS));
@@ -0,0 +1,42 @@
1
+ import { Logger } from '../logging/Logger.js';
2
+ import { delayPromise } from './delayPromise.js';
3
+
4
+ const logger = new Logger('fetchRetry');
5
+
6
+ /**
7
+ * Fetch with retry.
8
+ * @param {string | URL | Request} resource - Fetch URL.
9
+ * @param {RequestInit} [fetchOptions] - Fetch options.
10
+ * @param {{ delay?: number, numRetries?: number, isLog?: boolean}} [retryOptions] - Retry options.
11
+ * @returns {Promise<Response>} Fetch result.
12
+ */
13
+ export const fetchRetry = (resource, fetchOptions, retryOptions) => {
14
+ retryOptions = retryOptions ?? {};
15
+ retryOptions.delay = retryOptions.delay ?? 1000;
16
+ retryOptions.numRetries = retryOptions.numRetries ?? 3;
17
+ retryOptions.isLog = retryOptions.isLog ?? false;
18
+ return fetch(resource, fetchOptions)
19
+ .then((response) => {
20
+ if (retryOptions.isLog && !response.ok) {
21
+ logger.warn('Fetch failed', { fetchOptions, responseStatus: response.status });
22
+ }
23
+ return response.ok ? response.json() : Promise.reject(response);
24
+ })
25
+ .catch((error) => {
26
+ if (retryOptions.isLog) {
27
+ logger.exception('Fetch error', error);
28
+ }
29
+ retryOptions.numRetries -= 1;
30
+ retryOptions.delay *= 2;
31
+ if (retryOptions.numRetries > 0) {
32
+ if (retryOptions.isLog) {
33
+ logger.warn('Fetch retry', { resource, fetchOptions, retryOptions });
34
+ }
35
+ delayPromise(retryOptions.delay).then(() => {
36
+ return fetchRetry(resource, fetchOptions, retryOptions);
37
+ });
38
+ } else {
39
+ throw error;
40
+ }
41
+ });
42
+ };
@@ -3,6 +3,7 @@
3
3
  * @param {object} obj - TBD.
4
4
  * @param {string} path - TBD.
5
5
  * @param {*} value - TBD.
6
+ * @throws {SyntaxError} Error when illegal path value has been provided.
6
7
  */
7
8
  export function setObjValueByPath(obj, path, value) {
8
9
  if (!obj || !path) {
@@ -10,6 +11,9 @@ export function setObjValueByPath(obj, path, value) {
10
11
  }
11
12
  const keyParts = path.split('.');
12
13
  const nextKey = keyParts[0];
14
+ if (nextKey === '__proto__') {
15
+ throw new SyntaxError('Security violation error. Cannot use "__proto__" as parameter.');
16
+ }
13
17
  if (keyParts.length === 1) {
14
18
  obj[nextKey] = value;
15
19
  }
package/types/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export { Logger } from "./logging/Logger.js";
2
2
  export { addLeadingZero } from "./util/addLeadingZero.js";
3
3
  export { capitalize } from "./util/capitalize.js";
4
+ export { delayPromise } from "./util/delayPromise.js";
5
+ export { fetchRetry } from "./util/fetchRetry.js";
4
6
  export { getObjValueByPath } from "./util/getObjValueByPath.js";
5
7
  export { getRandomInt } from "./util/getRandomInt.js";
6
8
  export { getURLParam } from "./util/getURLParam.js";
@@ -7,7 +7,7 @@ export class Logger {
7
7
  /**
8
8
  * @type {(error) => {}}
9
9
  */
10
- static exceptionHandler: (error) => {};
10
+ static exceptionHandler: (error: any) => {};
11
11
  /**
12
12
  * Creates a new Logger instance.
13
13
  * @param {string} name - The logger name.
@@ -1 +1 @@
1
- {"version":3,"file":"Logger.d.ts","sourceRoot":"","sources":["../../src/logging/Logger.js"],"names":[],"mappings":"AAGA,4BAA6B;AAC7B,2BAA4B;AAC5B,2BAA4B;AAC5B,4BAA6B;AAC7B,6BAA8B;AAE9B;IACE;;OAEG;IACH,yBAFU,CAAC,KAAK,KAAK,EAAE,CAEQ;IAE/B;;;OAGG;IACH,kBAFW,MAAM,EAShB;IANC,aAAgB;IAKhB,cAAkG;IAGpG;;;;OAIG;IACH,eAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,cAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,cAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,eAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,mBAHW,MAAM,aACN,KAAK,QAWf;CACF"}
1
+ {"version":3,"file":"Logger.d.ts","sourceRoot":"","sources":["../../src/logging/Logger.js"],"names":[],"mappings":"AAGA,4BAA6B;AAC7B,2BAA4B;AAC5B,2BAA4B;AAC5B,4BAA6B;AAC7B,6BAA8B;AAE9B;IACE;;OAEG;IACH,yBAFU,CAAC,KAAK,KAAA,KAAK,EAAE,CAEQ;IAE/B;;;OAGG;IACH,kBAFW,MAAM,EAShB;IANC,aAAgB;IAKhB,cAAkG;IAGpG;;;;OAIG;IACH,eAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,cAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,cAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,eAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,mBAHW,MAAM,aACN,KAAK,QAWf;CACF"}
@@ -0,0 +1,2 @@
1
+ export function delayPromise(delayMS: number): Promise<void>;
2
+ //# sourceMappingURL=delayPromise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delayPromise.d.ts","sourceRoot":"","sources":["../../src/util/delayPromise.js"],"names":[],"mappings":"AAKO,sCAHI,MAAM,GACJ,OAAO,CAAC,IAAI,CAAC,CAEqE"}
@@ -0,0 +1,6 @@
1
+ export function fetchRetry(resource: string | URL | Request, fetchOptions?: RequestInit, retryOptions?: {
2
+ delay?: number;
3
+ numRetries?: number;
4
+ isLog?: boolean;
5
+ }): Promise<Response>;
6
+ //# sourceMappingURL=fetchRetry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetchRetry.d.ts","sourceRoot":"","sources":["../../src/util/fetchRetry.js"],"names":[],"mappings":"AAYO,qCALI,MAAM,GAAG,GAAG,GAAG,OAAO,iBACtB,WAAW,iBACX;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CAAC,GACrD,OAAO,CAAC,QAAQ,CAAC,CA+B7B"}
@@ -1 +1 @@
1
- {"version":3,"file":"getObjValueByPath.d.ts","sourceRoot":"","sources":["../../src/util/getObjValueByPath.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,uCAJW,MAAM,QACN,MAAM,OAahB"}
1
+ {"version":3,"file":"getObjValueByPath.d.ts","sourceRoot":"","sources":["../../src/util/getObjValueByPath.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,uCAJW,MAAM,QACN,MAAM,GACJ,GAAC,CAYb"}
@@ -1 +1 @@
1
- {"version":3,"file":"getURLParam.d.ts","sourceRoot":"","sources":["../../src/util/getURLParam.js"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,iCALW,MAAM,mCAEN,OAAO,GACL,MAAM,CAWlB"}
1
+ {"version":3,"file":"getURLParam.d.ts","sourceRoot":"","sources":["../../src/util/getURLParam.js"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,iCALW,MAAM,iBACN,GAAC,eACD,OAAO,GACL,MAAM,CAWlB"}
@@ -3,6 +3,7 @@
3
3
  * @param {object} obj - TBD.
4
4
  * @param {string} path - TBD.
5
5
  * @param {*} value - TBD.
6
+ * @throws {SyntaxError} Error when illegal path value has been provided.
6
7
  */
7
8
  export function setObjValueByPath(obj: object, path: string, value: any): void;
8
9
  //# sourceMappingURL=setObjValueByPath.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"setObjValueByPath.d.ts","sourceRoot":"","sources":["../../src/util/setObjValueByPath.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,uCAJW,MAAM,QACN,MAAM,oBAahB"}
1
+ {"version":3,"file":"setObjValueByPath.d.ts","sourceRoot":"","sources":["../../src/util/setObjValueByPath.js"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,uCALW,MAAM,QACN,MAAM,SACN,GAAC,QAgBX"}