zenrows 1.0.4 → 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
@@ -1,5 +1,5 @@
1
1
  # ZenRows Node.js SDK
2
- SDK to access [ZenRows](https://www.zenrows.com/) API directly from Node.js. We handle proxies rotation, headless browsers, and CAPTCHAs for you.
2
+ SDK to access [ZenRows](https://www.zenrows.com/) API directly from Node.js. ZenRows handles proxies rotation, headless browsers, and CAPTCHAs for you.
3
3
 
4
4
  ## Installation
5
5
  Install the SDK with npm.
@@ -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/';
@@ -37,6 +39,30 @@ const url = 'https://www.zenrows.com/';
37
39
 
38
40
  // Make your request from a given country. Requires premium_proxy
39
41
  proxy_country: '',
42
+
43
+ // Wait for a given CSS Selector to load in the DOM. Requires js_render
44
+ wait_for: '.content',
45
+
46
+ // Wait a fixed amount of time in milliseconds. Requires js_render
47
+ wait: 2500,
48
+
49
+ // Block specific resources from loading, check docs for the full list. Requires js_render
50
+ block_resources: 'image,media,font',
51
+
52
+ // Change the browser's window width and height. Requires js_render
53
+ window_width: 1920,
54
+ window_height: 1080,
55
+
56
+ // Will automatically use either desktop or mobile user agents in the headers
57
+ device: 'desktop',
58
+
59
+ // Will return the status code returned by the website
60
+ original_status: false,
61
+ }, {
62
+ headers: {
63
+ Referrer: 'https://www.google.com',
64
+ 'User-Agent': 'MyCustomUserAgent',
65
+ },
40
66
  });
41
67
 
42
68
  console.log(data);
@@ -56,6 +82,10 @@ const url = 'https://www.zenrows.com/';
56
82
  })();
57
83
  ```
58
84
 
85
+ You can also pass optional parameters and headers; the list above is a reference. For more info, check out [the documentation page](https://www.zenrows.com/documentation).
86
+
87
+ Sending headers to the target URL will overwrite our defaults. Be careful when doing it and contact us if there is any problem.
88
+
59
89
  ## Examples
60
90
 
61
91
  Take a look at the [examples directory](./examples) for Javascript and TypeScript files using the SDK.
package/dist/index.d.ts CHANGED
@@ -1,15 +1,33 @@
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;
5
8
  js_render?: boolean;
6
9
  premium_proxy?: boolean;
7
10
  proxy_country?: string;
11
+ wait_for?: string;
12
+ wait?: number;
13
+ block_resources?: string;
14
+ window_width?: number;
15
+ window_height?: number;
16
+ device?: string;
17
+ original_status?: boolean;
18
+ custom_headers?: boolean;
8
19
  [x: string]: unknown;
9
20
  }
21
+ interface Headers {
22
+ [x: string]: string;
23
+ }
10
24
  declare class ZenRows {
11
25
  readonly apiKey: string;
12
- constructor(apiKey: string);
13
- get(url: string, config?: Config): AxiosPromise;
26
+ readonly clientConfig: ClientConfig;
27
+ constructor(apiKey: string, clientConfig?: ClientConfig);
28
+ get(url: string, config?: Config, { headers }?: {
29
+ headers?: Headers;
30
+ }): AxiosPromise;
31
+ private applyRetries;
14
32
  }
15
33
  export { ZenRows };
package/dist/index.js CHANGED
@@ -2,23 +2,44 @@
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
- get(url, config) {
13
+ get(url, config, { headers = {} } = {}) {
12
14
  const params = Object.assign(Object.assign({}, config), { url, apikey: this.apiKey });
13
- const headers = {
14
- 'User-Agent': `zenrows/${version_1.VERSION} node`,
15
- };
15
+ const finalHeaders = Object.assign({ 'User-Agent': `zenrows/${version_1.VERSION} node` }, headers);
16
16
  const axiosRequestConfig = {
17
17
  baseURL: API_URL,
18
18
  params,
19
- headers,
19
+ headers: finalHeaders,
20
20
  };
21
+ if (headers && Object.keys(headers).length) {
22
+ params.custom_headers = true;
23
+ }
24
+ this.applyRetries();
21
25
  return (0, axios_1.default)(axiosRequestConfig);
22
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
+ }
23
44
  }
24
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.4";
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.4';
4
+ exports.VERSION = '1.1.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zenrows",
3
- "version": "1.0.4",
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
  }