webcrawlerapi-js 2.0.3 → 2.0.5

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
@@ -18,6 +18,7 @@ Install WebcrawlerAPI js package:
18
18
  The package is regularly updated with new features and improvements to ensure the best possible scraping experience.
19
19
 
20
20
  ## JavaScript Examples
21
+ Use `crawlRawMarkdown` when you want a crawl to finish and immediately return the combined `/job/{id}/markdown` content.
21
22
 
22
23
  ### Synchronous Job
23
24
  In this mode, the promise will be resolved when all data is ready. Synchronous jobs are perfect for quick scraping tasks where you need immediate results and don't want to handle polling logic. However, for long running jobs we recommend to use async crawling or the webhook.
@@ -176,4 +177,4 @@ Example of a job response. The response structure is carefully designed to provi
176
177
  ],
177
178
  recommended_pull_delay_ms: 5000
178
179
  }
179
- ```
180
+ ```
@@ -11,8 +11,10 @@ export declare class WebcrawlerClient {
11
11
  scrapeAsync(request: ScrapeRequest): Promise<ScrapeId>;
12
12
  getScrape(scrapeId: string): Promise<ScrapeResponse | ScrapeResponseError>;
13
13
  scrape(request: ScrapeRequest, maxPolls?: number): Promise<ScrapeResponse | ScrapeResponseError>;
14
+ crawlRawMarkdown(crawlRequest: CrawlRequest, actions?: Action | Action[]): Promise<string>;
14
15
  crawl(crawlRequest: CrawlRequest, actions?: Action | Action[]): Promise<Job>;
15
16
  crawlAsync(crawlRequest: CrawlRequest, actions?: Action | Action[]): Promise<JobId>;
16
17
  getJob(jobID: string): Promise<Job>;
18
+ getJobMarkdown(jobID: string): Promise<string>;
17
19
  private sendRequest;
18
20
  }
@@ -42,6 +42,7 @@ export interface CrawlRequest {
42
42
  actions?: Action[];
43
43
  respect_robots_txt?: boolean;
44
44
  main_content_only?: boolean;
45
+ max_depth?: number;
45
46
  }
46
47
  export interface Job {
47
48
  id: string;
@@ -53,6 +54,7 @@ export interface Job {
53
54
  blacklist_regexp: string;
54
55
  allow_subdomains: boolean;
55
56
  items_limit: number;
57
+ max_depth?: number;
56
58
  created_at: string;
57
59
  finished_at: string;
58
60
  updated_at: string;
@@ -73,6 +75,7 @@ export interface JobItem {
73
75
  updated_at: string;
74
76
  cost: number;
75
77
  referred_url: string;
78
+ depth?: number;
76
79
  raw_content_url?: string;
77
80
  cleaned_content_url?: string;
78
81
  getContent(): Promise<string | null>;
package/dist/api.js CHANGED
@@ -138,6 +138,20 @@ class WebcrawlerClient {
138
138
  // Return the last known state if maxPolls is reached
139
139
  return result;
140
140
  }
141
+ async crawlRawMarkdown(crawlRequest, actions) {
142
+ const markdownRequest = {
143
+ ...crawlRequest,
144
+ scrape_type: crawlRequest.scrape_type ?? 'markdown'
145
+ };
146
+ const job = await this.crawl(markdownRequest, actions);
147
+ if (job.status !== constants_1.JobStatus.DONE) {
148
+ throw new errors_1.WebcrawlerApiError('job_not_done', `Job finished with status ${job.status}`, 0);
149
+ }
150
+ if (job.scrape_type !== 'markdown') {
151
+ throw new errors_1.WebcrawlerApiError('invalid_scrape_type', 'CrawlRawMarkdown requires scrape_type to be markdown', 0);
152
+ }
153
+ return this.getJobMarkdown(job.id);
154
+ }
141
155
  async crawl(crawlRequest, actions) {
142
156
  const url = `${this.basePath}/${this.apiVersion}/crawl`;
143
157
  const requestBody = {
@@ -206,6 +220,40 @@ class WebcrawlerClient {
206
220
  const job = await this.sendRequest(url, requestOptions);
207
221
  return addGetContentMethod(job);
208
222
  }
223
+ async getJobMarkdown(jobID) {
224
+ const url = `${this.basePath}/${this.apiVersion}/job/${jobID}/markdown`;
225
+ const requestOptions = {
226
+ 'method': 'GET',
227
+ 'headers': {
228
+ 'Content-Type': 'application/json',
229
+ 'Authorization': `Bearer ${this.apiKey}`,
230
+ "User-Agent": "WebcrawlerAPI-NodeJS-Client",
231
+ 'Cache-Control': 'no-cache, no-store, must-revalidate',
232
+ 'Pragma': 'no-cache',
233
+ 'Expires': '0'
234
+ }
235
+ };
236
+ let response;
237
+ try {
238
+ response = await fetch(url, requestOptions);
239
+ }
240
+ catch (e) {
241
+ throw new errors_1.WebcrawlerApiError('network_error', `Failed to send request: ${e}`, 0);
242
+ }
243
+ if (!response.ok) {
244
+ try {
245
+ const errorData = await response.json();
246
+ throw (0, errors_1.createErrorFromResponse)(response, errorData);
247
+ }
248
+ catch (e) {
249
+ if (e instanceof errors_1.WebcrawlerApiError) {
250
+ throw e;
251
+ }
252
+ throw new errors_1.WebcrawlerApiError('unknown_error', `Request failed with status ${response.status} ${response.statusText}`, response.status);
253
+ }
254
+ }
255
+ return response.text();
256
+ }
209
257
  async sendRequest(url, requestOptions) {
210
258
  let response;
211
259
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webcrawlerapi-js",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "JS client for WebcrawlerAPI",
5
5
  "main": "./dist/api.js",
6
6
  "types": "./dist/@types/api.d.ts",
@@ -28,7 +28,6 @@
28
28
  "type": "git",
29
29
  "url": "https://github.com/WebCrawlerAPI/webcrawlerapi-js-sdk"
30
30
  },
31
- "dependencies": {},
32
31
  "scripts": {
33
32
  "build": "tsc --outDir dist/"
34
33
  }