-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (52 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { SCRAPE_IT_CLOUD_API_URL, SCRAPE_IT_CLOUD_API_ENDPOINT } = require('./const');
const { doRequest } = require('./utils/http');
class ScrapeitCloud {
/**
*
* @param {apiKey} apiKey - Scrapeit Cloud API Key
* @throws {Error}
*
*/
constructor(apiKey = null) {
if (!apiKey) {
throw new Error('API Key is not provided');
}
this.apiKey = apiKey;
}
/**
*
* @param {params} params - Scrapeit Cloud API Params
* @throws {Error}
* @returns {object} Scrapeit Cloud API Response
*
*/
async scrape(params) {
const responseBody = await doRequest(
`${SCRAPE_IT_CLOUD_API_URL}${SCRAPE_IT_CLOUD_API_ENDPOINT}`,
{
'x-api-key': this.apiKey,
},
{
...params,
source: 'nodejs_sdk'
}
);
let result = {};
try {
result = JSON.parse(responseBody);
} catch (e) {
result = responseBody;
}
if (result.status === 'ok') {
return result.scrapingResult;
}
if (result.error) {
throw new Error(result.error);
}
if (result.status === 'error') {
throw new Error(`${result.message}: ${JSON.stringify(result.errors || {})}`);
}
return result;
}
}
module.exports = ScrapeitCloud;