firecrawl 1.29.2 → 3.0.2

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.
Files changed (47) hide show
  1. package/.env.example +4 -2
  2. package/LICENSE +0 -0
  3. package/README.md +85 -78
  4. package/audit-ci.jsonc +4 -0
  5. package/dist/chunk-JFWW4BWA.js +85 -0
  6. package/dist/index.cjs +1005 -42
  7. package/dist/index.d.cts +535 -11
  8. package/dist/index.d.ts +535 -11
  9. package/dist/index.js +994 -32
  10. package/dist/package-KYZ3HXR5.js +4 -0
  11. package/dump.rdb +0 -0
  12. package/jest.config.js +0 -0
  13. package/package.json +6 -5
  14. package/src/__tests__/{v1/e2e_withAuth → e2e/v1}/index.test.ts +1 -0
  15. package/src/__tests__/e2e/v2/batch.test.ts +74 -0
  16. package/src/__tests__/e2e/v2/crawl.test.ts +182 -0
  17. package/src/__tests__/e2e/v2/extract.test.ts +70 -0
  18. package/src/__tests__/e2e/v2/map.test.ts +55 -0
  19. package/src/__tests__/e2e/v2/scrape.test.ts +130 -0
  20. package/src/__tests__/e2e/v2/search.test.ts +247 -0
  21. package/src/__tests__/e2e/v2/usage.test.ts +36 -0
  22. package/src/__tests__/e2e/v2/utils/idmux.ts +58 -0
  23. package/src/__tests__/e2e/v2/watcher.test.ts +96 -0
  24. package/src/__tests__/unit/v1/monitor-job-status-retry.test.ts +154 -0
  25. package/src/__tests__/unit/v2/errorHandler.test.ts +19 -0
  26. package/src/__tests__/unit/v2/scrape.unit.test.ts +11 -0
  27. package/src/__tests__/unit/v2/validation.test.ts +59 -0
  28. package/src/index.backup.ts +2146 -0
  29. package/src/index.ts +27 -2071
  30. package/src/v1/index.ts +2158 -0
  31. package/src/v2/client.ts +281 -0
  32. package/src/v2/methods/batch.ts +131 -0
  33. package/src/v2/methods/crawl.ts +160 -0
  34. package/src/v2/methods/extract.ts +86 -0
  35. package/src/v2/methods/map.ts +37 -0
  36. package/src/v2/methods/scrape.ts +26 -0
  37. package/src/v2/methods/search.ts +69 -0
  38. package/src/v2/methods/usage.ts +39 -0
  39. package/src/v2/types.ts +308 -0
  40. package/src/v2/utils/errorHandler.ts +18 -0
  41. package/src/v2/utils/getVersion.ts +14 -0
  42. package/src/v2/utils/httpClient.ts +99 -0
  43. package/src/v2/utils/validation.ts +50 -0
  44. package/src/v2/watcher.ts +159 -0
  45. package/tsconfig.json +2 -1
  46. package/tsup.config.ts +0 -0
  47. package/dist/package-E7ICGMY6.js +0 -110
package/src/index.ts CHANGED
@@ -1,2083 +1,39 @@
1
- import axios, { type AxiosResponse, type AxiosRequestHeaders, AxiosError } from "axios";
2
- import * as zt from "zod";
3
- import { zodToJsonSchema } from "zod-to-json-schema";
4
- import { TypedEventTarget } from "typescript-event-target";
5
-
6
- /**
7
- * Configuration interface for FirecrawlApp.
8
- * @param apiKey - Optional API key for authentication.
9
- * @param apiUrl - Optional base URL of the API; defaults to 'https://api.firecrawl.dev'.
10
- */
11
- export interface FirecrawlAppConfig {
12
- apiKey?: string | null;
13
- apiUrl?: string | null;
14
- }
15
-
16
1
  /**
17
- * Metadata for a Firecrawl document.
18
- * Includes various optional properties for document metadata.
2
+ * Firecrawl JS/TS SDK unified entrypoint.
3
+ * - v2 by default on the top‑level client
4
+ * - v1 available under `.v1` (feature‑frozen)
5
+ * - Exports: `Firecrawl` (default), `FirecrawlClient` (v2), `FirecrawlAppV1` (v1), and v2 types
19
6
  */
20
- export interface FirecrawlDocumentMetadata {
21
- title?: string;
22
- description?: string;
23
- language?: string;
24
- keywords?: string;
25
- robots?: string;
26
- ogTitle?: string;
27
- ogDescription?: string;
28
- ogUrl?: string;
29
- ogImage?: string;
30
- ogAudio?: string;
31
- ogDeterminer?: string;
32
- ogLocale?: string;
33
- ogLocaleAlternate?: string[];
34
- ogSiteName?: string;
35
- ogVideo?: string;
36
- dctermsCreated?: string;
37
- dcDateCreated?: string;
38
- dcDate?: string;
39
- dctermsType?: string;
40
- dcType?: string;
41
- dctermsAudience?: string;
42
- dctermsSubject?: string;
43
- dcSubject?: string;
44
- dcDescription?: string;
45
- dctermsKeywords?: string;
46
- modifiedTime?: string;
47
- publishedTime?: string;
48
- articleTag?: string;
49
- articleSection?: string;
50
- sourceURL?: string;
51
- statusCode?: number;
52
- error?: string;
53
- proxyUsed?: "basic" | "stealth";
54
- cacheState?: "miss" | "hit";
55
- cachedAt?: string;
56
- [key: string]: any; // Allows for additional metadata properties not explicitly defined.
57
- }
58
7
 
59
- /**
60
- * Document interface for Firecrawl.
61
- * Represents a document retrieved or processed by Firecrawl.
62
- */
63
- export interface FirecrawlDocument<T = any, ActionsSchema extends (ActionsResult | never) = never> {
64
- url?: string;
65
- markdown?: string;
66
- html?: string;
67
- rawHtml?: string;
68
- links?: string[];
69
- extract?: T;
70
- json?: T;
71
- screenshot?: string;
72
- metadata?: FirecrawlDocumentMetadata;
73
- actions: ActionsSchema;
74
- changeTracking?: {
75
- previousScrapeAt: string | null;
76
- changeStatus: "new" | "same" | "changed" | "removed";
77
- visibility: "visible" | "hidden";
78
- diff?: {
79
- text: string;
80
- json: {
81
- files: Array<{
82
- from: string | null;
83
- to: string | null;
84
- chunks: Array<{
85
- content: string;
86
- changes: Array<{
87
- type: string;
88
- normal?: boolean;
89
- ln?: number;
90
- ln1?: number;
91
- ln2?: number;
92
- content: string;
93
- }>;
94
- }>;
95
- }>;
96
- };
97
- };
98
- json?: any;
99
- };
100
- // v1 search only
101
- title?: string;
102
- description?: string;
103
- }
8
+ /** Direct v2 client. */
9
+ export { FirecrawlClient } from "./v2/client";
10
+ /** Public v2 request/response types. */
11
+ export * from "./v2/types";
12
+ /** Legacy v1 client (feature‑frozen). */
13
+ export { default as FirecrawlAppV1 } from "./v1";
104
14
 
105
- /**
106
- * Parameters for scraping operations.
107
- * Defines the options and configurations available for scraping web content.
108
- */
109
- export interface CrawlScrapeOptions {
110
- formats?: ("markdown" | "html" | "rawHtml" | "content" | "links" | "screenshot" | "screenshot@fullPage" | "extract" | "json" | "changeTracking")[];
111
- headers?: Record<string, string>;
112
- includeTags?: string[];
113
- excludeTags?: string[];
114
- onlyMainContent?: boolean;
115
- waitFor?: number;
116
- timeout?: number;
117
- location?: {
118
- country?: string;
119
- languages?: string[];
120
- };
121
- mobile?: boolean;
122
- skipTlsVerification?: boolean;
123
- removeBase64Images?: boolean;
124
- blockAds?: boolean;
125
- proxy?: "basic" | "stealth" | "auto";
126
- storeInCache?: boolean;
127
- maxAge?: number;
128
- parsePDF?: boolean;
129
- }
15
+ import V1 from "./v1";
16
+ import { FirecrawlClient as V2 } from "./v2/client";
17
+ import type { FirecrawlAppConfig } from "./v1";
130
18
 
