@ezs/basics 1.19.0 → 1.20.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.20.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.19.0...@ezs/basics@1.20.0) (2022-06-24)
7
+
8
+
9
+ ### Features
10
+
11
+ * 🎸 add [URLPager] ([59dcbdc](https://github.com/Inist-CNRS/ezs/commit/59dcbdca0c33eaf2ebf5a195de153cec802d1bc1))
12
+
13
+
14
+
15
+
16
+
6
17
  # [1.19.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.18.0...@ezs/basics@1.19.0) (2022-06-22)
7
18
 
8
19
 
package/README.md CHANGED
@@ -32,7 +32,9 @@ npm install @ezs/basics
32
32
  - [TXTZip](#txtzip)
33
33
  - [URLConnect](#urlconnect)
34
34
  - [URLFetch](#urlfetch)
35
+ - [URLPagination](#urlpagination)
35
36
  - [URLParse](#urlparse)
37
+ - [URLRequest](#urlrequest)
36
38
  - [URLStream](#urlstream)
37
39
  - [URLString](#urlstring)
38
40
  - [XMLConvert](#xmlconvert)
@@ -610,6 +612,68 @@ Or if no target is specified, the output will be the returned content of URL.
610
612
 
611
613
  Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
612
614
 
615
+ ### URLPagination
616
+
617
+ Take `Object` and multiple it to make it one object per page
618
+
619
+ Input:
620
+
621
+ ```json
622
+ [{"q": "a"}]
623
+ ```
624
+
625
+ Script:
626
+
627
+ ```ini
628
+ [URLRequest]
629
+ url = https://api.search.net
630
+
631
+ [URLPagination]
632
+ total = get('total')
633
+ ```
634
+
635
+ Output:
636
+
637
+ ```json
638
+ [
639
+ {
640
+ "q": "a",
641
+ "total": 22
642
+ "offset": 0,
643
+ "pageNumber": 1,
644
+ "totalPages", 3,
645
+ "maxPages": 1000,
646
+ "limit": 10
647
+ },
648
+ {
649
+ "q": "a",
650
+ "total": 22
651
+ "offset": 10,
652
+ "pageNumber": 2,
653
+ "totalPages", 3,
654
+ "maxPages": 1000,
655
+ "limit": 10
656
+ },
657
+ {
658
+ "q": "a",
659
+ "total": 22
660
+ "offset": 20,
661
+ "pageNumber": 3,
662
+ "totalPages", 3,
663
+ "maxPages": 1000,
664
+ "limit": 10
665
+ }
666
+ ]
667
+ ```
668
+
669
+ #### Parameters
670
+
671
+ - `total` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** total to use for the pagination (optional, default `0`)
672
+ - `limit` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** limit to use to pagination (optional, default `10`)
673
+ - `maxPages` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** maxPages to use to pagination (optional, default `1000`)
674
+
675
+ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
676
+
613
677
  ### URLParse
614
678
 
615
679
  Take an URL `String`, parse it and return `Object`.
@@ -637,6 +701,43 @@ See:
637
701
 
638
702
  Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
639
703
 
704
+ ### URLRequest
705
+
706
+ Take `Object` as parameters of URL, throw each chunk from the result
707
+
708
+ Input:
709
+
710
+ ```json
711
+ [{"q": "a"}]
712
+ ```
713
+
714
+ Script:
715
+
716
+ ```ini
717
+ [URLRequest]
718
+ url = https://api.search.net
719
+ ```
720
+
721
+ Output:
722
+
723
+ ```json
724
+ [
725
+ {
726
+ "result": "a"
727
+ }
728
+ ]
729
+ ```
730
+
731
+ #### Parameters
732
+
733
+ - `url` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** URL to fetch
734
+ - `json` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** parse result as json (optional, default `true`)
735
+ - `timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Timeout in milliseconds (optional, default `1000`)
736
+ - `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
+ - `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`)
738
+
739
+ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
740
+
640
741
  ### URLStream
641
742
 
642
743
  Take `String` as URL, throw each chunk from the result or
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"));
@@ -75,6 +79,8 @@ const funcs = {
75
79
  JSONParse: _jsonParse.default,
76
80
  JSONString: _jsonString.default,
77
81
  URLFetch: _urlFetch.default,
82
+ URLPagination: _urlPagination.default,
83
+ URLRequest: _urlRequest.default,
78
84
  URLParse: _urlParse.default,
79
85
  URLString: _urlString.default,
80
86
  URLStream: _urlStream.default,
@@ -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,100 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = URLRequest;
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 _parseHeaders = _interopRequireDefault(require("parse-headers"));
15
+
16
+ var _asyncRetry = _interopRequireDefault(require("async-retry"));
17
+
18
+ var _request = _interopRequireDefault(require("./request"));
19
+
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+
22
+ /**
23
+ * Take `Object` as parameters of URL, throw each chunk from the result
24
+ *
25
+ *
26
+ * Input:
27
+ *
28
+ * ```json
29
+ * [{"q": "a"}]
30
+ * ```
31
+ *
32
+ * Script:
33
+ *
34
+ * ```ini
35
+ * [URLRequest]
36
+ * url = https://api.search.net
37
+ * ```
38
+ *
39
+ * Output:
40
+ *
41
+ * ```json
42
+ * [
43
+ * {
44
+ * "result": "a"
45
+ * }
46
+ * ]
47
+ * ```
48
+ *
49
+ * @name URLRequest
50
+ * @param {String} [url] URL to fetch
51
+ * @param {Boolean} [json=true] parse result as json
52
+ * @param {Number} [timeout=1000] Timeout in milliseconds
53
+ * @param {Boolean} [noerror=false] Ignore all errors, the target field will remain undefined
54
+ * @param {Number} [retries=5] The maximum amount of times to retry the connection
55
+ * @returns {Object}
56
+ */
57
+ async function URLRequest(data, feed) {
58
+ if (this.isLast()) {
59
+ return feed.close();
60
+ }
61
+
62
+ const url = this.getParam('url');
63
+ const json = Boolean(this.getParam('json', true));
64
+ const retries = Number(this.getParam('retries', 5));
65
+ const noerror = Boolean(this.getParam('noerror', false));
66
+ const timeout = Number(this.getParam('timeout')) || 1000;
67
+ const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
68
+ const cURL = new _url.URL(url || data);
69
+ const controller = new _nodeAbortController.default();
70
+ const parameters = {
71
+ timeout,
72
+ headers,
73
+ signal: controller.signal
74
+ };
75
+ const options = {
76
+ retries
77
+ };
78
+ cURL.search = new _url.URLSearchParams(data);
79
+
80
+ const onError = e => {
81
+ controller.abort();
82
+
83
+ if (noerror) {
84
+ (0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLRequest] <${e}>`);
85
+ return feed.send(data);
86
+ }
87
+
88
+ (0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLRequest] <${e}>`);
89
+ return feed.send(e);
90
+ };
91
+
92
+ try {
93
+ const response = await (0, _asyncRetry.default)((0, _request.default)(cURL.href, parameters), options);
94
+ const func = json ? 'json' : 'text';
95
+ const value = await response[func]();
96
+ feed.send(value);
97
+ } catch (e) {
98
+ onError(e);
99
+ }
100
+ }
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.20.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": "4a9a6877830fb4a769c1aae19d798b7985783760",
40
40
  "homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/basics#readme",
41
41
  "keywords": [
42
42
  "ezs"