0

Update index.ts

This commit is contained in:
Nicolas 2024-06-18 12:56:13 -04:00
parent 26e8bfc23a
commit 90a807c547

View File

@ -129,7 +129,7 @@ export default class FirecrawlApp {
*/ */
constructor({ apiKey = null, apiUrl = null }: FirecrawlAppConfig) { constructor({ apiKey = null, apiUrl = null }: FirecrawlAppConfig) {
this.apiKey = apiKey || ""; this.apiKey = apiKey || "";
this.apiUrl = apiUrl || "https://api.firecrawl.dev" this.apiUrl = apiUrl || "https://api.firecrawl.dev";
if (!this.apiKey) { if (!this.apiKey) {
throw new Error("No API key provided"); throw new Error("No API key provided");
} }
@ -169,7 +169,7 @@ export default class FirecrawlApp {
const response: AxiosResponse = await axios.post( const response: AxiosResponse = await axios.post(
this.apiUrl + "/v0/scrape", this.apiUrl + "/v0/scrape",
jsonData, jsonData,
{ headers }, { headers }
); );
if (response.status === 200) { if (response.status === 200) {
const responseData = response.data; const responseData = response.data;
@ -288,7 +288,9 @@ export default class FirecrawlApp {
success: true, success: true,
status: response.data.status, status: response.data.status,
data: response.data.data, data: response.data.data,
partial_data: !response.data.data ? response.data.partial_data : undefined, partial_data: !response.data.data
? response.data.partial_data
: undefined,
}; };
} else { } else {
this.handleError(response, "check crawl status"); this.handleError(response, "check crawl status");
@ -309,10 +311,10 @@ export default class FirecrawlApp {
*/ */
prepareHeaders(idempotencyKey?: string): AxiosRequestHeaders { prepareHeaders(idempotencyKey?: string): AxiosRequestHeaders {
return { return {
'Content-Type': 'application/json', "Content-Type": "application/json",
'Authorization': `Bearer ${this.apiKey}`, Authorization: `Bearer ${this.apiKey}`,
...(idempotencyKey ? { 'x-idempotency-key': idempotencyKey } : {}), ...(idempotencyKey ? { "x-idempotency-key": idempotencyKey } : {}),
} as AxiosRequestHeaders & { 'x-idempotency-key'?: string }; } as AxiosRequestHeaders & { "x-idempotency-key"?: string };
} }
/** /**
@ -374,7 +376,9 @@ export default class FirecrawlApp {
if (checkInterval < 2) { if (checkInterval < 2) {
checkInterval = 2; checkInterval = 2;
} }
await new Promise((resolve) => setTimeout(resolve, checkInterval * 1000)); // Wait for the specified timeout before checking again await new Promise((resolve) =>
setTimeout(resolve, checkInterval * 1000)
); // Wait for the specified timeout before checking again
} else { } else {
throw new Error( throw new Error(
`Crawl job failed or was stopped. Status: ${statusData.status}` `Crawl job failed or was stopped. Status: ${statusData.status}`