@ezs/basics 1.20.0 → 1.21.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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.21.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.20.0...@ezs/basics@1.21.0) (2022-07-11)
7
+
8
+
9
+ ### Features
10
+
11
+ * 🎸 add target option to URLRequest ([3861c5e](https://github.com/Inist-CNRS/ezs/commit/3861c5e10b4dcfd060b49fa9b23f2bcf98d8c942))
12
+
13
+
14
+
15
+
16
+
6
17
  # [1.20.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.19.0...@ezs/basics@1.20.0) (2022-06-24)
7
18
 
8
19
 
package/README.md CHANGED
@@ -723,7 +723,7 @@ Output:
723
723
  ```json
724
724
  [
725
725
  {
726
- "result": "a"
726
+ "result": "a"
727
727
  }
728
728
  ]
729
729
  ```
@@ -732,9 +732,11 @@ Output:
732
732
 
733
733
  - `url` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** URL to fetch
734
734
  - `json` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** parse result as json (optional, default `true`)
735
+ - `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** choose the key to set
735
736
  - `timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Timeout in milliseconds (optional, default `1000`)
736
737
  - `noerror` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Ignore all errors, the target field will remain undefined (optional, default `false`)
737
738
  - `retries` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The maximum amount of times to retry the connection (optional, default `5`)
739
+ - `insert` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** a header response value in the result
738
740
 
739
741
  Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
740
742
 
package/lib/url-fetch.js CHANGED
@@ -44,7 +44,7 @@ async function URLFetch(data, feed) {
44
44
 
45
45
  const url = this.getParam('url');
46
46
  const path = this.getParam('path');
47
- const target = this.getParam('target');
47
+ const target = [].concat(this.getParam('target')).filter(Boolean).shift();
48
48
  const json = Boolean(this.getParam('json', false));
49
49
  const retries = Number(this.getParam('retries', 5));
50
50
  const noerror = Boolean(this.getParam('noerror', false));
@@ -74,8 +74,10 @@ async function URLFetch(data, feed) {
74
74
  const func = json ? 'json' : 'text';
75
75
  const value = await response[func]();
76
76
 
77
- if (target && typeof target === 'string' && typeof data === 'object') {
78
- const result = { ...data
77
+ if (target) {
78
+ const result = typeof data === 'object' ? { ...data
79
+ } : {
80
+ input: data
79
81
  };
80
82
  (0, _lodash2.default)(result, target, value);
81
83
  return feed.send(result);
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = URLRequest;
7
7
 
8
+ var _lodash = _interopRequireDefault(require("lodash.set"));
9
+
8
10
  var _debug = _interopRequireDefault(require("debug"));
9
11
 
10
12
  var _url = require("url");
@@ -41,7 +43,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
41
43
  * ```json
42
44
  * [
43
45
  * {
44
- * "result": "a"
46
+ * "result": "a"
45
47
  * }
46
48
  * ]
47
49
  * ```
@@ -49,9 +51,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
49
51
  * @name URLRequest
50
52
  * @param {String} [url] URL to fetch
51
53
  * @param {Boolean} [json=true] parse result as json
54
+ * @param {String} [target] choose the key to set
52
55
  * @param {Number} [timeout=1000] Timeout in milliseconds
53
56
  * @param {Boolean} [noerror=false] Ignore all errors, the target field will remain undefined
54
57
  * @param {Number} [retries=5] The maximum amount of times to retry the connection
58
+ * @param {String} [insert] a header response value in the result
55
59
  * @returns {Object}
56
60
  */
57
61
  async function URLRequest(data, feed) {
@@ -61,10 +65,12 @@ async function URLRequest(data, feed) {
61
65
 
62
66
  const url = this.getParam('url');
63
67
  const json = Boolean(this.getParam('json', true));
68
+ const target = [].concat(this.getParam('target')).filter(Boolean).shift();
64
69
  const retries = Number(this.getParam('retries', 5));
65
70
  const noerror = Boolean(this.getParam('noerror', false));
66
71
  const timeout = Number(this.getParam('timeout')) || 1000;
67
72
  const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
73
+ const inserts = [].concat(this.getParam('insert')).filter(Boolean);
68
74
  const cURL = new _url.URL(url || data);
69
75
  const controller = new _nodeAbortController.default();
70
76
  const parameters = {
@@ -75,7 +81,10 @@ async function URLRequest(data, feed) {
75
81
  const options = {
76
82
  retries
77
83
  };
78
- cURL.search = new _url.URLSearchParams(data);
84
+
85
+ if (url) {
86
+ cURL.search = new _url.URLSearchParams(data);
87
+ }
79
88
 
80
89
  const onError = e => {
81
90
  controller.abort();
@@ -93,7 +102,19 @@ async function URLRequest(data, feed) {
93
102
  const response = await (0, _asyncRetry.default)((0, _request.default)(cURL.href, parameters), options);
94
103
  const func = json ? 'json' : 'text';
95
104
  const value = await response[func]();
96
- feed.send(value);
105
+
106
+ if (target) {
107
+ const result = typeof data === 'object' ? { ...data
108
+ } : {
109
+ url: data
110
+ };
111
+ (0, _lodash.default)(result, target, value);
112
+ inserts.forEach(i => (0, _lodash.default)(result, i, response.headers.get(i)));
113
+ return feed.send(result);
114
+ }
115
+
116
+ inserts.forEach(i => (0, _lodash.default)(value, i, response.headers.get(i)));
117
+ return feed.send(value);
97
118
  } catch (e) {
98
119
  onError(e);
99
120
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ezs/basics",
3
3
  "description": "Basics statements for EZS",
4
- "version": "1.20.0",
4
+ "version": "1.21.0",
5
5
  "author": "Nicolas Thouvenin <nthouvenin@gmail.com>",
6
6
  "bugs": "https://github.com/Inist-CNRS/ezs/issues",
7
7
  "dependencies": {
@@ -36,7 +36,7 @@
36
36
  "directories": {
37
37
  "test": "test"
38
38
  },
39
- "gitHead": "4a9a6877830fb4a769c1aae19d798b7985783760",
39
+ "gitHead": "b6d40a5733f24e3f138d999f5b7b53e42d3d812a",
40
40
  "homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/basics#readme",
41
41
  "keywords": [
42
42
  "ezs"