131
- export type Action = {
132
- type: "wait",
133
- milliseconds?: number,
134
- selector?: string,
135
- } | {
136
- type: "click",
137
- selector: string,
138
- all?: boolean,
139
- } | {
140
- type: "screenshot",
141
- fullPage?: boolean,
142
- quality?: number,
143
- } | {
144
- type: "write",
145
- text: string,
146
- } | {
147
- type: "press",
148
- key: string,
149
- } | {
150
- type: "scroll",
151
- direction?: "up" | "down",
152
- selector?: string,
153
- } | {
154
- type: "scrape",
155
- } | {
156
- type: "executeJavascript",
157
- script: string,
158
- };
19
+ /** Unified client: extends v2 and adds `.v1` for backward compatibility. */
20
+ export class Firecrawl extends V2 {
21
+ /** Feature‑frozen v1 client (lazy). */
22
+ private _v1?: V1;
23
+ private _v1Opts: FirecrawlAppConfig;
159
24
 
160
- export interface ScrapeParams<LLMSchema extends zt.ZodSchema = any, ActionsSchema extends (Action[] | undefined) = undefined> extends CrawlScrapeOptions {
161
- extract?: {
162
- prompt?: string;
163
- schema?: LLMSchema;
164
- systemPrompt?: string;
165
- };
166
- jsonOptions?:{
167
- prompt?: string;
168
- schema?: LLMSchema;
169
- systemPrompt?: string;
25
+ /** @param opts API credentials and base URL. */
26
+ constructor(opts: FirecrawlAppConfig = {}) {
27
+ super(opts as any);
28
+ this._v1Opts = opts;
170
29
  }
171
- changeTrackingOptions?: {
172
- prompt?: string;
173
- schema?: any;
174
- modes?: ("json" | "git-diff")[];
175
- tag?: string | null;
176
- }
177
- actions?: ActionsSchema;
178
- agent?: AgentOptions;
179
- zeroDataRetention?: boolean;
180
- }
181
-
182
- export interface ActionsResult {
183
- screenshots: string[];
184
- scrapes: ({
185
- url: string;
186
- html: string;
187
- })[];
188
- javascriptReturns: {
189
- type: string;
190
- value: unknown
191
- }[];
192
- }
193
-
194
- /**
195
- * Response interface for scraping operations.
196
- * Defines the structure of the response received after a scraping operation.
197
- */
198
- export interface ScrapeResponse<LLMResult = any, ActionsSchema extends (ActionsResult | never) = never> extends FirecrawlDocument<LLMResult, ActionsSchema> {
199
- success: true;
200
- warning?: string;
201
- error?: string;
202
- }
203
-
204
- /**
205
- * Parameters for crawling operations.
206
- * Includes options for both scraping and mapping during a crawl.
207
- */
208
- export interface CrawlParams {
209
- includePaths?: string[];
210
- excludePaths?: string[];
211
- maxDepth?: number;
212
- maxDiscoveryDepth?: number;
213
- limit?: number;
214
- allowBackwardLinks?: boolean;
215
- crawlEntireDomain?: boolean;
216
- allowExternalLinks?: boolean;
217
- ignoreSitemap?: boolean;
218
- scrapeOptions?: CrawlScrapeOptions;
219
- webhook?: string | {
220
- url: string;
221
- headers?: Record<string, string>;
222
- metadata?: Record<string, string>;
223
- events?: ["completed", "failed", "page", "started"][number][];
224
- };
225
- deduplicateSimilarURLs?: boolean;
226
- ignoreQueryParameters?: boolean;
227
- regexOnFullURL?: boolean;
228
- /**
229
- * Delay in seconds between scrapes. This helps respect website rate limits.
230
- * If not provided, the crawler may use the robots.txt crawl delay if available.
231
- */
232
- delay?: number;
233
- allowSubdomains?: boolean;
234
- maxConcurrency?: number;
235
- zeroDataRetention?: boolean;
236
- }
237
-
238
- /**
239
- * Response interface for crawling operations.
240
- * Defines the structure of the response received after initiating a crawl.
241
- */
242
- export interface CrawlResponse {
243
- id?: string;
244
- url?: string;
245
- success: true;
246
- error?: string;
247
- }
248
-
249
- /**
250
- * Response interface for batch scrape operations.
251
- * Defines the structure of the response received after initiating a crawl.
252
- */
253
- export interface BatchScrapeResponse {
254
- id?: string;
255
- url?: string;
256
- success: true;
257
- error?: string;
258
- invalidURLs?: string[];
259
- }
260
-
261
- /**
262
- * Response interface for job status checks.
263
- * Provides detailed status of a crawl job including progress and results.
264
- */
265
- export interface CrawlStatusResponse {
266
- success: true;
267
- status: "scraping" | "completed" | "failed" | "cancelled";
268
- completed: number;
269
- total: number;
270
- creditsUsed: number;
271
- expiresAt: Date;
272
- next?: string;
273
- data: FirecrawlDocument<undefined>[];
274
- };
275
-
276
- /**
277
- * Response interface for batch scrape job status checks.
278
- * Provides detailed status of a batch scrape job including progress and results.
279
- */
280
- export interface BatchScrapeStatusResponse {
281
- success: true;
282
- status: "scraping" | "completed" | "failed" | "cancelled";
283
- completed: number;
284
- total: number;
285
- creditsUsed: number;
286
- expiresAt: Date;
287
- next?: string;
288
- data: FirecrawlDocument<undefined>[];
289
- };
290
-
291
- /**
292
- * Parameters for mapping operations.
293
- * Defines options for mapping URLs during a crawl.
294
- */
295
- export interface MapParams {
296
- search?: string;
297
- ignoreSitemap?: boolean;
298
- includeSubdomains?: boolean;
299
- sitemapOnly?: boolean;
300
- limit?: number;
301
- timeout?: number;
302
- useIndex?: boolean;
303
- }
304
-
305
- /**
306
- * Response interface for mapping operations.
307
- * Defines the structure of the response received after a mapping operation.
308
- */
309
- export interface MapResponse {
310
- success: true;
311
- links?: string[];
312
- error?: string;
313
- }
314
-
315
- /**
316
- * Parameters for extracting information from URLs.
317
- * Defines options for extracting information from URLs.
318
- */
319
- export interface AgentOptions {
320
- model?: string;
321
- prompt?: string;
322
- sessionId?: string;
323
- }
324
-
325
- /**
326
- * Parameters for extracting information from URLs.
327
- * Defines options for extracting information from URLs.
328
- */
329
- export interface AgentOptionsExtract {
330
- model?: string;
331
- sessionId?: string;
332
- }
333
-
334
- export interface ExtractParams<LLMSchema extends zt.ZodSchema = any> {
335
- prompt?: string;
336
- schema?: LLMSchema | object;
337
- systemPrompt?: string;
338
- allowExternalLinks?: boolean;
339
- enableWebSearch?: boolean;
340
- includeSubdomains?: boolean;
341
- origin?: string;
342
- showSources?: boolean;
343
- scrapeOptions?: CrawlScrapeOptions;
344
- agent?: AgentOptionsExtract;
345
- }
346
-
347
- /**
348
- * Response interface for extracting information from URLs.
349
- * Defines the structure of the response received after extracting information from URLs.
350
- */
351
- export interface ExtractResponse<LLMSchema extends zt.ZodSchema = any> {
352
- success: boolean;
353
- data: LLMSchema;
354
- error?: string;
355
- warning?: string;
356
- sources?: string[];
357
- }
358
-
359
- /**
360
- * Error response interface.
361
- * Defines the structure of the response received when an error occurs.
362
- */
363
- export interface ErrorResponse {
364
- success: false;
365
- error: string;
366
- }
367
30
 
368
- /**
369
- * Custom error class for Firecrawl.
370
- * Extends the built-in Error class to include a status code.
371
- */
372
- export class FirecrawlError extends Error {
373
- statusCode: number;
374
- details?: any;
375
- constructor(message: string, statusCode: number, details?: any) {
376
- super(message);
377
- this.statusCode = statusCode;
378
- this.details = details;
31
+ /** Access the legacy v1 client (instantiated on first access). */
32
+ get v1(): V1 {
33
+ if (!this._v1) this._v1 = new V1(this._v1Opts);
34
+ return this._v1;
379
35
  }
380
36
  }
381
37
 
382
- /**
383
- * Parameters for search operations.
384
- * Defines options for searching and scraping search results.
385
- */
386
- export interface SearchParams {
387
- limit?: number;
388
- tbs?: string;
389
- filter?: string;
390
- lang?: string;
391
- country?: string;
392
- location?: string;
393
- origin?: string;
394
- timeout?: number;
395
- scrapeOptions?: ScrapeParams;
396
- }
397
-
398
- /**
399
- * Response interface for search operations.
400
- * Defines the structure of the response received after a search operation.
401
- */
402
- export interface SearchResponse {
403
- success: boolean;
404
- data: FirecrawlDocument<undefined>[];
405
- warning?: string;
406
- error?: string;
407
- }
408
-
409
- /**
410
- * Response interface for crawl/batch scrape error monitoring.
411
- */
412
- export interface CrawlErrorsResponse {
413
- /**
414
- * Scrapes that errored out + error details
415
- */
416
- errors: {
417
- id: string,
418
- timestamp?: string,
419
- url: string,
420
- error: string,
421
- }[];
422
-
423
- /**
424
- * URLs blocked by robots.txt
425
- */
426
- robotsBlocked: string[];
427
- };
428
-
429
- /**
430
- * Parameters for deep research operations.
431
- * Defines options for conducting deep research on a query.
432
- */
433
- export interface DeepResearchParams<LLMSchema extends zt.ZodSchema = any> {
434
- /**
435
- * Maximum depth of research iterations (1-10)
436
- * @default 7
437
- */
438
- maxDepth?: number;
439
- /**
440
- * Time limit in seconds (30-300)
441
- * @default 270
442
- */
443
- timeLimit?: number;
444
- /**
445
- * Maximum number of URLs to analyze (1-1000)
446
- * @default 20
447
- */
448
- maxUrls?: number;
449
- /**
450
- * The prompt to use for the final analysis
451
- */
452
- analysisPrompt?: string;
453
- /**
454
- * The system prompt to use for the research agent
455
- */
456
- systemPrompt?: string;
457
- /**
458
- * The formats to use for the final analysis
459
- */
460
- formats?: ("markdown" | "json")[];
461
- /**
462
- * The JSON options to use for the final analysis
463
- */
464
- jsonOptions?:{
465
- prompt?: string;
466
- schema?: LLMSchema;
467
- systemPrompt?: string;
468
- };
469
- /**
470
- * Experimental flag for streaming steps
471
- */
472
- // __experimental_streamSteps?: boolean;
473
- }
474
-
475
- /**
476
- * Response interface for deep research operations.
477
- */
478
- export interface DeepResearchResponse {
479
- success: boolean;
480
- id: string;
481
- }
482
-
483
- /**
484
- * Status response interface for deep research operations.
485
- */
486
- export interface DeepResearchStatusResponse {
487
- success: boolean;
488
- data: {
489
- finalAnalysis: string;
490
- activities: Array<{
491
- type: string;
492
- status: string;
493
- message: string;
494
- timestamp: string;
495
- depth: number;
496
- }>;
497
- sources: Array<{
498
- url: string;
499
- title: string;
500
- description: string;
501
- }>;
502
- };
503
- status: "processing" | "completed" | "failed";
504
- error?: string;
505
- expiresAt: string;
506
- currentDepth: number;
507
- maxDepth: number;
508
- activities: Array<{
509
- type: string;
510
- status: string;
511
- message: string;
512
- timestamp: string;
513
- depth: number;
514
- }>;
515
- sources: Array<{
516
- url: string;
517
- title: string;
518
- description: string;
519
- }>;
520
- summaries: string[];
521
- }
522
-
523
- /**
524
- * Parameters for LLMs.txt generation operations.
525
- */
526
- export interface GenerateLLMsTextParams {
527
- /**
528
- * Maximum number of URLs to process (1-100)
529
- * @default 10
530
- */
531
- maxUrls?: number;
532
- /**
533
- * Whether to show the full LLMs-full.txt in the response
534
- * @default false
535
- */
536
- showFullText?: boolean;
537
- /**
538
- * Whether to use cached content if available
539
- * @default true
540
- */
541
- cache?: boolean;
542
- /**
543
- * Experimental flag for streaming
544
- */
545
- __experimental_stream?: boolean;
546
- }
547
-
548
- /**
549
- * Response interface for LLMs.txt generation operations.
550
- */
551
- export interface GenerateLLMsTextResponse {
552
- success: boolean;
553
- id: string;
554
- }
555
-
556
- /**
557
- * Status response interface for LLMs.txt generation operations.
558
- */
559
- export interface GenerateLLMsTextStatusResponse {
560
- success: boolean;
561
- data: {
562
- llmstxt: string;
563
- llmsfulltxt?: string;
564
- };
565
- status: "processing" | "completed" | "failed";
566
- error?: string;
567
- expiresAt: string;
568
- }
569
-
570
- /**
571
- * Main class for interacting with the Firecrawl API.
572
- * Provides methods for scraping, searching, crawling, and mapping web content.
573
- */
574
- export default class FirecrawlApp {
575
- public apiKey: string;
576
- public apiUrl: string;
577
- public version: string = "1.25.1";
578
-
579
- private isCloudService(url: string): boolean {
580
- return url.includes('api.firecrawl.dev');
581
- }
582
-
583
- private async getVersion(): Promise<string> {
584
- try {
585
- const packageJson = await import('../package.json', { assert: { type: 'json' } });
586
- return packageJson.default.version;
587
- } catch (error) {
588
- console.error("Error getting version:", error);
589
- return "1.25.1";
590
- }
591
- }
592
-
593
- private async init() {
594
- this.version = await this.getVersion();
595
- }
596
-
597
- /**
598
- * Initializes a new instance of the FirecrawlApp class.
599
- * @param config - Configuration options for the FirecrawlApp instance.
600
- */
601
- constructor({ apiKey = null, apiUrl = null }: FirecrawlAppConfig) {
602
- const baseUrl = apiUrl || "https://api.firecrawl.dev";
603
-
604
- if (this.isCloudService(baseUrl) && typeof apiKey !== "string") {
605
- throw new FirecrawlError("No API key provided", 401);
606
- }
607
-
608
- this.apiKey = apiKey || '';
609
- this.apiUrl = baseUrl;
610
- this.init();
611
- }
612
-
613
- /**
614
- * Scrapes a URL using the Firecrawl API.
615
- * @param url - The URL to scrape.
616
- * @param params - Additional parameters for the scrape request.
617
- * @returns The response from the scrape operation.
618
- */
619
- async scrapeUrl<T extends zt.ZodSchema, ActionsSchema extends (Action[] | undefined) = undefined>(
620
- url: string,
621
- params?: ScrapeParams<T, ActionsSchema>
622
- ): Promise<ScrapeResponse<zt.infer<T>, ActionsSchema extends Action[] ? ActionsResult : never> | ErrorResponse> {
623
- const headers: AxiosRequestHeaders = {
624
- "Content-Type": "application/json",
625
- Authorization: `Bearer ${this.apiKey}`,
626
- } as AxiosRequestHeaders;
627
- let jsonData: any = { url, ...params, origin: `js-sdk@${this.version}` };
628
- if (jsonData?.extract?.schema) {
629
- let schema = jsonData.extract.schema;
630
-
631
- // Try parsing the schema as a Zod schema
632
- try {
633
- schema = zodToJsonSchema(schema);
634
- } catch (error) {
635
-
636
- }
637
- jsonData = {
638
- ...jsonData,
639
- extract: {
640
- ...jsonData.extract,
641
- schema: schema,
642
- },
643
- };
644
- }
645
-
646
- if (jsonData?.jsonOptions?.schema) {
647
- let schema = jsonData.jsonOptions.schema;
648
- // Try parsing the schema as a Zod schema
649
- try {
650
- schema = zodToJsonSchema(schema);
651
- } catch (error) {
652
-
653
- }
654
- jsonData = {
655
- ...jsonData,
656
- jsonOptions: {
657
- ...jsonData.jsonOptions,
658
- schema: schema,
659
- },
660
- };
661
- }
662
- try {
663
- const response: AxiosResponse = await axios.post(
664
- this.apiUrl + `/v1/scrape`,
665
- jsonData,
666
- { headers, timeout: params?.timeout !== undefined ? (params.timeout + 5000) : undefined },
667
- );
668
- if (response.status === 200) {
669
- const responseData = response.data;
670
- if (responseData.success) {
671
- return {
672
- success: true,
673
- warning: responseData.warning,
674
- error: responseData.error,
675
- ...responseData.data
676
- };
677
- } else {
678
- throw new FirecrawlError(`Failed to scrape URL. Error: ${responseData.error}`, response.status);
679
- }
680
- } else {
681
- this.handleError(response, "scrape URL");
682
- }
683
- } catch (error: any) {
684
- this.handleError(error.response, "scrape URL");
685
- }
686
- return { success: false, error: "Internal server error." };
687
- }
688
-
689
- /**
690
- * Searches using the Firecrawl API and optionally scrapes the results.
691
- * @param query - The search query string.
692
- * @param params - Optional parameters for the search request.
693
- * @returns The response from the search operation.
694
- */
695
- async search(query: string, params?: SearchParams | Record<string, any>): Promise<SearchResponse> {
696
- const headers: AxiosRequestHeaders = {
697
- "Content-Type": "application/json",
698
- Authorization: `Bearer ${this.apiKey}`,
699
- } as AxiosRequestHeaders;
700
-
701
- let jsonData: any = {
702
- query,
703
- limit: params?.limit ?? 5,
704
- tbs: params?.tbs,
705
- filter: params?.filter,
706
- lang: params?.lang ?? "en",
707
- country: params?.country ?? "us",
708
- location: params?.location,
709
- origin: `js-sdk@${this.version}`,
710
- timeout: params?.timeout ?? 60000,
711
- scrapeOptions: params?.scrapeOptions ?? { formats: [] },
712
- };
713
-
714
- if (jsonData?.scrapeOptions?.extract?.schema) {
715
- let schema = jsonData.scrapeOptions.extract.schema;
716
-
717
- // Try parsing the schema as a Zod schema
718
- try {
719
- schema = zodToJsonSchema(schema);
720
- } catch (error) {
721
-
722
- }
723
- jsonData = {
724
- ...jsonData,
725
- scrapeOptions: {
726
- ...jsonData.scrapeOptions,
727
- extract: {
728
- ...jsonData.scrapeOptions.extract,
729
- schema: schema,
730
- },
731
- },
732
- };
733
- }
734
-
735
- try {
736
- const response: AxiosResponse = await this.postRequest(
737
- this.apiUrl + `/v1/search`,
738
- jsonData,
739
- headers
740
- );
741
-
742
- if (response.status === 200) {
743
- const responseData = response.data;
744
- if (responseData.success) {
745
- return {
746
- success: true,
747
- data: responseData.data as FirecrawlDocument<any>[],
748
- warning: responseData.warning,
749
- };
750
- } else {
751
- throw new FirecrawlError(`Failed to search. Error: ${responseData.error}`, response.status);
752
- }
753
- } else {
754
- this.handleError(response, "search");
755
- }
756
- } catch (error: any) {
757
- if (error.response?.data?.error) {
758
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
759
- } else {
760
- throw new FirecrawlError(error.message, 500);
761
- }
762
- }
763
- return { success: false, error: "Internal server error.", data: [] };
764
- }
765
-
766
- /**
767
- * Initiates a crawl job for a URL using the Firecrawl API.
768
- * @param url - The URL to crawl.
769
- * @param params - Additional parameters for the crawl request.
770
- * @param pollInterval - Time in seconds for job status checks.
771
- * @param idempotencyKey - Optional idempotency key for the request.
772
- * @returns The response from the crawl operation.
773
- */
774
- async crawlUrl(
775
- url: string,
776
- params?: CrawlParams,
777
- pollInterval: number = 2,
778
- idempotencyKey?: string
779
- ): Promise<CrawlStatusResponse | ErrorResponse> {
780
- const headers = this.prepareHeaders(idempotencyKey);
781
- let jsonData: any = { url, ...params, origin: `js-sdk@${this.version}` };
782
- try {
783
- const response: AxiosResponse = await this.postRequest(
784
- this.apiUrl + `/v1/crawl`,
785
- jsonData,
786
- headers
787
- );
788
- if (response.status === 200) {
789
- const id: string = response.data.id;
790
- return this.monitorJobStatus(id, headers, pollInterval);
791
- } else {
792
- this.handleError(response, "start crawl job");
793
- }
794
- } catch (error: any) {
795
- if (error.response?.data?.error) {
796
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
797
- } else {
798
- throw new FirecrawlError(error.message, 500);
799
- }
800
- }
801
- return { success: false, error: "Internal server error." };
802
- }
803
-
804
- async asyncCrawlUrl(
805
- url: string,
806
- params?: CrawlParams,
807
- idempotencyKey?: string
808
- ): Promise<CrawlResponse | ErrorResponse> {
809
- const headers = this.prepareHeaders(idempotencyKey);
810
- let jsonData: any = { url, ...params, origin: `js-sdk@${this.version}` };
811
- try {
812
- const response: AxiosResponse = await this.postRequest(
813
- this.apiUrl + `/v1/crawl`,
814
- jsonData,
815
- headers
816
- );
817
- if (response.status === 200) {
818
- return response.data;
819
- } else {
820
- this.handleError(response, "start crawl job");
821
- }
822
- } catch (error: any) {
823
- if (error.response?.data?.error) {
824
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
825
- } else {
826
- throw new FirecrawlError(error.message, 500);
827
- }
828
- }
829
- return { success: false, error: "Internal server error." };
830
- }
831
-
832
- /**
833
- * Checks the status of a crawl job using the Firecrawl API.
834
- * @param id - The ID of the crawl operation.
835
- * @param getAllData - Paginate through all the pages of documents, returning the full list of all documents. (default: `false`)
836
- * @param nextURL - The `next` URL from the previous crawl status. Only required if you're not manually increasing `skip`. Only used when `getAllData = false`.
837
- * @param skip - How many entries to skip to paginate. Only required if you're not providing `nextURL`. Only used when `getAllData = false`.
838
- * @param limit - How many entries to return. Only used when `getAllData = false`.
839
- * @returns The response containing the job status.
840
- */
841
- async checkCrawlStatus(id?: string, getAllData = false, nextURL?: string, skip?: number, limit?: number): Promise<CrawlStatusResponse | ErrorResponse> {
842
- if (!id) {
843
- throw new FirecrawlError("No crawl ID provided", 400);
844
- }
38
+ export default Firecrawl;
845
39
 
846
- const headers: AxiosRequestHeaders = this.prepareHeaders();
847
- const targetURL = new URL(nextURL ?? `${this.apiUrl}/v1/crawl/${id}`);
848
- if (skip !== undefined) {
849
- targetURL.searchParams.set("skip", skip.toString());
850
- }
851
- if (limit !== undefined) {
852
- targetURL.searchParams.set("limit", limit.toString());
853
- }
854
-
855
- try {
856
- const response: AxiosResponse = await this.getRequest(
857
- targetURL.href,
858
- headers
859
- );
860
- if (response.status === 200) {
861
- let allData = response.data.data;
862
- if (getAllData && response.data.status === "completed") {
863
- let statusData = response.data
864
- if ("data" in statusData) {
865
- let data = statusData.data;
866
- while (typeof statusData === 'object' && 'next' in statusData) {
867
- if (data.length === 0) {
868
- break
869
- }
870
- statusData = (await this.getRequest(statusData.next, headers)).data;
871
- data = data.concat(statusData.data);
872
- }
873
- allData = data;
874
- }
875
- }
876
-
877
- let resp: CrawlStatusResponse | ErrorResponse = {
878
- success: response.data.success,
879
- status: response.data.status,
880
- total: response.data.total,
881
- completed: response.data.completed,
882
- creditsUsed: response.data.creditsUsed,
883
- next: getAllData ? undefined : response.data.next,
884
- expiresAt: new Date(response.data.expiresAt),
885
- data: allData
886
- }
887
-
888
- if (!response.data.success && response.data.error) {
889
- resp = {
890
- ...resp,
891
- success: false,
892
- error: response.data.error
893
- } as ErrorResponse;
894
- }
895
-
896
- if (response.data.next) {
897
- (resp as CrawlStatusResponse).next = response.data.next;
898
- }
899
-
900
- return resp;
901
- } else {
902
- this.handleError(response, "check crawl status");
903
- }
904
- } catch (error: any) {
905
- throw new FirecrawlError(error.message, 500);
906
- }
907
- return { success: false, error: "Internal server error." };
908
- }
909
-
910
- /**
911
- * Returns information about crawl errors.
912
- * @param id - The ID of the crawl operation.
913
- * @returns Information about crawl errors.
914
- */
915
- async checkCrawlErrors(id: string): Promise<CrawlErrorsResponse | ErrorResponse> {
916
- const headers = this.prepareHeaders();
917
- try {
918
- const response: AxiosResponse = await this.deleteRequest(
919
- `${this.apiUrl}/v1/crawl/${id}/errors`,
920
- headers
921
- );
922
- if (response.status === 200) {
923
- return response.data;
924
- } else {
925
- this.handleError(response, "check crawl errors");
926
- }
927
- } catch (error: any) {
928
- throw new FirecrawlError(error.message, 500);
929
- }
930
- return { success: false, error: "Internal server error." };
931
- }
932
-
933
- /**
934
- * Cancels a crawl job using the Firecrawl API.
935
- * @param id - The ID of the crawl operation.
936
- * @returns The response from the cancel crawl operation.
937
- */
938
- async cancelCrawl(id: string): Promise<ErrorResponse> {
939
- const headers = this.prepareHeaders();
940
- try {
941
- const response: AxiosResponse = await this.deleteRequest(
942
- `${this.apiUrl}/v1/crawl/${id}`,
943
- headers
944
- );
945
- if (response.status === 200) {
946
- return response.data;
947
- } else {
948
- this.handleError(response, "cancel crawl job");
949
- }
950
- } catch (error: any) {
951
- throw new FirecrawlError(error.message, 500);
952
- }
953
- return { success: false, error: "Internal server error." };
954
- }
955
-
956
- /**
957
- * Initiates a crawl job and returns a CrawlWatcher to monitor the job via WebSocket.
958
- * @param url - The URL to crawl.
959
- * @param params - Additional parameters for the crawl request.
960
- * @param idempotencyKey - Optional idempotency key for the request.
961
- * @returns A CrawlWatcher instance to monitor the crawl job.
962
- */
963
- async crawlUrlAndWatch(
964
- url: string,
965
- params?: CrawlParams,
966
- idempotencyKey?: string,
967
- ) {
968
- const crawl = await this.asyncCrawlUrl(url, params, idempotencyKey);
969
-
970
- if (crawl.success && crawl.id) {
971
- const id = crawl.id;
972
- return new CrawlWatcher(id, this);
973
- }
974
-
975
- throw new FirecrawlError("Crawl job failed to start", 400);
976
- }
977
-
978
- /**
979
- * Maps a URL using the Firecrawl API.
980
- * @param url - The URL to map.
981
- * @param params - Additional parameters for the map request.
982
- * @returns The response from the map operation.
983
- */
984
- async mapUrl(url: string, params?: MapParams): Promise<MapResponse | ErrorResponse> {
985
- const headers = this.prepareHeaders();
986
- let jsonData: any = { url, ...params, origin: `js-sdk@${this.version}` };
987
-
988
- try {
989
- const response: AxiosResponse = await this.postRequest(
990
- this.apiUrl + `/v1/map`,
991
- jsonData,
992
- headers
993
- );
994
- if (response.status === 200) {
995
- return response.data as MapResponse;
996
- } else {
997
- this.handleError(response, "map");
998
- }
999
- } catch (error: any) {
1000
- throw new FirecrawlError(error.message, 500);
1001
- }
1002
- return { success: false, error: "Internal server error." };
1003
- }
1004
-
1005
- /**
1006
- * Initiates a batch scrape job for multiple URLs using the Firecrawl API.
1007
- * @param url - The URLs to scrape.
1008
- * @param params - Additional parameters for the scrape request.
1009
- * @param pollInterval - Time in seconds for job status checks.
1010
- * @param idempotencyKey - Optional idempotency key for the request.
1011
- * @param webhook - Optional webhook for the batch scrape.
1012
- * @param ignoreInvalidURLs - Optional flag to ignore invalid URLs.
1013
- * @returns The response from the crawl operation.
1014
- */
1015
- async batchScrapeUrls(
1016
- urls: string[],
1017
- params?: ScrapeParams,
1018
- pollInterval: number = 2,
1019
- idempotencyKey?: string,
1020
- webhook?: CrawlParams["webhook"],
1021
- ignoreInvalidURLs?: boolean,
1022
- maxConcurrency?: number,
1023
- ): Promise<BatchScrapeStatusResponse | ErrorResponse> {
1024
- const headers = this.prepareHeaders(idempotencyKey);
1025
- let jsonData: any = { urls, webhook, ignoreInvalidURLs, maxConcurrency, ...params, origin: `js-sdk@${this.version}` };
1026
- if (jsonData?.extract?.schema) {
1027
- let schema = jsonData.extract.schema;
1028
-
1029
- // Try parsing the schema as a Zod schema
1030
- try {
1031
- schema = zodToJsonSchema(schema);
1032
- } catch (error) {
1033
-
1034
- }
1035
- jsonData = {
1036
- ...jsonData,
1037
- extract: {
1038
- ...jsonData.extract,
1039
- schema: schema,
1040
- },
1041
- };
1042
- }
1043
- if (jsonData?.jsonOptions?.schema) {
1044
- let schema = jsonData.jsonOptions.schema;
1045
-
1046
- // Try parsing the schema as a Zod schema
1047
- try {
1048
- schema = zodToJsonSchema(schema);
1049
- } catch (error) {
1050
-
1051
- }
1052
- jsonData = {
1053
- ...jsonData,
1054
- jsonOptions: {
1055
- ...jsonData.jsonOptions,
1056
- schema: schema,
1057
- },
1058
- };
1059
- }
1060
- try {
1061
- const response: AxiosResponse = await this.postRequest(
1062
- this.apiUrl + `/v1/batch/scrape`,
1063
- jsonData,
1064
- headers
1065
- );
1066
- if (response.status === 200) {
1067
- const id: string = response.data.id;
1068
- return this.monitorJobStatus(id, headers, pollInterval);
1069
- } else {
1070
- this.handleError(response, "start batch scrape job");
1071
- }
1072
- } catch (error: any) {
1073
- if (error.response?.data?.error) {
1074
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
1075
- } else {
1076
- throw new FirecrawlError(error.message, 500);
1077
- }
1078
- }
1079
- return { success: false, error: "Internal server error." };
1080
- }
1081
-
1082
- async asyncBatchScrapeUrls(
1083
- urls: string[],
1084
- params?: ScrapeParams,
1085
- idempotencyKey?: string,
1086
- webhook?: CrawlParams["webhook"],
1087
- ignoreInvalidURLs?: boolean,
1088
- ): Promise<BatchScrapeResponse | ErrorResponse> {
1089
- const headers = this.prepareHeaders(idempotencyKey);
1090
- let jsonData: any = { urls, webhook, ignoreInvalidURLs, ...params, origin: `js-sdk@${this.version}` };
1091
- try {
1092
- const response: AxiosResponse = await this.postRequest(
1093
- this.apiUrl + `/v1/batch/scrape`,
1094
- jsonData,
1095
- headers
1096
- );
1097
- if (response.status === 200) {
1098
- return response.data;
1099
- } else {
1100
- this.handleError(response, "start batch scrape job");
1101
- }
1102
- } catch (error: any) {
1103
- if (error.response?.data?.error) {
1104
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
1105
- } else {
1106
- throw new FirecrawlError(error.message, 500);
1107
- }
1108
- }
1109
- return { success: false, error: "Internal server error." };
1110
- }
1111
-
1112
- /**
1113
- * Initiates a batch scrape job and returns a CrawlWatcher to monitor the job via WebSocket.
1114
- * @param urls - The URL to scrape.
1115
- * @param params - Additional parameters for the scrape request.
1116
- * @param idempotencyKey - Optional idempotency key for the request.
1117
- * @returns A CrawlWatcher instance to monitor the crawl job.
1118
- */
1119
- async batchScrapeUrlsAndWatch(
1120
- urls: string[],
1121
- params?: ScrapeParams,
1122
- idempotencyKey?: string,
1123
- webhook?: CrawlParams["webhook"],
1124
- ignoreInvalidURLs?: boolean,
1125
- ) {
1126
- const crawl = await this.asyncBatchScrapeUrls(urls, params, idempotencyKey, webhook, ignoreInvalidURLs);
1127
-
1128
- if (crawl.success && crawl.id) {
1129
- const id = crawl.id;
1130
- return new CrawlWatcher(id, this);
1131
- }
1132
-
1133
- throw new FirecrawlError("Batch scrape job failed to start", 400);
1134
- }
1135
-
1136
- /**
1137
- * Checks the status of a batch scrape job using the Firecrawl API.
1138
- * @param id - The ID of the batch scrape operation.
1139
- * @param getAllData - Paginate through all the pages of documents, returning the full list of all documents. (default: `false`)
1140
- * @param nextURL - The `next` URL from the previous batch scrape status. Only required if you're not manually increasing `skip`. Only used when `getAllData = false`.
1141
- * @param skip - How many entries to skip to paginate. Only used when `getAllData = false`.
1142
- * @param limit - How many entries to return. Only used when `getAllData = false`.
1143
- * @returns The response containing the job status.
1144
- */
1145
- async checkBatchScrapeStatus(id?: string, getAllData = false, nextURL?: string, skip?: number, limit?: number): Promise<BatchScrapeStatusResponse | ErrorResponse> {
1146
- if (!id) {
1147
- throw new FirecrawlError("No batch scrape ID provided", 400);
1148
- }
1149
-
1150
- const headers: AxiosRequestHeaders = this.prepareHeaders();
1151
- const targetURL = new URL(nextURL ?? `${this.apiUrl}/v1/batch/scrape/${id}`);
1152
- if (skip !== undefined) {
1153
- targetURL.searchParams.set("skip", skip.toString());
1154
- }
1155
- if (limit !== undefined) {
1156
- targetURL.searchParams.set("limit", limit.toString());
1157
- }
1158
-
1159
- try {
1160
- const response: AxiosResponse = await this.getRequest(
1161
- targetURL.href,
1162
- headers
1163
- );
1164
- if (response.status === 200) {
1165
- let allData = response.data.data;
1166
- if (getAllData && response.data.status === "completed") {
1167
- let statusData = response.data
1168
- if ("data" in statusData) {
1169
- let data = statusData.data;
1170
- while (typeof statusData === 'object' && 'next' in statusData) {
1171
- if (data.length === 0) {
1172
- break
1173
- }
1174
- statusData = (await this.getRequest(statusData.next, headers)).data;
1175
- data = data.concat(statusData.data);
1176
- }
1177
- allData = data;
1178
- }
1179
- }
1180
-
1181
- let resp: BatchScrapeStatusResponse | ErrorResponse = {
1182
- success: response.data.success,
1183
- status: response.data.status,
1184
- total: response.data.total,
1185
- completed: response.data.completed,
1186
- creditsUsed: response.data.creditsUsed,
1187
- next: getAllData ? undefined : response.data.next,
1188
- expiresAt: new Date(response.data.expiresAt),
1189
- data: allData
1190
- }
1191
-
1192
- if (!response.data.success && response.data.error) {
1193
- resp = {
1194
- ...resp,
1195
- success: false,
1196
- error: response.data.error
1197
- } as ErrorResponse;
1198
- }
1199
-
1200
- if (response.data.next) {
1201
- (resp as BatchScrapeStatusResponse).next = response.data.next;
1202
- }
1203
-
1204
- return resp;
1205
- } else {
1206
- this.handleError(response, "check batch scrape status");
1207
- }
1208
- } catch (error: any) {
1209
- throw new FirecrawlError(error.message, 500);
1210
- }
1211
- return { success: false, error: "Internal server error." };
1212
- }
1213
-
1214
- /**
1215
- * Returns information about batch scrape errors.
1216
- * @param id - The ID of the batch scrape operation.
1217
- * @returns Information about batch scrape errors.
1218
- */
1219
- async checkBatchScrapeErrors(id: string): Promise<CrawlErrorsResponse | ErrorResponse> {
1220
- const headers = this.prepareHeaders();
1221
- try {
1222
- const response: AxiosResponse = await this.deleteRequest(
1223
- `${this.apiUrl}/v1/batch/scrape/${id}/errors`,
1224
- headers
1225
- );
1226
- if (response.status === 200) {
1227
- return response.data;
1228
- } else {
1229
- this.handleError(response, "check batch scrape errors");
1230
- }
1231
- } catch (error: any) {
1232
- throw new FirecrawlError(error.message, 500);
1233
- }
1234
- return { success: false, error: "Internal server error." };
1235
- }
1236
-
1237
- /**
1238
- * Extracts information from URLs using the Firecrawl API.
1239
- * Currently in Beta. Expect breaking changes on future minor versions.
1240
- * @param urls - The URLs to extract information from. Optional if using other methods for data extraction.
1241
- * @param params - Additional parameters for the extract request.
1242
- * @returns The response from the extract operation.
1243
- */
1244
- async extract<T extends zt.ZodSchema = any>(urls?: string[], params?: ExtractParams<T>): Promise<ExtractResponse<zt.infer<T>> | ErrorResponse> {
1245
- const headers = this.prepareHeaders();
1246
-
1247
- let jsonData: { urls?: string[] } & ExtractParams<T> = { urls: urls, ...params };
1248
- let jsonSchema: any;
1249
- try {
1250
- if (!params?.schema) {
1251
- jsonSchema = undefined;
1252
- } else {
1253
- try {
1254
- jsonSchema = zodToJsonSchema(params.schema as zt.ZodType);
1255
- } catch (_) {
1256
- jsonSchema = params.schema;
1257
- }
1258
- }
1259
- } catch (error: any) {
1260
- throw new FirecrawlError("Invalid schema. Schema must be either a valid Zod schema or JSON schema object.", 400);
1261
- }
1262
-
1263
- try {
1264
- const response: AxiosResponse = await this.postRequest(
1265
- this.apiUrl + `/v1/extract`,
1266
- { ...jsonData, schema: jsonSchema, origin: `js-sdk@${this.version}` },
1267
- headers
1268
- );
1269
-
1270
- if (response.status === 200) {
1271
- const jobId = response.data.id;
1272
- let extractStatus;
1273
- do {
1274
- const statusResponse: AxiosResponse = await this.getRequest(
1275
- `${this.apiUrl}/v1/extract/${jobId}`,
1276
- headers
1277
- );
1278
- extractStatus = statusResponse.data;
1279
- if (extractStatus.status === "completed") {
1280
- if (extractStatus.success) {
1281
- return {
1282
- success: true,
1283
- data: extractStatus.data,
1284
- warning: extractStatus.warning,
1285
- error: extractStatus.error,
1286
- sources: extractStatus?.sources || undefined,
1287
- };
1288
- } else {
1289
- throw new FirecrawlError(`Failed to extract data. Error: ${extractStatus.error}`, statusResponse.status);
1290
- }
1291
- } else if (extractStatus.status === "failed" || extractStatus.status === "cancelled") {
1292
- throw new FirecrawlError(`Extract job ${extractStatus.status}. Error: ${extractStatus.error}`, statusResponse.status);
1293
- }
1294
- await new Promise(resolve => setTimeout(resolve, 1000)); // Polling interval
1295
- } while (extractStatus.status !== "completed");
1296
- } else {
1297
- this.handleError(response, "extract");
1298
- }
1299
- } catch (error: any) {
1300
- throw new FirecrawlError(error.message, 500, error.response?.data?.details);
1301
- }
1302
- return { success: false, error: "Internal server error."};
1303
- }
1304
-
1305
- /**
1306
- * Initiates an asynchronous extract job for a URL using the Firecrawl API.
1307
- * @param url - The URL to extract data from.
1308
- * @param params - Additional parameters for the extract request.
1309
- * @param idempotencyKey - Optional idempotency key for the request.
1310
- * @returns The response from the extract operation.
1311
- */
1312
- async asyncExtract(
1313
- urls: string[],
1314
- params?: ExtractParams,
1315
- idempotencyKey?: string
1316
- ): Promise<ExtractResponse | ErrorResponse> {
1317
- const headers = this.prepareHeaders(idempotencyKey);
1318
- let jsonData: any = { urls, ...params };
1319
- let jsonSchema: any;
1320
-
1321
- try {
1322
- if (!params?.schema) {
1323
- jsonSchema = undefined;
1324
- } else {
1325
- try {
1326
- jsonSchema = zodToJsonSchema(params.schema as zt.ZodType);
1327
- } catch (_) {
1328
- jsonSchema = params.schema;
1329
- }
1330
- }
1331
- } catch (error: any) {
1332
- throw new FirecrawlError("Invalid schema. Schema must be either a valid Zod schema or JSON schema object.", 400);
1333
- }
1334
-
1335
- try {
1336
- const response: AxiosResponse = await this.postRequest(
1337
- this.apiUrl + `/v1/extract`,
1338
- { ...jsonData, schema: jsonSchema, origin: `js-sdk@${this.version}` },
1339
- headers
1340
- );
1341
-
1342
- if (response.status === 200) {
1343
- return response.data;
1344
- } else {
1345
- this.handleError(response, "start extract job");
1346
- }
1347
- } catch (error: any) {
1348
- throw new FirecrawlError(error.message, 500, error.response?.data?.details);
1349
- }
1350
- return { success: false, error: "Internal server error." };
1351
- }
1352
-
1353
- /**
1354
- * Retrieves the status of an extract job.
1355
- * @param jobId - The ID of the extract job.
1356
- * @returns The status of the extract job.
1357
- */
1358
- async getExtractStatus(jobId: string): Promise<any> {
1359
- try {
1360
- const response: AxiosResponse = await this.getRequest(
1361
- `${this.apiUrl}/v1/extract/${jobId}`,
1362
- this.prepareHeaders()
1363
- );
1364
-
1365
- if (response.status === 200) {
1366
- return response.data;
1367
- } else {
1368
- this.handleError(response, "get extract status");
1369
- }
1370
- } catch (error: any) {
1371
- throw new FirecrawlError(error.message, 500);
1372
- }
1373
- }
1374
-
1375
- /**
1376
- * Prepares the headers for an API request.
1377
- * @param idempotencyKey - Optional key to ensure idempotency.
1378
- * @returns The prepared headers.
1379
- */
1380
- prepareHeaders(idempotencyKey?: string): AxiosRequestHeaders {
1381
- return {
1382
- "Content-Type": "application/json",
1383
- Authorization: `Bearer ${this.apiKey}`,
1384
- ...(idempotencyKey ? { "x-idempotency-key": idempotencyKey } : {}),
1385
- } as AxiosRequestHeaders & { "x-idempotency-key"?: string };
1386
- }
1387
-
1388
- /**
1389
- * Sends a POST request to the specified URL.
1390
- * @param url - The URL to send the request to.
1391
- * @param data - The data to send in the request.
1392
- * @param headers - The headers for the request.
1393
- * @returns The response from the POST request.
1394
- */
1395
- postRequest(
1396
- url: string,
1397
- data: any,
1398
- headers: AxiosRequestHeaders
1399
- ): Promise<AxiosResponse> {
1400
- return axios.post(url, data, { headers, timeout: (data?.timeout ? (data.timeout + 5000) : undefined) });
1401
- }
1402
-
1403
- /**
1404
- * Sends a GET request to the specified URL.
1405
- * @param url - The URL to send the request to.
1406
- * @param headers - The headers for the request.
1407
- * @returns The response from the GET request.
1408
- */
1409
- async getRequest(
1410
- url: string,
1411
- headers: AxiosRequestHeaders
1412
- ): Promise<AxiosResponse> {
1413
- try {
1414
- return await axios.get(url, { headers });
1415
- } catch (error) {
1416
- if (error instanceof AxiosError && error.response) {
1417
- return error.response as AxiosResponse;
1418
- } else {
1419
- throw error;
1420
- }
1421
- }
1422
- }
1423
-
1424
- /**
1425
- * Sends a DELETE request to the specified URL.
1426
- * @param url - The URL to send the request to.
1427
- * @param headers - The headers for the request.
1428
- * @returns The response from the DELETE request.
1429
- */
1430
- async deleteRequest(
1431
- url: string,
1432
- headers: AxiosRequestHeaders
1433
- ): Promise<AxiosResponse> {
1434
- try {
1435
- return await axios.delete(url, { headers });
1436
- } catch (error) {
1437
- if (error instanceof AxiosError && error.response) {
1438
- return error.response as AxiosResponse;
1439
- } else {
1440
- throw error;
1441
- }
1442
- }
1443
- }
1444
-
1445
- /**
1446
- * Monitors the status of a crawl job until completion or failure.
1447
- * @param id - The ID of the crawl operation.
1448
- * @param headers - The headers for the request.
1449
- * @param checkInterval - Interval in seconds for job status checks.
1450
- * @param checkUrl - Optional URL to check the status (used for v1 API)
1451
- * @returns The final job status or data.
1452
- */
1453
- async monitorJobStatus(
1454
- id: string,
1455
- headers: AxiosRequestHeaders,
1456
- checkInterval: number
1457
- ): Promise<CrawlStatusResponse | ErrorResponse> {
1458
- try {
1459
- let failedTries = 0;
1460
- while (true) {
1461
- let statusResponse: AxiosResponse = await this.getRequest(
1462
- `${this.apiUrl}/v1/crawl/${id}`,
1463
- headers
1464
- );
1465
- if (statusResponse.status === 200) {
1466
- failedTries = 0;
1467
- let statusData = statusResponse.data;
1468
- if (statusData.status === "completed") {
1469
- if ("data" in statusData) {
1470
- let data = statusData.data;
1471
- while (typeof statusData === 'object' && 'next' in statusData) {
1472
- if (data.length === 0) {
1473
- break
1474
- }
1475
- statusResponse = await this.getRequest(statusData.next, headers);
1476
- statusData = statusResponse.data;
1477
- data = data.concat(statusData.data);
1478
- }
1479
- statusData.data = data;
1480
- return statusData;
1481
- } else {
1482
- throw new FirecrawlError("Crawl job completed but no data was returned", 500);
1483
- }
1484
- } else if (
1485
- ["active", "paused", "pending", "queued", "waiting", "scraping"].includes(statusData.status)
1486
- ) {
1487
- checkInterval = Math.max(checkInterval, 2);
1488
- await new Promise((resolve) =>
1489
- setTimeout(resolve, checkInterval * 1000)
1490
- );
1491
- } else {
1492
- throw new FirecrawlError(
1493
- `Crawl job failed or was stopped. Status: ${statusData.status}`,
1494
- 500
1495
- );
1496
- }
1497
- } else {
1498
- failedTries++;
1499
- if (failedTries >= 3) {
1500
- this.handleError(statusResponse, "check crawl status");
1501
- }
1502
- }
1503
- }
1504
- } catch (error: any) {
1505
- throw new FirecrawlError(error, 500);
1506
- }
1507
- }
1508
-
1509
- /**
1510
- * Handles errors from API responses.
1511
- * @param {AxiosResponse} response - The response from the API.
1512
- * @param {string} action - The action being performed when the error occurred.
1513
- */
1514
- handleError(response: AxiosResponse, action: string): void {
1515
- if (!response) {
1516
- throw new FirecrawlError(
1517
- `No response received while trying to ${action}. This may be a network error or the server is unreachable.`,
1518
- 0
1519
- );
1520
- }
1521
-
1522
- if ([400, 402, 403, 408, 409, 500].includes(response.status)) {
1523
- const errorMessage: string =
1524
- response.data.error || "Unknown error occurred";
1525
- const details = response.data.details ? ` - ${JSON.stringify(response.data.details)}` : '';
1526
- throw new FirecrawlError(
1527
- `Failed to ${action}. Status code: ${response.status}. Error: ${errorMessage}${details}`,
1528
- response.status,
1529
- response?.data?.details
1530
- );
1531
- } else {
1532
- throw new FirecrawlError(
1533
- `Unexpected error occurred while trying to ${action}. Status code: ${response.status}`,
1534
- response.status
1535
- );
1536
- }
1537
- }
1538
-
1539
- /**
1540
- * Initiates a deep research operation on a given query and polls until completion.
1541
- * @param query - The query to research.
1542
- * @param params - Parameters for the deep research operation.
1543
- * @param onActivity - Optional callback to receive activity updates in real-time.
1544
- * @param onSource - Optional callback to receive source updates in real-time.
1545
- * @returns The final research results.
1546
- */
1547
- async deepResearch(
1548
- query: string,
1549
- params: DeepResearchParams<zt.ZodSchema>,
1550
- onActivity?: (activity: {
1551
- type: string;
1552
- status: string;
1553
- message: string;
1554
- timestamp: string;
1555
- depth: number;
1556
- }) => void,
1557
- onSource?: (source: {
1558
- url: string;
1559
- title?: string;
1560
- description?: string;
1561
- icon?: string;
1562
- }) => void
1563
- ): Promise<DeepResearchStatusResponse | ErrorResponse> {
1564
- try {
1565
- const response = await this.asyncDeepResearch(query, params);
1566
-
1567
- if (!response.success || 'error' in response) {
1568
- return { success: false, error: 'error' in response ? response.error : 'Unknown error' };
1569
- }
1570
-
1571
- if (!response.id) {
1572
- throw new FirecrawlError(`Failed to start research. No job ID returned.`, 500);
1573
- }
1574
-
1575
- const jobId = response.id;
1576
- let researchStatus;
1577
- let lastActivityCount = 0;
1578
- let lastSourceCount = 0;
1579
-
1580
- while (true) {
1581
- researchStatus = await this.checkDeepResearchStatus(jobId);
1582
-
1583
- if ('error' in researchStatus && !researchStatus.success) {
1584
- return researchStatus;
1585
- }
1586
-
1587
- // Stream new activities through the callback if provided
1588
- if (onActivity && researchStatus.activities) {
1589
- const newActivities = researchStatus.activities.slice(lastActivityCount);
1590
- for (const activity of newActivities) {
1591
- onActivity(activity);
1592
- }
1593
- lastActivityCount = researchStatus.activities.length;
1594
- }
1595
-
1596
- // Stream new sources through the callback if provided
1597
- if (onSource && researchStatus.sources) {
1598
- const newSources = researchStatus.sources.slice(lastSourceCount);
1599
- for (const source of newSources) {
1600
- onSource(source);
1601
- }
1602
- lastSourceCount = researchStatus.sources.length;
1603
- }
1604
-
1605
- if (researchStatus.status === "completed") {
1606
- return researchStatus;
1607
- }
1608
-
1609
- if (researchStatus.status === "failed") {
1610
- throw new FirecrawlError(
1611
- `Research job ${researchStatus.status}. Error: ${researchStatus.error}`,
1612
- 500
1613
- );
1614
- }
1615
-
1616
- if (researchStatus.status !== "processing") {
1617
- break;
1618
- }
1619
-
1620
- await new Promise(resolve => setTimeout(resolve, 2000));
1621
- }
1622
-
1623
- return { success: false, error: "Research job terminated unexpectedly" };
1624
- } catch (error: any) {
1625
- throw new FirecrawlError(error.message, 500, error.response?.data?.details);
1626
- }
1627
- }
1628
-
1629
- /**
1630
- * Initiates a deep research operation on a given query without polling.
1631
- * @param params - Parameters for the deep research operation.
1632
- * @returns The response containing the research job ID.
1633
- */
1634
- async asyncDeepResearch(query: string, params: DeepResearchParams<zt.ZodSchema>): Promise<DeepResearchResponse | ErrorResponse> {
1635
- const headers = this.prepareHeaders();
1636
- let jsonData: any = { query, ...params, origin: `js-sdk@${this.version}` };
1637
-
1638
- if (jsonData?.jsonOptions?.schema) {
1639
- let schema = jsonData.jsonOptions.schema;
1640
- // Try parsing the schema as a Zod schema
1641
- try {
1642
- schema = zodToJsonSchema(schema);
1643
- } catch (error) {
1644
- // Ignore error if schema can't be parsed as Zod
1645
- }
1646
- jsonData = {
1647
- ...jsonData,
1648
- jsonOptions: {
1649
- ...jsonData.jsonOptions,
1650
- schema: schema,
1651
- },
1652
- };
1653
- }
1654
-
1655
- try {
1656
- const response: AxiosResponse = await this.postRequest(
1657
- `${this.apiUrl}/v1/deep-research`,
1658
- jsonData,
1659
- headers
1660
- );
1661
-
1662
- if (response.status === 200) {
1663
- return response.data;
1664
- } else {
1665
- this.handleError(response, "start deep research");
1666
- }
1667
- } catch (error: any) {
1668
- if (error.response?.data?.error) {
1669
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
1670
- } else {
1671
- throw new FirecrawlError(error.message, 500);
1672
- }
1673
- }
1674
- return { success: false, error: "Internal server error." };
1675
- }
1676
-
1677
- /**
1678
- * Checks the status of a deep research operation.
1679
- * @param id - The ID of the deep research operation.
1680
- * @returns The current status and results of the research operation.
1681
- */
1682
- async checkDeepResearchStatus(id: string): Promise<DeepResearchStatusResponse | ErrorResponse> {
1683
- const headers = this.prepareHeaders();
1684
- try {
1685
- const response: AxiosResponse = await this.getRequest(
1686
- `${this.apiUrl}/v1/deep-research/${id}`,
1687
- headers
1688
- );
1689
-
1690
- if (response.status === 200) {
1691
- return response.data;
1692
- } else if (response.status === 404) {
1693
- throw new FirecrawlError("Deep research job not found", 404);
1694
- } else {
1695
- this.handleError(response, "check deep research status");
1696
- }
1697
- } catch (error: any) {
1698
- if (error.response?.data?.error) {
1699
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
1700
- } else {
1701
- throw new FirecrawlError(error.message, 500);
1702
- }
1703
- }
1704
- return { success: false, error: "Internal server error." };
1705
- }
1706
-
1707
- /**
1708
- * @deprecated Use deepResearch() instead
1709
- * Initiates a deep research operation on a given topic and polls until completion.
1710
- * @param topic - The topic to research.
1711
- * @param params - Parameters for the deep research operation.
1712
- * @param onActivity - Optional callback to receive activity updates in real-time.
1713
- * @returns The final research results.
1714
- */
1715
- async __deepResearch(
1716
- topic: string,
1717
- params: DeepResearchParams,
1718
- onActivity?: (activity: {
1719
- type: string;
1720
- status: string;
1721
- message: string;
1722
- timestamp: string;
1723
- depth: number;
1724
- }) => void
1725
- ): Promise<DeepResearchStatusResponse | ErrorResponse> {
1726
- try {
1727
- const response = await this.__asyncDeepResearch(topic, params);
1728
-
1729
- if (!response.success || 'error' in response) {
1730
- return { success: false, error: 'error' in response ? response.error : 'Unknown error' };
1731
- }
1732
-
1733
- if (!response.id) {
1734
- throw new FirecrawlError(`Failed to start research. No job ID returned.`, 500);
1735
- }
1736
-
1737
- const jobId = response.id;
1738
- let researchStatus;
1739
- let lastActivityCount = 0;
1740
-
1741
- while (true) {
1742
- researchStatus = await this.__checkDeepResearchStatus(jobId);
1743
-
1744
- if ('error' in researchStatus && !researchStatus.success) {
1745
- return researchStatus;
1746
- }
1747
-
1748
- // Stream new activities through the callback if provided
1749
- if (onActivity && researchStatus.activities) {
1750
- const newActivities = researchStatus.activities.slice(lastActivityCount);
1751
- for (const activity of newActivities) {
1752
- onActivity(activity);
1753
- }
1754
- lastActivityCount = researchStatus.activities.length;
1755
- }
1756
-
1757
- if (researchStatus.status === "completed") {
1758
- return researchStatus;
1759
- }
1760
-
1761
- if (researchStatus.status === "failed") {
1762
- throw new FirecrawlError(
1763
- `Research job ${researchStatus.status}. Error: ${researchStatus.error}`,
1764
- 500
1765
- );
1766
- }
1767
-
1768
- if (researchStatus.status !== "processing") {
1769
- break;
1770
- }
1771
-
1772
- await new Promise(resolve => setTimeout(resolve, 2000));
1773
- }
1774
-
1775
- return { success: false, error: "Research job terminated unexpectedly" };
1776
- } catch (error: any) {
1777
- throw new FirecrawlError(error.message, 500, error.response?.data?.details);
1778
- }
1779
- }
1780
-
1781
- /**
1782
- * @deprecated Use asyncDeepResearch() instead
1783
- * Initiates a deep research operation on a given topic without polling.
1784
- * @param params - Parameters for the deep research operation.
1785
- * @returns The response containing the research job ID.
1786
- */
1787
- async __asyncDeepResearch(topic: string, params: DeepResearchParams): Promise<DeepResearchResponse | ErrorResponse> {
1788
- const headers = this.prepareHeaders();
1789
- try {
1790
- let jsonData: any = { topic, ...params, origin: `js-sdk@${this.version}` };
1791
- const response: AxiosResponse = await this.postRequest(
1792
- `${this.apiUrl}/v1/deep-research`,
1793
- jsonData,
1794
- headers
1795
- );
1796
-
1797
- if (response.status === 200) {
1798
- return response.data;
1799
- } else {
1800
- this.handleError(response, "start deep research");
1801
- }
1802
- } catch (error: any) {
1803
- if (error.response?.data?.error) {
1804
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
1805
- } else {
1806
- throw new FirecrawlError(error.message, 500);
1807
- }
1808
- }
1809
- return { success: false, error: "Internal server error." };
1810
- }
1811
-
1812
- /**
1813
- * @deprecated Use checkDeepResearchStatus() instead
1814
- * Checks the status of a deep research operation.
1815
- * @param id - The ID of the deep research operation.
1816
- * @returns The current status and results of the research operation.
1817
- */
1818
- async __checkDeepResearchStatus(id: string): Promise<DeepResearchStatusResponse | ErrorResponse> {
1819
- const headers = this.prepareHeaders();
1820
- try {
1821
- const response: AxiosResponse = await this.getRequest(
1822
- `${this.apiUrl}/v1/deep-research/${id}`,
1823
- headers
1824
- );
1825
-
1826
- if (response.status === 200) {
1827
- return response.data;
1828
- } else if (response.status === 404) {
1829
- throw new FirecrawlError("Deep research job not found", 404);
1830
- } else {
1831
- this.handleError(response, "check deep research status");
1832
- }
1833
- } catch (error: any) {
1834
- if (error.response?.data?.error) {
1835
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
1836
- } else {
1837
- throw new FirecrawlError(error.message, 500);
1838
- }
1839
- }
1840
- return { success: false, error: "Internal server error." };
1841
- }
1842
-
1843
- /**
1844
- * Generates LLMs.txt for a given URL and polls until completion.
1845
- * @param url - The URL to generate LLMs.txt from.
1846
- * @param params - Parameters for the LLMs.txt generation operation.
1847
- * @returns The final generation results.
1848
- */
1849
- async generateLLMsText(url: string, params?: GenerateLLMsTextParams): Promise<GenerateLLMsTextStatusResponse | ErrorResponse> {
1850
- try {
1851
- const response = await this.asyncGenerateLLMsText(url, params);
1852
-
1853
- if (!response.success || 'error' in response) {
1854
- return { success: false, error: 'error' in response ? response.error : 'Unknown error' };
1855
- }
1856
-
1857
- if (!response.id) {
1858
- throw new FirecrawlError(`Failed to start LLMs.txt generation. No job ID returned.`, 500);
1859
- }
1860
-
1861
- const jobId = response.id;
1862
- let generationStatus;
1863
-
1864
- while (true) {
1865
- generationStatus = await this.checkGenerateLLMsTextStatus(jobId);
1866
-
1867
- if ('error' in generationStatus && !generationStatus.success) {
1868
- return generationStatus;
1869
- }
1870
-
1871
- if (generationStatus.status === "completed") {
1872
- return generationStatus;
1873
- }
1874
-
1875
- if (generationStatus.status === "failed") {
1876
- throw new FirecrawlError(
1877
- `LLMs.txt generation job ${generationStatus.status}. Error: ${generationStatus.error}`,
1878
- 500
1879
- );
1880
- }
1881
-
1882
- if (generationStatus.status !== "processing") {
1883
- break;
1884
- }
1885
-
1886
- await new Promise(resolve => setTimeout(resolve, 2000));
1887
- }
1888
-
1889
- return { success: false, error: "LLMs.txt generation job terminated unexpectedly" };
1890
- } catch (error: any) {
1891
- throw new FirecrawlError(error.message, 500, error.response?.data?.details);
1892
- }
1893
- }
1894
-
1895
- /**
1896
- * Initiates a LLMs.txt generation operation without polling.
1897
- * @param url - The URL to generate LLMs.txt from.
1898
- * @param params - Parameters for the LLMs.txt generation operation.
1899
- * @returns The response containing the generation job ID.
1900
- */
1901
- async asyncGenerateLLMsText(url: string, params?: GenerateLLMsTextParams): Promise<GenerateLLMsTextResponse | ErrorResponse> {
1902
- const headers = this.prepareHeaders();
1903
- let jsonData: any = { url, ...params, origin: `js-sdk@${this.version}` };
1904
- try {
1905
- const response: AxiosResponse = await this.postRequest(
1906
- `${this.apiUrl}/v1/llmstxt`,
1907
- jsonData,
1908
- headers
1909
- );
1910
-
1911
- if (response.status === 200) {
1912
- return response.data;
1913
- } else {
1914
- this.handleError(response, "start LLMs.txt generation");
1915
- }
1916
- } catch (error: any) {
1917
- if (error.response?.data?.error) {
1918
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
1919
- } else {
1920
- throw new FirecrawlError(error.message, 500);
1921
- }
1922
- }
1923
- return { success: false, error: "Internal server error." };
1924
- }
1925
-
1926
- /**
1927
- * Checks the status of a LLMs.txt generation operation.
1928
- * @param id - The ID of the LLMs.txt generation operation.
1929
- * @returns The current status and results of the generation operation.
1930
- */
1931
- async checkGenerateLLMsTextStatus(id: string): Promise<GenerateLLMsTextStatusResponse | ErrorResponse> {
1932
- const headers = this.prepareHeaders();
1933
- try {
1934
- const response: AxiosResponse = await this.getRequest(
1935
- `${this.apiUrl}/v1/llmstxt/${id}`,
1936
- headers
1937
- );
1938
-
1939
- if (response.status === 200) {
1940
- return response.data;
1941
- } else if (response.status === 404) {
1942
- throw new FirecrawlError("LLMs.txt generation job not found", 404);
1943
- } else {
1944
- this.handleError(response, "check LLMs.txt generation status");
1945
- }
1946
- } catch (error: any) {
1947
- if (error.response?.data?.error) {
1948
- throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
1949
- } else {
1950
- throw new FirecrawlError(error.message, 500);
1951
- }
1952
- }
1953
- return { success: false, error: "Internal server error." };
1954
- }
1955
- }
1956
-
1957
- interface CrawlWatcherEvents {
1958
- document: CustomEvent<FirecrawlDocument<undefined>>,
1959
- done: CustomEvent<{
1960
- status: CrawlStatusResponse["status"];
1961
- data: FirecrawlDocument<undefined>[];
1962
- }>,
1963
- error: CustomEvent<{
1964
- status: CrawlStatusResponse["status"],
1965
- data: FirecrawlDocument<undefined>[],
1966
- error: string,
1967
- }>,
1968
- }
1969
-
1970
- export class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
1971
- private ws: WebSocket;
1972
- public data: FirecrawlDocument<undefined>[];
1973
- public status: CrawlStatusResponse["status"];
1974
- public id: string;
1975
-
1976
- constructor(id: string, app: FirecrawlApp) {
1977
- super();
1978
- this.id = id;
1979
- // replace `http` with `ws` (`http://` -> `ws://` and `https://` -> `wss://`)
1980
- const wsUrl = app.apiUrl.replace(/^http/, "ws");
1981
- this.ws = new WebSocket(`${wsUrl}/v1/crawl/${id}`, app.apiKey);
1982
- this.status = "scraping";
1983
- this.data = [];
1984
-
1985
- type ErrorMessage = {
1986
- type: "error",
1987
- error: string,
1988
- }
1989
-
1990
- type CatchupMessage = {
1991
- type: "catchup",
1992
- data: CrawlStatusResponse,
1993
- }
1994
-
1995
- type DocumentMessage = {
1996
- type: "document",
1997
- data: FirecrawlDocument<undefined>,
1998
- }
1999
-
2000
- type DoneMessage = { type: "done" }
2001
-
2002
- type Message = ErrorMessage | CatchupMessage | DoneMessage | DocumentMessage;
2003
-
2004
- const messageHandler = (msg: Message) => {
2005
- if (msg.type === "done") {
2006
- this.status = "completed";
2007
- this.dispatchTypedEvent("done", new CustomEvent("done", {
2008
- detail: {
2009
- status: this.status,
2010
- data: this.data,
2011
- id: this.id,
2012
- },
2013
- }));
2014
- } else if (msg.type === "error") {
2015
- this.status = "failed";
2016
- this.dispatchTypedEvent("error", new CustomEvent("error", {
2017
- detail: {
2018
- status: this.status,
2019
- data: this.data,
2020
- error: msg.error,
2021
- id: this.id,
2022
- },
2023
- }));
2024
- } else if (msg.type === "catchup") {
2025
- this.status = msg.data.status;
2026
- this.data.push(...(msg.data.data ?? []));
2027
- for (const doc of this.data) {
2028
- this.dispatchTypedEvent("document", new CustomEvent("document", {
2029
- detail: {
2030
- ...doc,
2031
- id: this.id,
2032
- },
2033
- }));
2034
- }
2035
- } else if (msg.type === "document") {
2036
- this.dispatchTypedEvent("document", new CustomEvent("document", {
2037
- detail: {
2038
- ...msg.data,
2039
- id: this.id,
2040
- },
2041
- }));
2042
- }
2043
- }
2044
-
2045
- this.ws.onmessage = ((ev: MessageEvent) => {
2046
- if (typeof ev.data !== "string") {
2047
- this.ws.close();
2048
- return;
2049
- }
2050
- try {
2051
- const msg = JSON.parse(ev.data) as Message;
2052
- messageHandler(msg);
2053
- } catch (error) {
2054
- console.error("Error on message", error);
2055
- }
2056
- }).bind(this);
2057
-
2058
- this.ws.onclose = ((ev: CloseEvent) => {
2059
- try {
2060
- const msg = JSON.parse(ev.reason) as Message;
2061
- messageHandler(msg);
2062
- } catch (error) {
2063
- console.error("Error on close", error);
2064
- }
2065
- }).bind(this);
2066
-
2067
- this.ws.onerror = ((_: Event) => {
2068
- this.status = "failed"
2069
- this.dispatchTypedEvent("error", new CustomEvent("error", {
2070
- detail: {
2071
- status: this.status,
2072
- data: this.data,
2073
- error: "WebSocket error",
2074
- id: this.id,
2075
- },
2076
- }));
2077
- }).bind(this);
2078
- }
2079
-
2080
- close() {
2081
- this.ws.close();
2082
- }
2083
- }