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