0
v-firecrawl/apps/js-sdk/firecrawl/src/index.ts

340 lines
10 KiB
TypeScript
Raw Normal View History

2024-05-08 19:38:49 -04:00
import axios, { AxiosResponse, AxiosRequestHeaders } from "axios";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
2024-04-19 13:49:35 -04:00
/**
* Configuration interface for FirecrawlApp.
*/
export interface FirecrawlAppConfig {
2024-04-16 13:02:16 -04:00
apiKey?: string | null;
}
2024-04-19 13:49:35 -04:00
/**
* Generic parameter interface.
*/
export interface Params {
2024-04-16 13:02:16 -04:00
[key: string]: any;
2024-05-08 19:38:49 -04:00
extractorOptions?: {
extractionSchema: z.ZodSchema | any;
mode?: "llm-extraction";
extractionPrompt?: string;
};
2024-04-16 13:02:16 -04:00
}
2024-04-19 13:49:35 -04:00
/**
* Response interface for scraping operations.
*/
export interface ScrapeResponse {
success: boolean;
data?: any;
error?: string;
}
2024-04-25 15:49:10 -04:00
/**
* Response interface for searching operations.
*/
export interface SearchResponse {
success: boolean;
data?: any;
error?: string;
}
2024-04-19 13:49:35 -04:00
/**
* Response interface for crawling operations.
*/
export interface CrawlResponse {
success: boolean;
jobId?: string;
data?: any;
error?: string;
}
/**
* Response interface for job status checks.
*/
export interface JobStatusResponse {
success: boolean;
status: string;
jobId?: string;
data?: any;
error?: string;
}
/**
* Main class for interacting with the Firecrawl API.
*/
2024-04-16 13:02:16 -04:00
export default class FirecrawlApp {
private apiKey: string;
2024-04-19 13:49:35 -04:00
/**
* Initializes a new instance of the FirecrawlApp class.
* @param {FirecrawlAppConfig} config - Configuration options for the FirecrawlApp instance.
*/
2024-04-16 13:02:16 -04:00
constructor({ apiKey = null }: FirecrawlAppConfig) {
2024-05-08 19:38:49 -04:00
this.apiKey = apiKey || "";
2024-04-16 13:02:16 -04:00
if (!this.apiKey) {
2024-05-08 19:38:49 -04:00
throw new Error("No API key provided");
2024-04-16 13:02:16 -04:00
}
}
2024-04-19 13:49:35 -04:00
/**
* Scrapes a URL using the Firecrawl API.
* @param {string} url - The URL to scrape.
* @param {Params | null} params - Additional parameters for the scrape request.
* @returns {Promise<ScrapeResponse>} The response from the scrape operation.
*/
2024-05-08 19:38:49 -04:00
async scrapeUrl(
url: string,
params: Params | null = null
): Promise<ScrapeResponse> {
2024-04-16 13:02:16 -04:00
const headers: AxiosRequestHeaders = {
2024-05-08 19:38:49 -04:00
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
2024-04-16 13:02:16 -04:00
} as AxiosRequestHeaders;
2024-05-08 19:38:49 -04:00
let jsonData: Params = { url, ...params };
if (params?.extractorOptions?.extractionSchema) {
let schema = params.extractorOptions.extractionSchema;
// Check if schema is an instance of ZodSchema to correctly identify Zod schemas
if (schema instanceof z.ZodSchema) {
schema = zodToJsonSchema(schema);
}
2024-05-08 19:38:49 -04:00
jsonData = {
...jsonData,
extractorOptions: {
...params.extractorOptions,
extractionSchema: schema,
mode: params.extractorOptions.mode || "llm-extraction",
},
};
2024-04-16 13:02:16 -04:00
}
try {
2024-05-08 19:38:49 -04:00
const response: AxiosResponse = await axios.post(
"https://api.firecrawl.dev/v0/scrape",
jsonData,
2024-05-13 21:34:00 -04:00
{ headers },
2024-05-08 19:38:49 -04:00
);
2024-04-16 13:02:16 -04:00
if (response.status === 200) {
const responseData = response.data;
if (responseData.success) {
2024-05-08 19:38:49 -04:00
return responseData;
2024-04-16 13:02:16 -04:00
} else {
throw new Error(`Failed to scrape URL. Error: ${responseData.error}`);
}
} else {
2024-05-08 19:38:49 -04:00
this.handleError(response, "scrape URL");
2024-04-16 13:02:16 -04:00
}
} catch (error: any) {
throw new Error(error.message);
}
2024-05-08 19:38:49 -04:00
return { success: false, error: "Internal server error." };
2024-04-16 13:02:16 -04:00
}
2024-04-25 15:49:10 -04:00
/**
* Searches for a query using the Firecrawl API.
* @param {string} query - The query to search for.
* @param {Params | null} params - Additional parameters for the search request.
* @returns {Promise<SearchResponse>} The response from the search operation.
*/
2024-05-08 19:38:49 -04:00
async search(
query: string,
params: Params | null = null
): Promise<SearchResponse> {
2024-04-25 15:49:10 -04:00
const headers: AxiosRequestHeaders = {
2024-05-08 19:38:49 -04:00
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
2024-04-25 15:49:10 -04:00
} as AxiosRequestHeaders;
let jsonData: Params = { query };
if (params) {
jsonData = { ...jsonData, ...params };
}
try {
2024-05-08 19:38:49 -04:00
const response: AxiosResponse = await axios.post(
"https://api.firecrawl.dev/v0/search",
jsonData,
{ headers }
);
2024-04-25 15:49:10 -04:00
if (response.status === 200) {
const responseData = response.data;
if (responseData.success) {
2024-05-08 19:38:49 -04:00
return responseData;
2024-04-25 15:49:10 -04:00
} else {
throw new Error(`Failed to search. Error: ${responseData.error}`);
}
} else {
2024-05-08 19:38:49 -04:00
this.handleError(response, "search");
2024-04-25 15:49:10 -04:00
}
} catch (error: any) {
throw new Error(error.message);
}
2024-05-08 19:38:49 -04:00
return { success: false, error: "Internal server error." };
2024-04-25 15:49:10 -04:00
}
2024-04-19 13:49:35 -04:00
/**
* Initiates a crawl job for a URL using the Firecrawl API.
* @param {string} url - The URL to crawl.
* @param {Params | null} params - Additional parameters for the crawl request.
* @param {boolean} waitUntilDone - Whether to wait for the crawl job to complete.
* @param {number} timeout - Timeout in seconds for job status checks.
2024-04-23 13:55:40 -04:00
* @returns {Promise<CrawlResponse | any>} The response from the crawl operation.
2024-04-19 13:49:35 -04:00
*/
2024-05-08 19:38:49 -04:00
async crawlUrl(
url: string,
params: Params | null = null,
waitUntilDone: boolean = true,
timeout: number = 2
): Promise<CrawlResponse | any> {
2024-04-16 13:02:16 -04:00
const headers = this.prepareHeaders();
let jsonData: Params = { url };
if (params) {
jsonData = { ...jsonData, ...params };
}
try {
2024-05-08 19:38:49 -04:00
const response: AxiosResponse = await this.postRequest(
"https://api.firecrawl.dev/v0/crawl",
jsonData,
headers
);
2024-04-16 13:02:16 -04:00
if (response.status === 200) {
const jobId: string = response.data.jobId;
if (waitUntilDone) {
return this.monitorJobStatus(jobId, headers, timeout);
} else {
2024-04-19 13:49:35 -04:00
return { success: true, jobId };
2024-04-16 13:02:16 -04:00
}
} else {
2024-05-08 19:38:49 -04:00
this.handleError(response, "start crawl job");
2024-04-16 13:02:16 -04:00
}
} catch (error: any) {
2024-05-08 19:38:49 -04:00
console.log(error);
2024-04-16 13:02:16 -04:00
throw new Error(error.message);
}
2024-05-08 19:38:49 -04:00
return { success: false, error: "Internal server error." };
2024-04-16 13:02:16 -04:00
}
2024-04-19 13:49:35 -04:00
/**
* Checks the status of a crawl job using the Firecrawl API.
* @param {string} jobId - The job ID of the crawl operation.
* @returns {Promise<JobStatusResponse>} The response containing the job status.
*/
async checkCrawlStatus(jobId: string): Promise<JobStatusResponse> {
2024-04-16 13:02:16 -04:00
const headers: AxiosRequestHeaders = this.prepareHeaders();
try {
2024-05-08 19:38:49 -04:00
const response: AxiosResponse = await this.getRequest(
`https://api.firecrawl.dev/v0/crawl/status/${jobId}`,
headers
);
2024-04-16 13:02:16 -04:00
if (response.status === 200) {
return response.data;
} else {
2024-05-08 19:38:49 -04:00
this.handleError(response, "check crawl status");
2024-04-16 13:02:16 -04:00
}
} catch (error: any) {
throw new Error(error.message);
}
2024-05-08 19:38:49 -04:00
return {
success: false,
status: "unknown",
error: "Internal server error.",
};
2024-04-16 13:02:16 -04:00
}
2024-04-19 13:49:35 -04:00
/**
* Prepares the headers for an API request.
* @returns {AxiosRequestHeaders} The prepared headers.
*/
2024-04-16 13:02:16 -04:00
prepareHeaders(): AxiosRequestHeaders {
return {
2024-05-08 19:38:49 -04:00
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
2024-04-16 13:02:16 -04:00
} as AxiosRequestHeaders;
}
2024-04-19 13:49:35 -04:00
/**
* Sends a POST request to the specified URL.
* @param {string} url - The URL to send the request to.
* @param {Params} data - The data to send in the request.
* @param {AxiosRequestHeaders} headers - The headers for the request.
* @returns {Promise<AxiosResponse>} The response from the POST request.
*/
2024-05-08 19:38:49 -04:00
postRequest(
url: string,
data: Params,
headers: AxiosRequestHeaders
): Promise<AxiosResponse> {
2024-04-16 13:02:16 -04:00
return axios.post(url, data, { headers });
}
2024-04-19 13:49:35 -04:00
/**
* Sends a GET request to the specified URL.
* @param {string} url - The URL to send the request to.
* @param {AxiosRequestHeaders} headers - The headers for the request.
* @returns {Promise<AxiosResponse>} The response from the GET request.
*/
2024-05-08 19:38:49 -04:00
getRequest(
url: string,
headers: AxiosRequestHeaders
): Promise<AxiosResponse> {
2024-04-16 13:02:16 -04:00
return axios.get(url, { headers });
}
2024-04-19 13:49:35 -04:00
/**
* Monitors the status of a crawl job until completion or failure.
* @param {string} jobId - The job ID of the crawl operation.
* @param {AxiosRequestHeaders} headers - The headers for the request.
* @param {number} timeout - Timeout in seconds for job status checks.
* @returns {Promise<any>} The final job status or data.
*/
2024-05-08 19:38:49 -04:00
async monitorJobStatus(
jobId: string,
headers: AxiosRequestHeaders,
timeout: number
): Promise<any> {
2024-04-16 13:02:16 -04:00
while (true) {
2024-05-08 19:38:49 -04:00
const statusResponse: AxiosResponse = await this.getRequest(
`https://api.firecrawl.dev/v0/crawl/status/${jobId}`,
headers
);
2024-04-16 13:02:16 -04:00
if (statusResponse.status === 200) {
const statusData = statusResponse.data;
2024-05-08 19:38:49 -04:00
if (statusData.status === "completed") {
if ("data" in statusData) {
2024-04-16 13:02:16 -04:00
return statusData.data;
} else {
2024-05-08 19:38:49 -04:00
throw new Error("Crawl job completed but no data was returned");
2024-04-16 13:02:16 -04:00
}
2024-05-08 19:38:49 -04:00
} else if (
["active", "paused", "pending", "queued"].includes(statusData.status)
) {
2024-04-16 13:02:16 -04:00
if (timeout < 2) {
timeout = 2;
}
2024-05-08 19:38:49 -04:00
await new Promise((resolve) => setTimeout(resolve, timeout * 1000)); // Wait for the specified timeout before checking again
2024-04-16 13:02:16 -04:00
} else {
2024-05-08 19:38:49 -04:00
throw new Error(
`Crawl job failed or was stopped. Status: ${statusData.status}`
);
2024-04-16 13:02:16 -04:00
}
} else {
2024-05-08 19:38:49 -04:00
this.handleError(statusResponse, "check crawl status");
2024-04-16 13:02:16 -04:00
}
}
}
2024-04-19 13:49:35 -04:00
/**
* Handles errors from API responses.
* @param {AxiosResponse} response - The response from the API.
* @param {string} action - The action being performed when the error occurred.
*/
2024-04-16 13:02:16 -04:00
handleError(response: AxiosResponse, action: string): void {
2024-05-13 21:34:00 -04:00
if ([402, 408, 409, 500].includes(response.status)) {
2024-05-08 19:38:49 -04:00
const errorMessage: string =
response.data.error || "Unknown error occurred";
throw new Error(
`Failed to ${action}. Status code: ${response.status}. Error: ${errorMessage}`
);
2024-04-16 13:02:16 -04:00
} else {
2024-05-08 19:38:49 -04:00
throw new Error(
`Unexpected error occurred while trying to ${action}. Status code: ${response.status}`
);
2024-04-16 13:02:16 -04:00
}
}
2024-04-25 12:33:06 -04:00
}