zenrows 1.0.7 → 1.1.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 CHANGED
@@ -13,8 +13,10 @@ Start using the API by [creating your API Key](https://app.zenrows.com/register?
13
13
 
14
14
  The SDK uses [axios](https://axios-http.com/) for HTTP requests. The client's response will be an `AxiosPromise` if using TypeScript, a regular `Promise` otherwise.
15
15
 
16
+ It also uses [axios-retry](https://github.com/softonic/axios-retry) to automatically retry failed requests (status code 429 and 5XX). Retries are not active by default; you need to specify the number of retries, as shown below. It already includes an exponential back-off retry delay between failed requests.
17
+
16
18
  ```javascript
17
- const { ZenRows } = require('zenrows');
19
+ const { ZenRows } = require('zenrows', { retries: 1 });
18
20
 
19
21
  const apiKey = 'YOUR-API-KEY';
20
22
  const url = 'https://www.zenrows.com/';
package/dist/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import { AxiosPromise } from 'axios';
2
+ interface ClientConfig {
3
+ retries?: number;
4
+ }
2
5
  interface Config {
3
6
  autoparse?: boolean;
4
7
  css_extractor?: string;
@@ -20,9 +23,11 @@ interface Headers {
20
23
  }
21
24
  declare class ZenRows {
22
25
  readonly apiKey: string;
23
- constructor(apiKey: string);
26
+ readonly clientConfig: ClientConfig;
27
+ constructor(apiKey: string, clientConfig?: ClientConfig);
24
28
  get(url: string, config?: Config, { headers }?: {
25
29
  headers?: Headers;
26
30
  }): AxiosPromise;
31
+ private applyRetries;
27
32
  }
28
33
  export { ZenRows };
package/dist/index.js CHANGED
@@ -2,11 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ZenRows = void 0;
4
4
  const axios_1 = require("axios");
5
+ const axios_retry_1 = require("axios-retry");
5
6
  const version_1 = require("./version");
6
7
  const API_URL = 'https://api.zenrows.com/v1/';
7
8
  class ZenRows {
8
- constructor(apiKey) {
9
+ constructor(apiKey, clientConfig = {}) {
9
10
  this.apiKey = apiKey;
11
+ this.clientConfig = clientConfig;
10
12
  }
11
13
  get(url, config, { headers = {} } = {}) {
12
14
  const params = Object.assign(Object.assign({}, config), { url, apikey: this.apiKey });
@@ -19,7 +21,25 @@ class ZenRows {
19
21
  if (headers && Object.keys(headers).length) {
20
22
  params.custom_headers = true;
21
23
  }
24
+ this.applyRetries();
22
25
  return (0, axios_1.default)(axiosRequestConfig);
23
26
  }
27
+ applyRetries() {
28
+ var _a;
29
+ const retries = (_a = this.clientConfig.retries) !== null && _a !== void 0 ? _a : 0;
30
+ if (retries > 0) {
31
+ (0, axios_retry_1.default)(axios_1.default, {
32
+ retries,
33
+ retryDelay: axios_retry_1.default.exponentialDelay,
34
+ retryCondition: (error) => {
35
+ var _a;
36
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 429) {
37
+ return true;
38
+ }
39
+ return axios_retry_1.default.isNetworkOrIdempotentRequestError(error);
40
+ },
41
+ });
42
+ }
43
+ }
24
44
  }
25
45
  exports.ZenRows = ZenRows;
@@ -0,0 +1,4 @@
1
+ declare const queue: (concurrency: number) => {
2
+ enqueue: Function;
3
+ };
4
+ export { queue };
package/dist/queue.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ // TODO review disabled!!!!
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.queue = void 0;
14
+ /* eslint-disable @typescript-eslint/ban-types */
15
+ const queue = (concurrency) => {
16
+ let running = 0;
17
+ const tasks = [];
18
+ return {
19
+ enqueue: (givenTask, ...givenParams) => __awaiter(void 0, void 0, void 0, function* () {
20
+ tasks.push({ task: givenTask, params: givenParams });
21
+ if (running >= concurrency) {
22
+ return;
23
+ }
24
+ running += 1;
25
+ while (tasks.length > 0) {
26
+ const nextTask = tasks.shift();
27
+ if (nextTask) {
28
+ const { task, params } = nextTask;
29
+ // eslint-disable-next-line no-await-in-loop
30
+ yield task(...params);
31
+ }
32
+ }
33
+ running -= 1;
34
+ }),
35
+ };
36
+ };
37
+ exports.queue = queue;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.0.7";
1
+ export declare const VERSION = "1.1.0";
package/dist/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '1.0.7';
4
+ exports.VERSION = '1.1.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zenrows",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "description": "ZenRows Node SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -40,6 +40,7 @@
40
40
  "typescript": "^4.4.2"
41
41
  },
42
42
  "dependencies": {
43
- "axios": "^0.21.4"
43
+ "axios": "^0.21.4",
44
+ "axios-retry": "^3.2.4"
44
45
  }
45
46
  }