webcrawlerapi-js 1.1.0 → 1.1.1

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/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "webcrawlerapi-js",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
 
5
5
  "description": "JS client for WebcrawlerAPI",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
6
+ "main": "./dist/api.js",
7
+ "types": "./dist/@types/api.d.ts",
8
8
  "scripts": {
9
9
  "build": "tsc --outDir dist/"
10
10
  },
package/src/api.ts CHANGED
@@ -6,6 +6,9 @@ const BASE_PATH = "https://api.webcrawlerapi.com"
6
6
  const initialPullDelayMs = 2000
7
7
  const MaxPullRetries = 100
8
8
 
9
+ export { WebcrawlerApiError, JobStatus };
10
+ export * from "./model";
11
+
9
12
  export class WebcrawlerClient {
10
13
  protected apiVersion: string = "v1";
11
14
  protected apiKey: string
@@ -1,15 +0,0 @@
1
- import { CrawlRequest, Job, JobId, ScrapeRequest, ScrapeResponse } from "./model";
2
- export declare class WebcrawlerClient {
3
- protected apiVersion: string;
4
- protected apiKey: string;
5
- protected basePath: string;
6
- constructor(apiKey: string, basePath?: string, apiVersion?: string);
7
- scrapeAsync(scrapeRequest: ScrapeRequest): Promise<JobId>;
8
- scrapeWithMeta(scrapeRequest: ScrapeRequest, maxPollingRetries?: number): Promise<ScrapeResponse>;
9
- scrape(scrapeRequest: ScrapeRequest, maxPollingRetries?: number): Promise<any>;
10
- getScrapeResult(scrapeID: string): Promise<ScrapeResponse>;
11
- crawl(crawlRequest: CrawlRequest): Promise<Job>;
12
- crawlAsync(crawlRequest: CrawlRequest): Promise<JobId>;
13
- getJob(jobID: string): Promise<Job>;
14
- private sendRequest;
15
- }
@@ -1,13 +0,0 @@
1
- export declare const JobStatus: {
2
- readonly IN_PROGRESS: "in_progress";
3
- readonly NEW: "new";
4
- readonly DONE: "done";
5
- };
6
- export declare const HttpStatus: {
7
- readonly OK: 200;
8
- readonly BAD_REQUEST: 400;
9
- readonly UNAUTHORIZED: 401;
10
- readonly FORBIDDEN: 403;
11
- readonly NOT_FOUND: 404;
12
- readonly INTERNAL_SERVER_ERROR: 500;
13
- };
@@ -1,12 +0,0 @@
1
- export declare class WebcrawlerApiError extends Error {
2
- errorCode: string;
3
- errorMessage: string;
4
- statusCode?: number;
5
- constructor(errorCode: string, errorMessage: string, statusCode?: number);
6
- }
7
- export interface ErrorResponse {
8
- error_code?: string;
9
- error_message?: string;
10
- error?: string;
11
- }
12
- export declare function createErrorFromResponse(response: Response, data: ErrorResponse): WebcrawlerApiError;
@@ -1,4 +0,0 @@
1
- export * from "./api";
2
- export * from "./model";
3
- export * from "./constants";
4
- export * from "./errors";
@@ -1,62 +0,0 @@
1
- export interface ScrapeRequest {
2
- input: any;
3
- crawler_id: string;
4
- max_retries?: number;
5
- debug?: boolean;
6
- omit_cache?: boolean;
7
- }
8
- export interface JobId {
9
- id: string;
10
- }
11
- export interface ScrapeResponse {
12
- id: string;
13
- url: string;
14
- status: any;
15
- page_status_code: number;
16
- created_at: string;
17
- structured_data: any;
18
- recommended_pull_delay_ms: number;
19
- }
20
- export interface CrawlRequest {
21
- url: string;
22
- items_limit?: number;
23
- scrape_type?: string;
24
- whitelist_regexp?: string;
25
- blacklist_regexp?: string;
26
- allow_subdomains?: boolean;
27
- webhook_url?: string;
28
- }
29
- export interface Job {
30
- id: string;
31
- org_id: string;
32
- url: string;
33
- status: string;
34
- scrape_type: string;
35
- whitelist_regexp: string;
36
- blacklist_regexp: string;
37
- allow_subdomains: boolean;
38
- items_limit: number;
39
- created_at: string;
40
- finished_at: string;
41
- updated_at: string;
42
- webhook_url: string;
43
- recommended_pull_delay_ms: number;
44
- job_items: JobItem[];
45
- }
46
- export interface JobItem {
47
- id: string;
48
- job_id: string;
49
- original_url: string;
50
- page_status_code: number;
51
- markdown_content_url: string;
52
- status: string;
53
- title: string;
54
- last_error: string;
55
- created_at: string;
56
- updated_at: string;
57
- cost: number;
58
- referred_url: string;
59
- raw_content_url?: string;
60
- cleaned_content_url?: string;
61
- getContent(): Promise<string | null>;
62
- }
package/dist/api.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import { CrawlRequest, Job, JobId, ScrapeRequest, ScrapeResponse } from "./model";
2
- export declare class WebcrawlerClient {
3
- protected apiVersion: string;
4
- protected apiKey: string;
5
- protected basePath: string;
6
- constructor(apiKey: string, basePath?: string, apiVersion?: string);
7
- scrapeAsync(scrapeRequest: ScrapeRequest): Promise<JobId>;
8
- scrapeWithMeta(scrapeRequest: ScrapeRequest, maxPollingRetries?: number): Promise<ScrapeResponse>;
9
- scrape(scrapeRequest: ScrapeRequest, maxPollingRetries?: number): Promise<any>;
10
- getScrapeResult(scrapeID: string): Promise<ScrapeResponse>;
11
- crawl(crawlRequest: CrawlRequest): Promise<Job>;
12
- crawlAsync(crawlRequest: CrawlRequest): Promise<JobId>;
13
- getJob(jobID: string): Promise<Job>;
14
- private sendRequest;
15
- }
package/dist/api.js DELETED
@@ -1,195 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebcrawlerClient = void 0;
4
- const constants_1 = require("./constants");
5
- const errors_1 = require("./errors");
6
- const BASE_PATH = "https://api.webcrawlerapi.com";
7
- const initialPullDelayMs = 2000;
8
- const MaxPullRetries = 100;
9
- class WebcrawlerClient {
10
- constructor(apiKey, basePath = BASE_PATH, apiVersion = "v1") {
11
- this.apiVersion = "v1";
12
- this.apiKey = apiKey;
13
- this.basePath = basePath;
14
- this.apiVersion = apiVersion;
15
- }
16
- async scrapeAsync(scrapeRequest) {
17
- const url = `${this.basePath}/${this.apiVersion}/scrape`;
18
- const requestOptions = {
19
- 'method': 'POST',
20
- 'headers': {
21
- 'Content-Type': 'application/json',
22
- 'Authorization': `Bearer ${this.apiKey}`,
23
- "User-Agent": "WebcrawlerAPI-NodeJS-Client"
24
- },
25
- 'body': JSON.stringify(scrapeRequest),
26
- };
27
- return await this.sendRequest(url, requestOptions);
28
- }
29
- async scrapeWithMeta(scrapeRequest, maxPollingRetries = MaxPullRetries) {
30
- const url = `${this.basePath}/${this.apiVersion}/scrape`;
31
- const requestOptions = {
32
- 'method': 'POST',
33
- 'headers': {
34
- 'Content-Type': 'application/json',
35
- 'Authorization': `Bearer ${this.apiKey}`
36
- },
37
- 'body': JSON.stringify(scrapeRequest),
38
- };
39
- const jobIdResponse = await this.sendRequest(url, requestOptions);
40
- if (jobIdResponse.id === '') {
41
- throw new errors_1.WebcrawlerApiError('invalid_response', 'Failed to fetch job status', 0);
42
- }
43
- let delayIntervalMs = initialPullDelayMs;
44
- for (let i = 0; i < maxPollingRetries; i++) {
45
- await new Promise(resolve => setTimeout(resolve, delayIntervalMs));
46
- const scrapeResult = await this.getScrapeResult(jobIdResponse.id);
47
- if (scrapeRequest.debug) {
48
- console.log(`Scrape result: ${JSON.stringify(scrapeResult)}`);
49
- }
50
- if (scrapeResult.status !== constants_1.JobStatus.IN_PROGRESS && scrapeResult.status !== constants_1.JobStatus.NEW) {
51
- return scrapeResult;
52
- }
53
- if (scrapeResult.recommended_pull_delay_ms > 0) {
54
- delayIntervalMs = scrapeResult.recommended_pull_delay_ms;
55
- }
56
- }
57
- throw new errors_1.WebcrawlerApiError('timeout', 'Scraping took too long, please retry or increase the number of polling retries', 0);
58
- }
59
- async scrape(scrapeRequest, maxPollingRetries = MaxPullRetries) {
60
- const scrapeResult = await this.scrapeWithMeta(scrapeRequest, maxPollingRetries);
61
- return scrapeResult.structured_data;
62
- }
63
- async getScrapeResult(scrapeID) {
64
- const url = `${this.basePath}/${this.apiVersion}/scrape/${scrapeID}`;
65
- const requestOptions = {
66
- 'method': 'GET',
67
- 'headers': {
68
- 'Content-Type': 'application/json',
69
- 'Authorization': `Bearer ${this.apiKey}`,
70
- "User-Agent": "WebcrawlerAPI-NodeJS-Client",
71
- 'Cache-Control': 'no-cache, no-store, must-revalidate',
72
- 'Pragma': 'no-cache',
73
- 'Expires': '0'
74
- },
75
- };
76
- return await this.sendRequest(url, requestOptions);
77
- }
78
- async crawl(crawlRequest) {
79
- const url = `${this.basePath}/${this.apiVersion}/crawl`;
80
- const requestOptions = {
81
- 'method': 'POST',
82
- 'headers': {
83
- 'Content-Type': 'application/json',
84
- 'Authorization': `Bearer ${this.apiKey}`,
85
- "User-Agent": "WebcrawlerAPI-NodeJS-Client",
86
- 'Cache-Control': 'no-cache, no-store, must-revalidate',
87
- 'Pragma': 'no-cache',
88
- 'Expires': '0'
89
- },
90
- 'body': JSON.stringify(crawlRequest),
91
- };
92
- const jobIdResponse = await this.sendRequest(url, requestOptions);
93
- if (jobIdResponse.id === '') {
94
- throw new errors_1.WebcrawlerApiError('invalid_response', 'Failed to fetch job status', 0);
95
- }
96
- let delayIntervalMs = initialPullDelayMs;
97
- for (let i = 0; i < MaxPullRetries; i++) {
98
- await new Promise(resolve => setTimeout(resolve, delayIntervalMs));
99
- const timestamp = new Date().getTime();
100
- const job = await this.getJob(`${jobIdResponse.id}?t=${timestamp}`);
101
- if (job.status !== constants_1.JobStatus.IN_PROGRESS && job.status !== constants_1.JobStatus.NEW) {
102
- // Transform each job item to include getContent method
103
- job.job_items = job.job_items.map(item => ({
104
- ...item,
105
- getContent: async function () {
106
- if (job.status !== constants_1.JobStatus.DONE || this.status !== constants_1.JobStatus.DONE) {
107
- return null;
108
- }
109
- let contentUrl;
110
- switch (job.scrape_type) {
111
- case 'html':
112
- contentUrl = this.raw_content_url;
113
- break;
114
- case 'cleaned':
115
- contentUrl = this.cleaned_content_url;
116
- break;
117
- case 'markdown':
118
- contentUrl = this.markdown_content_url;
119
- break;
120
- }
121
- if (!contentUrl) {
122
- return null;
123
- }
124
- const response = await fetch(contentUrl, {
125
- headers: {
126
- 'Accept-Encoding': 'gzip, deflate, br',
127
- 'Accept': '*/*'
128
- }
129
- });
130
- if (!response.ok) {
131
- throw new Error(`Failed to fetch content: ${response.statusText}`);
132
- }
133
- return await response.text();
134
- }
135
- }));
136
- return job;
137
- }
138
- if (job.recommended_pull_delay_ms > 0) {
139
- delayIntervalMs = job.recommended_pull_delay_ms;
140
- }
141
- }
142
- throw new errors_1.WebcrawlerApiError('timeout', 'Crawling took too long, please retry or increase the number of polling retries', 0);
143
- }
144
- async crawlAsync(crawlRequest) {
145
- const url = `${this.basePath}/${this.apiVersion}/crawl`;
146
- const requestOptions = {
147
- 'method': 'POST',
148
- 'headers': {
149
- 'Content-Type': 'application/json',
150
- 'Authorization': `Bearer ${this.apiKey}`
151
- },
152
- 'body': JSON.stringify(crawlRequest),
153
- };
154
- return await this.sendRequest(url, requestOptions);
155
- }
156
- async getJob(jobID) {
157
- const url = `${this.basePath}/${this.apiVersion}/job/${jobID}`;
158
- const requestOptions = {
159
- 'method': 'GET',
160
- 'headers': {
161
- 'Content-Type': 'application/json',
162
- 'Authorization': `Bearer ${this.apiKey}`,
163
- "User-Agent": "WebcrawlerAPI-NodeJS-Client",
164
- 'Cache-Control': 'no-cache, no-store, must-revalidate',
165
- 'Pragma': 'no-cache',
166
- 'Expires': '0'
167
- }
168
- };
169
- return await this.sendRequest(url, requestOptions);
170
- }
171
- async sendRequest(url, requestOptions) {
172
- let response;
173
- try {
174
- response = await fetch(url, requestOptions);
175
- }
176
- catch (e) {
177
- throw new errors_1.WebcrawlerApiError('network_error', `Failed to send request: ${e}`, 0);
178
- }
179
- if (!response.ok) {
180
- try {
181
- const errorData = await response.json();
182
- throw (0, errors_1.createErrorFromResponse)(response, errorData);
183
- }
184
- catch (e) {
185
- if (e instanceof errors_1.WebcrawlerApiError) {
186
- throw e;
187
- }
188
- // If we can't parse the error response, create a generic error
189
- throw new errors_1.WebcrawlerApiError('unknown_error', `Request failed with status ${response.status} ${response.statusText}`, response.status);
190
- }
191
- }
192
- return response.json();
193
- }
194
- }
195
- exports.WebcrawlerClient = WebcrawlerClient;
@@ -1,16 +0,0 @@
1
- export interface ConfigurationParameters {
2
- apiKey?: string;
3
- basePath?: string;
4
- apiVersion?: string;
5
- }
6
- export declare class Configuration {
7
- /**
8
- * Base options for http call
9
- */
10
- baseOptions?: any;
11
- /**
12
- * API version, for example: "v1"
13
- */
14
- apiVersion: string;
15
- constructor(param?: ConfigurationParameters);
16
- }
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Configuration = void 0;
4
- const packageJson = require("../package.json");
5
- class Configuration {
6
- constructor(param = {}) {
7
- /**
8
- * API version, for example: "v1"
9
- */
10
- this.apiVersion = "v1";
11
- this.apiVersion = param.apiVersion;
12
- if (!this.baseOptions) {
13
- this.baseOptions = {};
14
- }
15
- this.baseOptions = {
16
- headers: Object.assign({ 'User-Agent': `webcrawlerapi-js/${packageJson.version}`, 'Content-Type': 'application/json' }, this.baseOptions.headers)
17
- };
18
- }
19
- }
20
- exports.Configuration = Configuration;
@@ -1,13 +0,0 @@
1
- export declare const JobStatus: {
2
- readonly IN_PROGRESS: "in_progress";
3
- readonly NEW: "new";
4
- readonly DONE: "done";
5
- };
6
- export declare const HttpStatus: {
7
- readonly OK: 200;
8
- readonly BAD_REQUEST: 400;
9
- readonly UNAUTHORIZED: 401;
10
- readonly FORBIDDEN: 403;
11
- readonly NOT_FOUND: 404;
12
- readonly INTERNAL_SERVER_ERROR: 500;
13
- };
package/dist/constants.js DELETED
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpStatus = exports.JobStatus = void 0;
4
- exports.JobStatus = {
5
- IN_PROGRESS: 'in_progress',
6
- NEW: 'new',
7
- DONE: 'done'
8
- };
9
- exports.HttpStatus = {
10
- OK: 200,
11
- BAD_REQUEST: 400,
12
- UNAUTHORIZED: 401,
13
- FORBIDDEN: 403,
14
- NOT_FOUND: 404,
15
- INTERNAL_SERVER_ERROR: 500
16
- };
package/dist/errors.d.ts DELETED
@@ -1,12 +0,0 @@
1
- export declare class WebcrawlerApiError extends Error {
2
- errorCode: string;
3
- errorMessage: string;
4
- statusCode?: number;
5
- constructor(errorCode: string, errorMessage: string, statusCode?: number);
6
- }
7
- export interface ErrorResponse {
8
- error_code?: string;
9
- error_message?: string;
10
- error?: string;
11
- }
12
- export declare function createErrorFromResponse(response: Response, data: ErrorResponse): WebcrawlerApiError;
package/dist/errors.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebcrawlerApiError = void 0;
4
- exports.createErrorFromResponse = createErrorFromResponse;
5
- class WebcrawlerApiError extends Error {
6
- constructor(errorCode, errorMessage, statusCode) {
7
- super(errorMessage);
8
- this.name = 'WebcrawlerApiError';
9
- this.errorCode = errorCode;
10
- this.errorMessage = errorMessage;
11
- this.statusCode = statusCode;
12
- }
13
- }
14
- exports.WebcrawlerApiError = WebcrawlerApiError;
15
- function createErrorFromResponse(response, data) {
16
- const errorCode = data.error_code || 'unknown_error';
17
- const errorMessage = data.error_message || data.error || 'Unknown error';
18
- return new WebcrawlerApiError(errorCode, errorMessage, response.status);
19
- }
package/dist/index.js DELETED
@@ -1,20 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./api"), exports);
18
- __exportStar(require("./model"), exports);
19
- __exportStar(require("./constants"), exports);
20
- __exportStar(require("./errors"), exports);
package/dist/model.d.ts DELETED
@@ -1,62 +0,0 @@
1
- export interface ScrapeRequest {
2
- input: any;
3
- crawler_id: string;
4
- max_retries?: number;
5
- debug?: boolean;
6
- omit_cache?: boolean;
7
- }
8
- export interface JobId {
9
- id: string;
10
- }
11
- export interface ScrapeResponse {
12
- id: string;
13
- url: string;
14
- status: any;
15
- page_status_code: number;
16
- created_at: string;
17
- structured_data: any;
18
- recommended_pull_delay_ms: number;
19
- }
20
- export interface CrawlRequest {
21
- url: string;
22
- items_limit?: number;
23
- scrape_type?: string;
24
- whitelist_regexp?: string;
25
- blacklist_regexp?: string;
26
- allow_subdomains?: boolean;
27
- webhook_url?: string;
28
- }
29
- export interface Job {
30
- id: string;
31
- org_id: string;
32
- url: string;
33
- status: string;
34
- scrape_type: string;
35
- whitelist_regexp: string;
36
- blacklist_regexp: string;
37
- allow_subdomains: boolean;
38
- items_limit: number;
39
- created_at: string;
40
- finished_at: string;
41
- updated_at: string;
42
- webhook_url: string;
43
- recommended_pull_delay_ms: number;
44
- job_items: JobItem[];
45
- }
46
- export interface JobItem {
47
- id: string;
48
- job_id: string;
49
- original_url: string;
50
- page_status_code: number;
51
- markdown_content_url: string;
52
- status: string;
53
- title: string;
54
- last_error: string;
55
- created_at: string;
56
- updated_at: string;
57
- cost: number;
58
- referred_url: string;
59
- raw_content_url?: string;
60
- cleaned_content_url?: string;
61
- getContent(): Promise<string | null>;
62
- }
package/dist/model.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from "./api";
2
- export * from "./model";
3
- export * from "./constants";
4
- export * from "./errors";