@mendable/firecrawl-js 0.0.17-beta.4 → 0.0.17-beta.7
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/build/index.js +38 -39
- package/package.json +1 -1
- package/src/index.ts +114 -53
- package/types/index.d.ts +8 -6
package/build/index.js
CHANGED
|
@@ -7,8 +7,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import axios from
|
|
11
|
-
import { zodToJsonSchema } from
|
|
10
|
+
import axios from "axios";
|
|
11
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
12
12
|
/**
|
|
13
13
|
* Main class for interacting with the Firecrawl API.
|
|
14
14
|
*/
|
|
@@ -18,9 +18,9 @@ export default class FirecrawlApp {
|
|
|
18
18
|
* @param {FirecrawlAppConfig} config - Configuration options for the FirecrawlApp instance.
|
|
19
19
|
*/
|
|
20
20
|
constructor({ apiKey = null }) {
|
|
21
|
-
this.apiKey = apiKey ||
|
|
21
|
+
this.apiKey = apiKey || "";
|
|
22
22
|
if (!this.apiKey) {
|
|
23
|
-
throw new Error(
|
|
23
|
+
throw new Error("No API key provided");
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
@@ -30,24 +30,19 @@ export default class FirecrawlApp {
|
|
|
30
30
|
* @returns {Promise<ScrapeResponse>} The response from the scrape operation.
|
|
31
31
|
*/
|
|
32
32
|
scrapeUrl(url_1) {
|
|
33
|
-
return __awaiter(this, arguments, void 0, function* (url, params = null
|
|
33
|
+
return __awaiter(this, arguments, void 0, function* (url, params = null) {
|
|
34
|
+
var _a;
|
|
34
35
|
const headers = {
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
37
38
|
};
|
|
38
|
-
let jsonData = { url };
|
|
39
|
-
if (params) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (extractorOptions) {
|
|
43
|
-
jsonData.extractorOptions = Object.assign({}, extractorOptions);
|
|
44
|
-
}
|
|
45
|
-
if (extractorOptions === null || extractorOptions === void 0 ? void 0 : extractorOptions.extractionSchema) {
|
|
46
|
-
const schema = zodToJsonSchema(extractorOptions.extractionSchema);
|
|
47
|
-
jsonData = Object.assign(Object.assign({}, jsonData), { extractorOptions: Object.assign(Object.assign({}, extractorOptions), { extractionSchema: schema }) });
|
|
39
|
+
let jsonData = Object.assign({ url }, params);
|
|
40
|
+
if ((_a = params === null || params === void 0 ? void 0 : params.extractorOptions) === null || _a === void 0 ? void 0 : _a.extractionSchema) {
|
|
41
|
+
const schema = zodToJsonSchema(params.extractorOptions.extractionSchema);
|
|
42
|
+
jsonData = Object.assign(Object.assign({}, jsonData), { extractorOptions: Object.assign(Object.assign({}, params.extractorOptions), { extractionSchema: schema, mode: params.extractorOptions.mode || "llm-extraction" }) });
|
|
48
43
|
}
|
|
49
44
|
try {
|
|
50
|
-
const response = yield axios.post(
|
|
45
|
+
const response = yield axios.post("https://api.firecrawl.dev/v0/scrape", jsonData, { headers });
|
|
51
46
|
if (response.status === 200) {
|
|
52
47
|
const responseData = response.data;
|
|
53
48
|
if (responseData.success) {
|
|
@@ -58,13 +53,13 @@ export default class FirecrawlApp {
|
|
|
58
53
|
}
|
|
59
54
|
}
|
|
60
55
|
else {
|
|
61
|
-
this.handleError(response,
|
|
56
|
+
this.handleError(response, "scrape URL");
|
|
62
57
|
}
|
|
63
58
|
}
|
|
64
59
|
catch (error) {
|
|
65
60
|
throw new Error(error.message);
|
|
66
61
|
}
|
|
67
|
-
return { success: false, error:
|
|
62
|
+
return { success: false, error: "Internal server error." };
|
|
68
63
|
});
|
|
69
64
|
}
|
|
70
65
|
/**
|
|
@@ -76,15 +71,15 @@ export default class FirecrawlApp {
|
|
|
76
71
|
search(query_1) {
|
|
77
72
|
return __awaiter(this, arguments, void 0, function* (query, params = null) {
|
|
78
73
|
const headers = {
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
"Content-Type": "application/json",
|
|
75
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
81
76
|
};
|
|
82
77
|
let jsonData = { query };
|
|
83
78
|
if (params) {
|
|
84
79
|
jsonData = Object.assign(Object.assign({}, jsonData), params);
|
|
85
80
|
}
|
|
86
81
|
try {
|
|
87
|
-
const response = yield axios.post(
|
|
82
|
+
const response = yield axios.post("https://api.firecrawl.dev/v0/search", jsonData, { headers });
|
|
88
83
|
if (response.status === 200) {
|
|
89
84
|
const responseData = response.data;
|
|
90
85
|
if (responseData.success) {
|
|
@@ -95,13 +90,13 @@ export default class FirecrawlApp {
|
|
|
95
90
|
}
|
|
96
91
|
}
|
|
97
92
|
else {
|
|
98
|
-
this.handleError(response,
|
|
93
|
+
this.handleError(response, "search");
|
|
99
94
|
}
|
|
100
95
|
}
|
|
101
96
|
catch (error) {
|
|
102
97
|
throw new Error(error.message);
|
|
103
98
|
}
|
|
104
|
-
return { success: false, error:
|
|
99
|
+
return { success: false, error: "Internal server error." };
|
|
105
100
|
});
|
|
106
101
|
}
|
|
107
102
|
/**
|
|
@@ -120,7 +115,7 @@ export default class FirecrawlApp {
|
|
|
120
115
|
jsonData = Object.assign(Object.assign({}, jsonData), params);
|
|
121
116
|
}
|
|
122
117
|
try {
|
|
123
|
-
const response = yield this.postRequest(
|
|
118
|
+
const response = yield this.postRequest("https://api.firecrawl.dev/v0/crawl", jsonData, headers);
|
|
124
119
|
if (response.status === 200) {
|
|
125
120
|
const jobId = response.data.jobId;
|
|
126
121
|
if (waitUntilDone) {
|
|
@@ -131,14 +126,14 @@ export default class FirecrawlApp {
|
|
|
131
126
|
}
|
|
132
127
|
}
|
|
133
128
|
else {
|
|
134
|
-
this.handleError(response,
|
|
129
|
+
this.handleError(response, "start crawl job");
|
|
135
130
|
}
|
|
136
131
|
}
|
|
137
132
|
catch (error) {
|
|
138
133
|
console.log(error);
|
|
139
134
|
throw new Error(error.message);
|
|
140
135
|
}
|
|
141
|
-
return { success: false, error:
|
|
136
|
+
return { success: false, error: "Internal server error." };
|
|
142
137
|
});
|
|
143
138
|
}
|
|
144
139
|
/**
|
|
@@ -155,13 +150,17 @@ export default class FirecrawlApp {
|
|
|
155
150
|
return response.data;
|
|
156
151
|
}
|
|
157
152
|
else {
|
|
158
|
-
this.handleError(response,
|
|
153
|
+
this.handleError(response, "check crawl status");
|
|
159
154
|
}
|
|
160
155
|
}
|
|
161
156
|
catch (error) {
|
|
162
157
|
throw new Error(error.message);
|
|
163
158
|
}
|
|
164
|
-
return {
|
|
159
|
+
return {
|
|
160
|
+
success: false,
|
|
161
|
+
status: "unknown",
|
|
162
|
+
error: "Internal server error.",
|
|
163
|
+
};
|
|
165
164
|
});
|
|
166
165
|
}
|
|
167
166
|
/**
|
|
@@ -170,8 +169,8 @@ export default class FirecrawlApp {
|
|
|
170
169
|
*/
|
|
171
170
|
prepareHeaders() {
|
|
172
171
|
return {
|
|
173
|
-
|
|
174
|
-
|
|
172
|
+
"Content-Type": "application/json",
|
|
173
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
175
174
|
};
|
|
176
175
|
}
|
|
177
176
|
/**
|
|
@@ -206,26 +205,26 @@ export default class FirecrawlApp {
|
|
|
206
205
|
const statusResponse = yield this.getRequest(`https://api.firecrawl.dev/v0/crawl/status/${jobId}`, headers);
|
|
207
206
|
if (statusResponse.status === 200) {
|
|
208
207
|
const statusData = statusResponse.data;
|
|
209
|
-
if (statusData.status ===
|
|
210
|
-
if (
|
|
208
|
+
if (statusData.status === "completed") {
|
|
209
|
+
if ("data" in statusData) {
|
|
211
210
|
return statusData.data;
|
|
212
211
|
}
|
|
213
212
|
else {
|
|
214
|
-
throw new Error(
|
|
213
|
+
throw new Error("Crawl job completed but no data was returned");
|
|
215
214
|
}
|
|
216
215
|
}
|
|
217
|
-
else if ([
|
|
216
|
+
else if (["active", "paused", "pending", "queued"].includes(statusData.status)) {
|
|
218
217
|
if (timeout < 2) {
|
|
219
218
|
timeout = 2;
|
|
220
219
|
}
|
|
221
|
-
yield new Promise(resolve => setTimeout(resolve, timeout * 1000)); // Wait for the specified timeout before checking again
|
|
220
|
+
yield new Promise((resolve) => setTimeout(resolve, timeout * 1000)); // Wait for the specified timeout before checking again
|
|
222
221
|
}
|
|
223
222
|
else {
|
|
224
223
|
throw new Error(`Crawl job failed or was stopped. Status: ${statusData.status}`);
|
|
225
224
|
}
|
|
226
225
|
}
|
|
227
226
|
else {
|
|
228
|
-
this.handleError(statusResponse,
|
|
227
|
+
this.handleError(statusResponse, "check crawl status");
|
|
229
228
|
}
|
|
230
229
|
}
|
|
231
230
|
});
|
|
@@ -237,7 +236,7 @@ export default class FirecrawlApp {
|
|
|
237
236
|
*/
|
|
238
237
|
handleError(response, action) {
|
|
239
238
|
if ([402, 409, 500].includes(response.status)) {
|
|
240
|
-
const errorMessage = response.data.error ||
|
|
239
|
+
const errorMessage = response.data.error || "Unknown error occurred";
|
|
241
240
|
throw new Error(`Failed to ${action}. Status code: ${response.status}. Error: ${errorMessage}`);
|
|
242
241
|
}
|
|
243
242
|
else {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import axios, { AxiosResponse, AxiosRequestHeaders } from
|
|
2
|
-
import { z } from
|
|
3
|
-
import { zodToJsonSchema } from
|
|
1
|
+
import axios, { AxiosResponse, AxiosRequestHeaders } from "axios";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
4
4
|
/**
|
|
5
5
|
* Configuration interface for FirecrawlApp.
|
|
6
6
|
*/
|
|
@@ -13,6 +13,11 @@ export interface FirecrawlAppConfig {
|
|
|
13
13
|
*/
|
|
14
14
|
export interface Params {
|
|
15
15
|
[key: string]: any;
|
|
16
|
+
extractorOptions?: {
|
|
17
|
+
extractionSchema: z.ZodSchema | any;
|
|
18
|
+
mode?: "llm-extraction";
|
|
19
|
+
extractionPrompt?: string;
|
|
20
|
+
};
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
/**
|
|
@@ -64,9 +69,9 @@ export default class FirecrawlApp {
|
|
|
64
69
|
* @param {FirecrawlAppConfig} config - Configuration options for the FirecrawlApp instance.
|
|
65
70
|
*/
|
|
66
71
|
constructor({ apiKey = null }: FirecrawlAppConfig) {
|
|
67
|
-
this.apiKey = apiKey ||
|
|
72
|
+
this.apiKey = apiKey || "";
|
|
68
73
|
if (!this.apiKey) {
|
|
69
|
-
throw new Error(
|
|
74
|
+
throw new Error("No API key provided");
|
|
70
75
|
}
|
|
71
76
|
}
|
|
72
77
|
|
|
@@ -76,38 +81,48 @@ export default class FirecrawlApp {
|
|
|
76
81
|
* @param {Params | null} params - Additional parameters for the scrape request.
|
|
77
82
|
* @returns {Promise<ScrapeResponse>} The response from the scrape operation.
|
|
78
83
|
*/
|
|
79
|
-
async scrapeUrl(
|
|
84
|
+
async scrapeUrl(
|
|
85
|
+
url: string,
|
|
86
|
+
params: Params | null = null
|
|
87
|
+
): Promise<ScrapeResponse> {
|
|
80
88
|
const headers: AxiosRequestHeaders = {
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
"Content-Type": "application/json",
|
|
90
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
83
91
|
} as AxiosRequestHeaders;
|
|
84
|
-
let jsonData: Params
|
|
85
|
-
if (params) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
jsonData
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
let jsonData: Params = { url, ...params };
|
|
93
|
+
if (params?.extractorOptions?.extractionSchema) {
|
|
94
|
+
const schema = zodToJsonSchema(
|
|
95
|
+
params.extractorOptions.extractionSchema as z.ZodSchema
|
|
96
|
+
);
|
|
97
|
+
jsonData = {
|
|
98
|
+
...jsonData,
|
|
99
|
+
extractorOptions: {
|
|
100
|
+
...params.extractorOptions,
|
|
101
|
+
extractionSchema: schema,
|
|
102
|
+
mode: params.extractorOptions.mode || "llm-extraction",
|
|
103
|
+
},
|
|
104
|
+
};
|
|
94
105
|
}
|
|
95
106
|
try {
|
|
96
|
-
const response: AxiosResponse = await axios.post(
|
|
107
|
+
const response: AxiosResponse = await axios.post(
|
|
108
|
+
"https://api.firecrawl.dev/v0/scrape",
|
|
109
|
+
jsonData,
|
|
110
|
+
{ headers }
|
|
111
|
+
);
|
|
97
112
|
if (response.status === 200) {
|
|
98
113
|
const responseData = response.data;
|
|
99
114
|
if (responseData.success) {
|
|
100
|
-
return responseData;
|
|
115
|
+
return responseData;
|
|
101
116
|
} else {
|
|
102
117
|
throw new Error(`Failed to scrape URL. Error: ${responseData.error}`);
|
|
103
118
|
}
|
|
104
119
|
} else {
|
|
105
|
-
this.handleError(response,
|
|
120
|
+
this.handleError(response, "scrape URL");
|
|
106
121
|
}
|
|
107
122
|
} catch (error: any) {
|
|
108
123
|
throw new Error(error.message);
|
|
109
124
|
}
|
|
110
|
-
return { success: false, error:
|
|
125
|
+
return { success: false, error: "Internal server error." };
|
|
111
126
|
}
|
|
112
127
|
|
|
113
128
|
/**
|
|
@@ -116,31 +131,38 @@ export default class FirecrawlApp {
|
|
|
116
131
|
* @param {Params | null} params - Additional parameters for the search request.
|
|
117
132
|
* @returns {Promise<SearchResponse>} The response from the search operation.
|
|
118
133
|
*/
|
|
119
|
-
async search(
|
|
134
|
+
async search(
|
|
135
|
+
query: string,
|
|
136
|
+
params: Params | null = null
|
|
137
|
+
): Promise<SearchResponse> {
|
|
120
138
|
const headers: AxiosRequestHeaders = {
|
|
121
|
-
|
|
122
|
-
|
|
139
|
+
"Content-Type": "application/json",
|
|
140
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
123
141
|
} as AxiosRequestHeaders;
|
|
124
142
|
let jsonData: Params = { query };
|
|
125
143
|
if (params) {
|
|
126
144
|
jsonData = { ...jsonData, ...params };
|
|
127
145
|
}
|
|
128
146
|
try {
|
|
129
|
-
const response: AxiosResponse = await axios.post(
|
|
147
|
+
const response: AxiosResponse = await axios.post(
|
|
148
|
+
"https://api.firecrawl.dev/v0/search",
|
|
149
|
+
jsonData,
|
|
150
|
+
{ headers }
|
|
151
|
+
);
|
|
130
152
|
if (response.status === 200) {
|
|
131
153
|
const responseData = response.data;
|
|
132
154
|
if (responseData.success) {
|
|
133
|
-
return responseData;
|
|
155
|
+
return responseData;
|
|
134
156
|
} else {
|
|
135
157
|
throw new Error(`Failed to search. Error: ${responseData.error}`);
|
|
136
158
|
}
|
|
137
159
|
} else {
|
|
138
|
-
this.handleError(response,
|
|
160
|
+
this.handleError(response, "search");
|
|
139
161
|
}
|
|
140
162
|
} catch (error: any) {
|
|
141
163
|
throw new Error(error.message);
|
|
142
164
|
}
|
|
143
|
-
return { success: false, error:
|
|
165
|
+
return { success: false, error: "Internal server error." };
|
|
144
166
|
}
|
|
145
167
|
|
|
146
168
|
/**
|
|
@@ -151,14 +173,23 @@ export default class FirecrawlApp {
|
|
|
151
173
|
* @param {number} timeout - Timeout in seconds for job status checks.
|
|
152
174
|
* @returns {Promise<CrawlResponse | any>} The response from the crawl operation.
|
|
153
175
|
*/
|
|
154
|
-
async crawlUrl(
|
|
176
|
+
async crawlUrl(
|
|
177
|
+
url: string,
|
|
178
|
+
params: Params | null = null,
|
|
179
|
+
waitUntilDone: boolean = true,
|
|
180
|
+
timeout: number = 2
|
|
181
|
+
): Promise<CrawlResponse | any> {
|
|
155
182
|
const headers = this.prepareHeaders();
|
|
156
183
|
let jsonData: Params = { url };
|
|
157
184
|
if (params) {
|
|
158
185
|
jsonData = { ...jsonData, ...params };
|
|
159
186
|
}
|
|
160
187
|
try {
|
|
161
|
-
const response: AxiosResponse = await this.postRequest(
|
|
188
|
+
const response: AxiosResponse = await this.postRequest(
|
|
189
|
+
"https://api.firecrawl.dev/v0/crawl",
|
|
190
|
+
jsonData,
|
|
191
|
+
headers
|
|
192
|
+
);
|
|
162
193
|
if (response.status === 200) {
|
|
163
194
|
const jobId: string = response.data.jobId;
|
|
164
195
|
if (waitUntilDone) {
|
|
@@ -167,13 +198,13 @@ export default class FirecrawlApp {
|
|
|
167
198
|
return { success: true, jobId };
|
|
168
199
|
}
|
|
169
200
|
} else {
|
|
170
|
-
this.handleError(response,
|
|
201
|
+
this.handleError(response, "start crawl job");
|
|
171
202
|
}
|
|
172
203
|
} catch (error: any) {
|
|
173
|
-
console.log(error)
|
|
204
|
+
console.log(error);
|
|
174
205
|
throw new Error(error.message);
|
|
175
206
|
}
|
|
176
|
-
return { success: false, error:
|
|
207
|
+
return { success: false, error: "Internal server error." };
|
|
177
208
|
}
|
|
178
209
|
|
|
179
210
|
/**
|
|
@@ -184,16 +215,23 @@ export default class FirecrawlApp {
|
|
|
184
215
|
async checkCrawlStatus(jobId: string): Promise<JobStatusResponse> {
|
|
185
216
|
const headers: AxiosRequestHeaders = this.prepareHeaders();
|
|
186
217
|
try {
|
|
187
|
-
const response: AxiosResponse = await this.getRequest(
|
|
218
|
+
const response: AxiosResponse = await this.getRequest(
|
|
219
|
+
`https://api.firecrawl.dev/v0/crawl/status/${jobId}`,
|
|
220
|
+
headers
|
|
221
|
+
);
|
|
188
222
|
if (response.status === 200) {
|
|
189
223
|
return response.data;
|
|
190
224
|
} else {
|
|
191
|
-
this.handleError(response,
|
|
225
|
+
this.handleError(response, "check crawl status");
|
|
192
226
|
}
|
|
193
227
|
} catch (error: any) {
|
|
194
228
|
throw new Error(error.message);
|
|
195
229
|
}
|
|
196
|
-
return {
|
|
230
|
+
return {
|
|
231
|
+
success: false,
|
|
232
|
+
status: "unknown",
|
|
233
|
+
error: "Internal server error.",
|
|
234
|
+
};
|
|
197
235
|
}
|
|
198
236
|
|
|
199
237
|
/**
|
|
@@ -202,8 +240,8 @@ export default class FirecrawlApp {
|
|
|
202
240
|
*/
|
|
203
241
|
prepareHeaders(): AxiosRequestHeaders {
|
|
204
242
|
return {
|
|
205
|
-
|
|
206
|
-
|
|
243
|
+
"Content-Type": "application/json",
|
|
244
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
207
245
|
} as AxiosRequestHeaders;
|
|
208
246
|
}
|
|
209
247
|
|
|
@@ -214,7 +252,11 @@ export default class FirecrawlApp {
|
|
|
214
252
|
* @param {AxiosRequestHeaders} headers - The headers for the request.
|
|
215
253
|
* @returns {Promise<AxiosResponse>} The response from the POST request.
|
|
216
254
|
*/
|
|
217
|
-
postRequest(
|
|
255
|
+
postRequest(
|
|
256
|
+
url: string,
|
|
257
|
+
data: Params,
|
|
258
|
+
headers: AxiosRequestHeaders
|
|
259
|
+
): Promise<AxiosResponse> {
|
|
218
260
|
return axios.post(url, data, { headers });
|
|
219
261
|
}
|
|
220
262
|
|
|
@@ -224,7 +266,10 @@ export default class FirecrawlApp {
|
|
|
224
266
|
* @param {AxiosRequestHeaders} headers - The headers for the request.
|
|
225
267
|
* @returns {Promise<AxiosResponse>} The response from the GET request.
|
|
226
268
|
*/
|
|
227
|
-
getRequest(
|
|
269
|
+
getRequest(
|
|
270
|
+
url: string,
|
|
271
|
+
headers: AxiosRequestHeaders
|
|
272
|
+
): Promise<AxiosResponse> {
|
|
228
273
|
return axios.get(url, { headers });
|
|
229
274
|
}
|
|
230
275
|
|
|
@@ -235,27 +280,38 @@ export default class FirecrawlApp {
|
|
|
235
280
|
* @param {number} timeout - Timeout in seconds for job status checks.
|
|
236
281
|
* @returns {Promise<any>} The final job status or data.
|
|
237
282
|
*/
|
|
238
|
-
async monitorJobStatus(
|
|
283
|
+
async monitorJobStatus(
|
|
284
|
+
jobId: string,
|
|
285
|
+
headers: AxiosRequestHeaders,
|
|
286
|
+
timeout: number
|
|
287
|
+
): Promise<any> {
|
|
239
288
|
while (true) {
|
|
240
|
-
const statusResponse: AxiosResponse = await this.getRequest(
|
|
289
|
+
const statusResponse: AxiosResponse = await this.getRequest(
|
|
290
|
+
`https://api.firecrawl.dev/v0/crawl/status/${jobId}`,
|
|
291
|
+
headers
|
|
292
|
+
);
|
|
241
293
|
if (statusResponse.status === 200) {
|
|
242
294
|
const statusData = statusResponse.data;
|
|
243
|
-
if (statusData.status ===
|
|
244
|
-
if (
|
|
295
|
+
if (statusData.status === "completed") {
|
|
296
|
+
if ("data" in statusData) {
|
|
245
297
|
return statusData.data;
|
|
246
298
|
} else {
|
|
247
|
-
throw new Error(
|
|
299
|
+
throw new Error("Crawl job completed but no data was returned");
|
|
248
300
|
}
|
|
249
|
-
} else if (
|
|
301
|
+
} else if (
|
|
302
|
+
["active", "paused", "pending", "queued"].includes(statusData.status)
|
|
303
|
+
) {
|
|
250
304
|
if (timeout < 2) {
|
|
251
305
|
timeout = 2;
|
|
252
306
|
}
|
|
253
|
-
await new Promise(resolve => setTimeout(resolve, timeout * 1000)); // Wait for the specified timeout before checking again
|
|
307
|
+
await new Promise((resolve) => setTimeout(resolve, timeout * 1000)); // Wait for the specified timeout before checking again
|
|
254
308
|
} else {
|
|
255
|
-
throw new Error(
|
|
309
|
+
throw new Error(
|
|
310
|
+
`Crawl job failed or was stopped. Status: ${statusData.status}`
|
|
311
|
+
);
|
|
256
312
|
}
|
|
257
313
|
} else {
|
|
258
|
-
this.handleError(statusResponse,
|
|
314
|
+
this.handleError(statusResponse, "check crawl status");
|
|
259
315
|
}
|
|
260
316
|
}
|
|
261
317
|
}
|
|
@@ -267,10 +323,15 @@ export default class FirecrawlApp {
|
|
|
267
323
|
*/
|
|
268
324
|
handleError(response: AxiosResponse, action: string): void {
|
|
269
325
|
if ([402, 409, 500].includes(response.status)) {
|
|
270
|
-
const errorMessage: string =
|
|
271
|
-
|
|
326
|
+
const errorMessage: string =
|
|
327
|
+
response.data.error || "Unknown error occurred";
|
|
328
|
+
throw new Error(
|
|
329
|
+
`Failed to ${action}. Status code: ${response.status}. Error: ${errorMessage}`
|
|
330
|
+
);
|
|
272
331
|
} else {
|
|
273
|
-
throw new Error(
|
|
332
|
+
throw new Error(
|
|
333
|
+
`Unexpected error occurred while trying to ${action}. Status code: ${response.status}`
|
|
334
|
+
);
|
|
274
335
|
}
|
|
275
336
|
}
|
|
276
337
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { AxiosResponse, AxiosRequestHeaders } from
|
|
1
|
+
import { AxiosResponse, AxiosRequestHeaders } from "axios";
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
/**
|
|
3
4
|
* Configuration interface for FirecrawlApp.
|
|
4
5
|
*/
|
|
@@ -10,6 +11,11 @@ export interface FirecrawlAppConfig {
|
|
|
10
11
|
*/
|
|
11
12
|
export interface Params {
|
|
12
13
|
[key: string]: any;
|
|
14
|
+
extractorOptions?: {
|
|
15
|
+
extractionSchema: z.ZodSchema | any;
|
|
16
|
+
mode?: "llm-extraction";
|
|
17
|
+
extractionPrompt?: string;
|
|
18
|
+
};
|
|
13
19
|
}
|
|
14
20
|
/**
|
|
15
21
|
* Response interface for scraping operations.
|
|
@@ -62,11 +68,7 @@ export default class FirecrawlApp {
|
|
|
62
68
|
* @param {Params | null} params - Additional parameters for the scrape request.
|
|
63
69
|
* @returns {Promise<ScrapeResponse>} The response from the scrape operation.
|
|
64
70
|
*/
|
|
65
|
-
scrapeUrl(url: string, params?: Params | null
|
|
66
|
-
mode: 'llm-extraction';
|
|
67
|
-
extractionPrompt?: string;
|
|
68
|
-
extractionSchema: object;
|
|
69
|
-
}): Promise<ScrapeResponse>;
|
|
71
|
+
scrapeUrl(url: string, params?: Params | null): Promise<ScrapeResponse>;
|
|
70
72
|
/**
|
|
71
73
|
* Searches for a query using the Firecrawl API.
|
|
72
74
|
* @param {string} query - The query to search for.
|