@ezs/basics 1.19.0 → 1.22.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,44 @@
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.22.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.21.0...@ezs/basics@1.22.0) (2022-09-05)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * use location to check filename ([cb9c6e2](https://github.com/Inist-CNRS/ezs/commit/cb9c6e2f42d12c287d806b4ccebf8a4f8607e755))
12
+
13
+
14
+ ### Features
15
+
16
+ * 🎸 add file load ([a06c221](https://github.com/Inist-CNRS/ezs/commit/a06c221c3463b4299383db5c613f5cabc687961c))
17
+
18
+
19
+
20
+
21
+
22
+ # [1.21.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.20.0...@ezs/basics@1.21.0) (2022-07-11)
23
+
24
+
25
+ ### Features
26
+
27
+ * 🎸 add target option to URLRequest ([3861c5e](https://github.com/Inist-CNRS/ezs/commit/3861c5e10b4dcfd060b49fa9b23f2bcf98d8c942))
28
+
29
+
30
+
31
+
32
+
33
+ # [1.20.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.19.0...@ezs/basics@1.20.0) (2022-06-24)
34
+
35
+
36
+ ### Features
37
+
38
+ * 🎸 add [URLPager] ([59dcbdc](https://github.com/Inist-CNRS/ezs/commit/59dcbdca0c33eaf2ebf5a195de153cec802d1bc1))
39
+
40
+
41
+
42
+
43
+
6
44
  # [1.19.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.18.0...@ezs/basics@1.19.0) (2022-06-22)
7
45
 
8
46
 
package/README.md CHANGED
@@ -18,6 +18,7 @@ npm install @ezs/basics
18
18
  - [CSVObject](#csvobject)
19
19
  - [CSVParse](#csvparse)
20
20
  - [CSVString](#csvstring)
21
+ - [FILELoad](#fileload)
21
22
  - [FILESave](#filesave)
22
23
  - [INIString](#inistring)
23
24
  - [JSONParse](#jsonparse)
@@ -32,7 +33,9 @@ npm install @ezs/basics
32
33
  - [TXTZip](#txtzip)
33
34
  - [URLConnect](#urlconnect)
34
35
  - [URLFetch](#urlfetch)
36
+ - [URLPagination](#urlpagination)
35
37
  - [URLParse](#urlparse)
38
+ - [URLRequest](#urlrequest)
36
39
  - [URLStream](#urlstream)
37
40
  - [URLString](#urlstring)
38
41
  - [XMLConvert](#xmlconvert)
@@ -190,6 +193,41 @@ a;b;c
190
193
 
191
194
  Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
192
195
 
196
+ ### FILELoad
197
+
198
+ Take `Object` containing filename et throw content by chunk
199
+
200
+ ```json
201
+ [ fi1e1.csv, file2.csv ]
202
+ ```
203
+
204
+ Script:
205
+
206
+ ```ini
207
+ [use]
208
+ plugin = analytics
209
+ plugin = basics
210
+
211
+ [FILELoad]
212
+ location = /tmp
213
+ [CSVParse]
214
+ ```
215
+
216
+ Output:
217
+
218
+ ```json
219
+ [
220
+ (...)
221
+ ]
222
+ ```
223
+
224
+ #### Parameters
225
+
226
+ - `location` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Directory location (optional, default `TMPDIR`)
227
+ - `compress` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Enable gzip compression (optional, default `false`)
228
+
229
+ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
230
+
193
231
  ### FILESave
194
232
 
195
233
  Take data, convert it to buffer and append it to file
@@ -610,6 +648,68 @@ Or if no target is specified, the output will be the returned content of URL.
610
648
 
611
649
  Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
612
650
 
651
+ ### URLPagination
652
+
653
+ Take `Object` and multiple it to make it one object per page
654
+
655
+ Input:
656
+
657
+ ```json
658
+ [{"q": "a"}]
659
+ ```
660
+
661
+ Script:
662
+
663
+ ```ini
664
+ [URLRequest]
665
+ url = https://api.search.net
666
+
667
+ [URLPagination]
668
+ total = get('total')
669
+ ```
670
+
671
+ Output:
672
+
673
+ ```json
674
+ [
675
+ {
676
+ "q": "a",
677
+ "total": 22
678
+ "offset": 0,
679
+ "pageNumber": 1,
680
+ "totalPages", 3,
681
+ "maxPages": 1000,
682
+ "limit": 10
683
+ },
684
+ {
685
+ "q": "a",
686
+ "total": 22
687
+ "offset": 10,
688
+ "pageNumber": 2,
689
+ "totalPages", 3,
690
+ "maxPages": 1000,
691
+ "limit": 10
692
+ },
693
+ {
694
+ "q": "a",
695
+ "total": 22
696
+ "offset": 20,
697
+ "pageNumber": 3,
698
+ "totalPages", 3,
699
+ "maxPages": 1000,
700
+ "limit": 10
701
+ }
702
+ ]
703
+ ```
704
+
705
+ #### Parameters
706
+
707
+ - `total` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** total to use for the pagination (optional, default `0`)
708
+ - `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** limit to use to pagination (optional, default `10`)
709
+ - `maxPages` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** maxPages to use to pagination (optional, default `1000`)
710
+
711
+ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
712
+
613
713
  ### URLParse
614
714
 
615
715
  Take an URL `String`, parse it and return `Object`.
@@ -637,6 +737,45 @@ See:
637
737
 
638
738
  Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
639
739
 
740
+ ### URLRequest
741
+
742
+ Take `Object` as parameters of URL, throw each chunk from the result
743
+
744
+ Input:
745
+
746
+ ```json
747
+ [{"q": "a"}]
748
+ ```
749
+
750
+ Script:
751
+
752
+ ```ini
753
+ [URLRequest]
754
+ url = https://api.search.net
755
+ ```
756
+
757
+ Output:
758
+
759
+ ```json
760
+ [
761
+ {
762
+ "result": "a"
763
+ }
764
+ ]
765
+ ```
766
+
767
+ #### Parameters
768
+
769
+ - `url` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** URL to fetch
770
+ - `json` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** parse result as json (optional, default `true`)
771
+ - `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** choose the key to set
772
+ - `timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Timeout in milliseconds (optional, default `1000`)
773
+ - `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`)
774
+ - `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`)
775
+ - `insert` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** a header response value in the result
776
+
777
+ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
778
+
640
779
  ### URLStream
641
780
 
642
781
  Take `String` as URL, throw each chunk from the result or
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = FILELoad;
7
+
8
+ var _zlib = require("zlib");
9
+
10
+ var _fs = require("fs");
11
+
12
+ var _path = require("path");
13
+
14
+ var _os = require("os");
15
+
16
+ /**
17
+ * Take `Object` containing filename et throw content by chunk
18
+ *
19
+ * ```json
20
+ * [ fi1e1.csv, file2.csv ]
21
+ * ```
22
+ *
23
+ * Script:
24
+ *
25
+ * ```ini
26
+ * [use]
27
+ * plugin = analytics
28
+ * plugin = basics
29
+ *
30
+ * [FILELoad]
31
+ * location = /tmp
32
+ * [CSVParse]
33
+ *
34
+ * ```
35
+ *
36
+ * Output:
37
+ *
38
+ * ```json
39
+ * [
40
+ * (...)
41
+ * ]
42
+ * ```
43
+ *
44
+ * @name FILELoad
45
+ * @param {String} [location=TMPDIR] Directory location
46
+ * @param {Boolean} [compress=false] Enable gzip compression
47
+ * @returns {Object}
48
+ */
49
+ function FILELoad(data, feed) {
50
+ if (this.isLast()) {
51
+ feed.close();
52
+ return;
53
+ }
54
+
55
+ const cwd = process.cwd();
56
+ const tpd = (0, _os.tmpdir)();
57
+ const location = this.getParam('location', tpd);
58
+ const compress = this.getParam('compress', false);
59
+ const locations = [cwd, tpd, location];
60
+ const file = locations.filter(Boolean).map(dir => (0, _path.resolve)(dir, String(data).trim())).filter(fil => (0, _fs.existsSync)(fil)).shift();
61
+
62
+ if (!file) {
63
+ feed.end();
64
+ return;
65
+ }
66
+
67
+ if (compress) {
68
+ feed.flow((0, _fs.createReadStream)(file).pipe((0, _zlib.createGunzip)()));
69
+ return;
70
+ }
71
+
72
+ feed.flow((0, _fs.createReadStream)(file));
73
+ }
package/lib/index.js CHANGED
@@ -41,6 +41,10 @@ var _urlFetch = _interopRequireDefault(require("./url-fetch"));
41
41
 
42
42
  var _urlParse = _interopRequireDefault(require("./url-parse"));
43
43
 
44
+ var _urlRequest = _interopRequireDefault(require("./url-request"));
45
+
46
+ var _urlPagination = _interopRequireDefault(require("./url-pagination"));
47
+
44
48
  var _urlString = _interopRequireDefault(require("./url-string"));
45
49
 
46
50
  var _urlStream = _interopRequireDefault(require("./url-stream"));
@@ -55,6 +59,8 @@ var _iniString = _interopRequireDefault(require("./ini-string"));
55
59
 
56
60
  var _fileSave = _interopRequireDefault(require("./file-save"));
57
61
 
62
+ var _fileLoad = _interopRequireDefault(require("./file-load"));
63
+
58
64
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59
65
 
60
66
  const funcs = {
@@ -75,6 +81,8 @@ const funcs = {
75
81
  JSONParse: _jsonParse.default,
76
82
  JSONString: _jsonString.default,
77
83
  URLFetch: _urlFetch.default,
84
+ URLPagination: _urlPagination.default,
85
+ URLRequest: _urlRequest.default,
78
86
  URLParse: _urlParse.default,
79
87
  URLString: _urlString.default,
80
88
  URLStream: _urlStream.default,
@@ -83,6 +91,7 @@ const funcs = {
83
91
  ZIPExtract: _zipExtract.default,
84
92
  INIString: _iniString.default,
85
93
  FILESave: _fileSave.default,
94
+ FILELoad: _fileLoad.default,
86
95
  // aliases
87
96
  bufferify: _bufObject.default.BUFObject,
88
97
  concat: _txtConcat.default.TXTConcat,
package/lib/request.js CHANGED
@@ -13,7 +13,10 @@ const request = (url, parameters) => async () => {
13
13
  const response = await (0, _fetchWithProxy.default)(url, parameters);
14
14
 
15
15
  if (!response.ok) {
16
- throw new Error(response.statusText);
16
+ const err = new Error(response.statusText);
17
+ const text = await response.text();
18
+ err.responseText = text;
19
+ throw err;
17
20
  }
18
21
 
19
22
  return response;
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);
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = URLPager;
7
+
8
+ var _debug = _interopRequireDefault(require("debug"));
9
+
10
+ var _url = require("url");
11
+
12
+ var _nodeAbortController = _interopRequireDefault(require("node-abort-controller"));
13
+
14
+ var _lodash = _interopRequireDefault(require("lodash.get"));
15
+
16
+ var _parseHeaders = _interopRequireDefault(require("parse-headers"));
17
+
18
+ var _asyncRetry = _interopRequireDefault(require("async-retry"));
19
+
20
+ var _request = _interopRequireDefault(require("./request"));
21
+
22
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
+
24
+ /**
25
+ * Take `Object` as parameters of URL, throw each chunk from the result
26
+ *
27
+ *
28
+ * Input:
29
+ *
30
+ * ```json
31
+ * [{"q": "a"}]
32
+ * ```
33
+ *
34
+ * Script:
35
+ *
36
+ * ```ini
37
+ * [URLPager]
38
+ * url = https://api.search.net
39
+ * path = total
40
+ * ```
41
+ *
42
+ * Output:
43
+ *
44
+ * ```json
45
+ * [
46
+ * {
47
+ * "q": "a",
48
+ * "total": 22
49
+ * "offset": 0,
50
+ * "pageNumber": 1,
51
+ * "totalPages", 3,
52
+ * "maxPages": 1000,
53
+ * "limit": 10
54
+ * },
55
+ * {
56
+ * "q": "a",
57
+ * "total": 22
58
+ * "offset": 10,
59
+ * "pageNumber": 2,
60
+ * "totalPages", 3,
61
+ * "maxPages": 1000,
62
+ * "limit": 10
63
+ * },
64
+ * {
65
+ * "q": "a",
66
+ * "total": 22
67
+ * "offset": 20,
68
+ * "pageNumber": 3,
69
+ * "totalPages", 3,
70
+ * "maxPages": 1000,
71
+ * "limit": 10
72
+ * }
73
+ * ]
74
+ * ```
75
+ *
76
+ * #### Example with URLs
77
+ *
78
+ * Input:
79
+ *
80
+ * ```json
81
+ * [
82
+ * "https://httpbin.org/get?a=a",
83
+ * "https://httpbin.org/get?a=b",
84
+ * "https://httpbin.org/get?a=c"
85
+ * ]
86
+ * ```
87
+ *
88
+ * Script:
89
+ *
90
+ * ```ini
91
+ * [URLPager]
92
+ * path = .args
93
+ * ```
94
+ *
95
+ * Output:
96
+ *
97
+ * ```json
98
+ * [{"a": "a"}, {"a": "b"}, {"a": "c" }]
99
+ * ```
100
+ *
101
+ * @name URLPager
102
+ * @param {String} [url] URL to fetch (by default input string is taken)
103
+ * @param {String} [path=total] choose the path to find the number of result
104
+ * @param {Number} [timeout=1000] Timeout in milliseconds
105
+ * @param {Boolean} [noerror=false] Ignore all errors, the target field will remain undefined
106
+ * @param {Number} [retries=5] The maximum amount of times to retry the connection
107
+ * @returns {Object}
108
+ */
109
+ async function URLPager(data, feed) {
110
+ if (this.isLast()) {
111
+ return feed.close();
112
+ }
113
+
114
+ const url = this.getParam('url');
115
+ const path = this.getParam('path', 'total');
116
+ const limit = Number(this.getParam('limit', 10));
117
+ const maxPages = Number(this.getParam('maxPages', 1000));
118
+ const retries = Number(this.getParam('retries', 5));
119
+ const noerror = Boolean(this.getParam('noerror', false));
120
+ const timeout = Number(this.getParam('timeout')) || 1000;
121
+ const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
122
+ const cURL = new _url.URL(url || data);
123
+ const controller = new _nodeAbortController.default();
124
+ const parameters = {
125
+ timeout,
126
+ headers,
127
+ signal: controller.signal
128
+ };
129
+ const options = {
130
+ retries
131
+ };
132
+ cURL.search = new _url.URLSearchParams(data);
133
+
134
+ const onError = e => {
135
+ controller.abort();
136
+
137
+ if (noerror) {
138
+ (0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLPager] <${e}>`);
139
+ return feed.send(data);
140
+ }
141
+
142
+ (0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLPager] <${e}>`);
143
+ return feed.send(e);
144
+ };
145
+
146
+ try {
147
+ const response = await (0, _asyncRetry.default)((0, _request.default)(cURL.href, parameters), options);
148
+ const json = await response.json();
149
+ const total = (0, _lodash.default)(json, path);
150
+
151
+ if (total === 0) {
152
+ return onError(new Error('No result.'));
153
+ }
154
+
155
+ if (total === undefined) {
156
+ return onError(new Error('Unexpected response.'));
157
+ }
158
+
159
+ let totalPages = Math.ceil(json.total / limit);
160
+
161
+ if (totalPages > maxPages) {
162
+ totalPages = maxPages;
163
+ }
164
+
165
+ for (let pageNumber = 1; pageNumber <= totalPages; pageNumber += 1) {
166
+ feed.write({ ...data,
167
+ offset: (pageNumber - 1) * limit,
168
+ pageNumber,
169
+ totalPages,
170
+ maxPages,
171
+ limit
172
+ });
173
+ }
174
+
175
+ feed.end();
176
+ } catch (e) {
177
+ onError(e);
178
+ }
179
+ }
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = URLPagination;
7
+
8
+ /**
9
+ * Take `Object` and multiple it to make it one object per page
10
+ *
11
+ *
12
+ * Input:
13
+ *
14
+ * ```json
15
+ * [{"q": "a"}]
16
+ * ```
17
+ *
18
+ * Script:
19
+ *
20
+ * ```ini
21
+ * [URLRequest]
22
+ * url = https://api.search.net
23
+ *
24
+ * [URLPagination]
25
+ * total = get('total')
26
+ * ```
27
+ *
28
+ * Output:
29
+ *
30
+ * ```json
31
+ * [
32
+ * {
33
+ * "q": "a",
34
+ * "total": 22
35
+ * "offset": 0,
36
+ * "pageNumber": 1,
37
+ * "totalPages", 3,
38
+ * "maxPages": 1000,
39
+ * "limit": 10
40
+ * },
41
+ * {
42
+ * "q": "a",
43
+ * "total": 22
44
+ * "offset": 10,
45
+ * "pageNumber": 2,
46
+ * "totalPages", 3,
47
+ * "maxPages": 1000,
48
+ * "limit": 10
49
+ * },
50
+ * {
51
+ * "q": "a",
52
+ * "total": 22
53
+ * "offset": 20,
54
+ * "pageNumber": 3,
55
+ * "totalPages", 3,
56
+ * "maxPages": 1000,
57
+ * "limit": 10
58
+ * }
59
+ * ]
60
+ * ```
61
+ *
62
+ *
63
+ * @name URLPagination
64
+ * @param {Number} [total=0] total to use for the pagination
65
+ * @param {Number} [limit=10] limit to use to pagination
66
+ * @param {Number} [maxPages=1000] maxPages to use to pagination
67
+ * @returns {Object}
68
+ */
69
+ function URLPagination(data, feed) {
70
+ if (this.isLast()) {
71
+ return feed.close();
72
+ }
73
+
74
+ const total = Number(this.getParam('total', 0));
75
+ const limit = Number(this.getParam('limit', 10));
76
+ const maxPages = Number(this.getParam('maxPages', 1000));
77
+
78
+ if (total === 0) {
79
+ return feed.send(new Error('No result.'));
80
+ }
81
+
82
+ if (Number.isNaN(total)) {
83
+ return feed.send(new Error('Unexpected response.'));
84
+ }
85
+
86
+ let totalPages = Math.ceil(total / limit);
87
+
88
+ if (totalPages > maxPages) {
89
+ totalPages = maxPages;
90
+ }
91
+
92
+ for (let pageNumber = 1; pageNumber <= totalPages; pageNumber += 1) {
93
+ feed.write({ ...data,
94
+ offset: (pageNumber - 1) * limit,
95
+ pageNumber,
96
+ totalPages,
97
+ maxPages,
98
+ limit
99
+ });
100
+ }
101
+
102
+ feed.end();
103
+ }
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = URLRequest;
7
+
8
+ var _lodash = _interopRequireDefault(require("lodash.set"));
9
+
10
+ var _debug = _interopRequireDefault(require("debug"));
11
+
12
+ var _url = require("url");
13
+
14
+ var _nodeAbortController = _interopRequireDefault(require("node-abort-controller"));
15
+
16
+ var _parseHeaders = _interopRequireDefault(require("parse-headers"));
17
+
18
+ var _asyncRetry = _interopRequireDefault(require("async-retry"));
19
+
20
+ var _request = _interopRequireDefault(require("./request"));
21
+
22
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
+
24
+ /**
25
+ * Take `Object` as parameters of URL, throw each chunk from the result
26
+ *
27
+ *
28
+ * Input:
29
+ *
30
+ * ```json
31
+ * [{"q": "a"}]
32
+ * ```
33
+ *
34
+ * Script:
35
+ *
36
+ * ```ini
37
+ * [URLRequest]
38
+ * url = https://api.search.net
39
+ * ```
40
+ *
41
+ * Output:
42
+ *
43
+ * ```json
44
+ * [
45
+ * {
46
+ * "result": "a"
47
+ * }
48
+ * ]
49
+ * ```
50
+ *
51
+ * @name URLRequest
52
+ * @param {String} [url] URL to fetch
53
+ * @param {Boolean} [json=true] parse result as json
54
+ * @param {String} [target] choose the key to set
55
+ * @param {Number} [timeout=1000] Timeout in milliseconds
56
+ * @param {Boolean} [noerror=false] Ignore all errors, the target field will remain undefined
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
59
+ * @returns {Object}
60
+ */
61
+ async function URLRequest(data, feed) {
62
+ if (this.isLast()) {
63
+ return feed.close();
64
+ }
65
+
66
+ const url = this.getParam('url');
67
+ const json = Boolean(this.getParam('json', true));
68
+ const target = [].concat(this.getParam('target')).filter(Boolean).shift();
69
+ const retries = Number(this.getParam('retries', 5));
70
+ const noerror = Boolean(this.getParam('noerror', false));
71
+ const timeout = Number(this.getParam('timeout')) || 1000;
72
+ const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
73
+ const inserts = [].concat(this.getParam('insert')).filter(Boolean);
74
+ const cURL = new _url.URL(url || data);
75
+ const controller = new _nodeAbortController.default();
76
+ const parameters = {
77
+ timeout,
78
+ headers,
79
+ signal: controller.signal
80
+ };
81
+ const options = {
82
+ retries
83
+ };
84
+
85
+ if (url) {
86
+ cURL.search = new _url.URLSearchParams(data);
87
+ }
88
+
89
+ const onError = e => {
90
+ controller.abort();
91
+
92
+ if (noerror) {
93
+ (0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLRequest] <${e}>`);
94
+ return feed.send(data);
95
+ }
96
+
97
+ (0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLRequest] <${e}>`);
98
+ return feed.send(e);
99
+ };
100
+
101
+ try {
102
+ const response = await (0, _asyncRetry.default)((0, _request.default)(cURL.href, parameters), options);
103
+ const func = json ? 'json' : 'text';
104
+ const value = await response[func]();
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);
118
+ } catch (e) {
119
+ onError(e);
120
+ }
121
+ }
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.19.0",
4
+ "version": "1.22.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": "2003a35bd4f57965f324309db6cb0cb2afbeefbc",
39
+ "gitHead": "ae3bd633484915926ad8dac4040e191635939a7d",
40
40
  "homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/basics#readme",
41
41
  "keywords": [
42
42
  "ezs"