removed v0 support
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mendable/firecrawl-js",
|
"name": "@mendable/firecrawl-js",
|
||||||
"version": "1.0.4",
|
"version": "1.1.4",
|
||||||
"description": "JavaScript SDK for Firecrawl API",
|
"description": "JavaScript SDK for Firecrawl API",
|
||||||
"main": "build/cjs/index.js",
|
"main": "build/cjs/index.js",
|
||||||
"types": "types/index.d.ts",
|
"types": "types/index.d.ts",
|
||||||
|
|||||||
@@ -8,12 +8,10 @@ import { TypedEventTarget } from "typescript-event-target";
|
|||||||
* Configuration interface for FirecrawlApp.
|
* Configuration interface for FirecrawlApp.
|
||||||
* @param apiKey - Optional API key for authentication.
|
* @param apiKey - Optional API key for authentication.
|
||||||
* @param apiUrl - Optional base URL of the API; defaults to 'https://api.firecrawl.dev'.
|
* @param apiUrl - Optional base URL of the API; defaults to 'https://api.firecrawl.dev'.
|
||||||
* @param version - API version, either 'v0' or 'v1'.
|
|
||||||
*/
|
*/
|
||||||
export interface FirecrawlAppConfig {
|
export interface FirecrawlAppConfig {
|
||||||
apiKey?: string | null;
|
apiKey?: string | null;
|
||||||
apiUrl?: string | null;
|
apiUrl?: string | null;
|
||||||
version?: "v0" | "v1";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,17 +54,6 @@ export interface FirecrawlDocumentMetadata {
|
|||||||
[key: string]: any; // Allows for additional metadata properties not explicitly defined.
|
[key: string]: any; // Allows for additional metadata properties not explicitly defined.
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Metadata for a Firecrawl document on v0.
|
|
||||||
* Similar to FirecrawlDocumentMetadata but includes properties specific to API version v0.
|
|
||||||
*/
|
|
||||||
export interface FirecrawlDocumentMetadataV0 {
|
|
||||||
// Similar properties as FirecrawlDocumentMetadata with additional v0 specific adjustments
|
|
||||||
pageStatusCode?: number;
|
|
||||||
pageError?: string;
|
|
||||||
[key: string]: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Document interface for Firecrawl.
|
* Document interface for Firecrawl.
|
||||||
* Represents a document retrieved or processed by Firecrawl.
|
* Represents a document retrieved or processed by Firecrawl.
|
||||||
@@ -78,28 +65,7 @@ export interface FirecrawlDocument {
|
|||||||
rawHtml?: string;
|
rawHtml?: string;
|
||||||
links?: string[];
|
links?: string[];
|
||||||
screenshot?: string;
|
screenshot?: string;
|
||||||
metadata: FirecrawlDocumentMetadata;
|
metadata?: FirecrawlDocumentMetadata;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Document interface for Firecrawl on v0.
|
|
||||||
* Represents a document specifically for API version v0 with additional properties.
|
|
||||||
*/
|
|
||||||
export interface FirecrawlDocumentV0 {
|
|
||||||
id?: string;
|
|
||||||
url?: string;
|
|
||||||
content: string;
|
|
||||||
markdown?: string;
|
|
||||||
html?: string;
|
|
||||||
llm_extraction?: Record<string, any>;
|
|
||||||
createdAt?: Date;
|
|
||||||
updatedAt?: Date;
|
|
||||||
type?: string;
|
|
||||||
metadata: FirecrawlDocumentMetadataV0;
|
|
||||||
childrenLinks?: string[];
|
|
||||||
provider?: string;
|
|
||||||
warning?: string;
|
|
||||||
index?: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,61 +73,25 @@ export interface FirecrawlDocumentV0 {
|
|||||||
* Defines the options and configurations available for scraping web content.
|
* Defines the options and configurations available for scraping web content.
|
||||||
*/
|
*/
|
||||||
export interface ScrapeParams {
|
export interface ScrapeParams {
|
||||||
formats: ("markdown" | "html" | "rawHtml" | "content" | "links" | "screenshot")[];
|
formats: ("markdown" | "html" | "rawHtml" | "content" | "links" | "screenshot" | "full@scrennshot")[];
|
||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
includeTags?: string[];
|
includeTags?: string[];
|
||||||
excludeTags?: string[];
|
excludeTags?: string[];
|
||||||
onlyMainContent?: boolean;
|
onlyMainContent?: boolean;
|
||||||
screenshotMode?: "desktop" | "full-desktop" | "mobile" | "full-mobile";
|
|
||||||
waitFor?: number;
|
waitFor?: number;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parameters for scraping operations on v0.
|
|
||||||
* Includes page and extractor options specific to API version v0.
|
|
||||||
*/
|
|
||||||
export interface ScrapeParamsV0 {
|
|
||||||
pageOptions?: {
|
|
||||||
headers?: Record<string, string>;
|
|
||||||
includeHtml?: boolean;
|
|
||||||
includeRawHtml?: boolean;
|
|
||||||
onlyIncludeTags?: string[];
|
|
||||||
onlyMainContent?: boolean;
|
|
||||||
removeTags?: string[];
|
|
||||||
replaceAllPathsWithAbsolutePaths?: boolean;
|
|
||||||
screenshot?: boolean;
|
|
||||||
fullPageScreenshot?: boolean;
|
|
||||||
waitFor?: number;
|
|
||||||
};
|
|
||||||
extractorOptions?: {
|
|
||||||
mode?: "markdown" | "llm-extraction" | "llm-extraction-from-raw-html" | "llm-extraction-from-markdown";
|
|
||||||
extractionPrompt?: string;
|
|
||||||
extractionSchema?: Record<string, any> | z.ZodSchema | any;
|
|
||||||
};
|
|
||||||
timeout?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Response interface for scraping operations.
|
* Response interface for scraping operations.
|
||||||
* Defines the structure of the response received after a scraping operation.
|
* Defines the structure of the response received after a scraping operation.
|
||||||
*/
|
*/
|
||||||
export interface ScrapeResponse extends FirecrawlDocument {
|
export interface ScrapeResponse extends FirecrawlDocument {
|
||||||
success: boolean;
|
success: true;
|
||||||
warning?: string;
|
warning?: string;
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Response interface for scraping operations on v0.
|
|
||||||
* Similar to ScrapeResponse but tailored for responses from API version v0.
|
|
||||||
*/
|
|
||||||
export interface ScrapeResponseV0 {
|
|
||||||
success: boolean;
|
|
||||||
data?: FirecrawlDocumentV0;
|
|
||||||
error?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameters for crawling operations.
|
* Parameters for crawling operations.
|
||||||
* Includes options for both scraping and mapping during a crawl.
|
* Includes options for both scraping and mapping during a crawl.
|
||||||
@@ -177,37 +107,6 @@ export interface CrawlParams {
|
|||||||
scrapeOptions?: ScrapeParams;
|
scrapeOptions?: ScrapeParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parameters for crawling operations on v0.
|
|
||||||
* Tailored for API version v0, includes specific options for crawling.
|
|
||||||
*/
|
|
||||||
export interface CrawlParamsV0 {
|
|
||||||
crawlerOptions?: {
|
|
||||||
includes?: string[];
|
|
||||||
excludes?: string[];
|
|
||||||
generateImgAltText?: boolean;
|
|
||||||
returnOnlyUrls?: boolean;
|
|
||||||
maxDepth?: number;
|
|
||||||
mode?: "default" | "fast";
|
|
||||||
ignoreSitemap?: boolean;
|
|
||||||
limit?: number;
|
|
||||||
allowBackwardCrawling?: boolean;
|
|
||||||
allowExternalContentLinks?: boolean;
|
|
||||||
};
|
|
||||||
pageOptions?: {
|
|
||||||
headers?: Record<string, string>;
|
|
||||||
includeHtml?: boolean;
|
|
||||||
includeRawHtml?: boolean;
|
|
||||||
onlyIncludeTags?: string[];
|
|
||||||
onlyMainContent?: boolean;
|
|
||||||
removeTags?: string[];
|
|
||||||
replaceAllPathsWithAbsolutePaths?: boolean;
|
|
||||||
screenshot?: boolean;
|
|
||||||
fullPageScreenshot?: boolean;
|
|
||||||
waitFor?: number;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Response interface for crawling operations.
|
* Response interface for crawling operations.
|
||||||
* Defines the structure of the response received after initiating a crawl.
|
* Defines the structure of the response received after initiating a crawl.
|
||||||
@@ -215,17 +114,7 @@ export interface CrawlParamsV0 {
|
|||||||
export interface CrawlResponse {
|
export interface CrawlResponse {
|
||||||
id?: string;
|
id?: string;
|
||||||
url?: string;
|
url?: string;
|
||||||
success: boolean;
|
success: true;
|
||||||
error?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response interface for crawling operations on v0.
|
|
||||||
* Similar to CrawlResponse but tailored for responses from API version v0.
|
|
||||||
*/
|
|
||||||
export interface CrawlResponseV0 {
|
|
||||||
jobId?: string;
|
|
||||||
success: boolean;
|
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,7 +123,7 @@ export interface CrawlResponseV0 {
|
|||||||
* Provides detailed status of a crawl job including progress and results.
|
* Provides detailed status of a crawl job including progress and results.
|
||||||
*/
|
*/
|
||||||
export interface CrawlStatusResponse {
|
export interface CrawlStatusResponse {
|
||||||
success: boolean;
|
success: true;
|
||||||
total: number;
|
total: number;
|
||||||
completed: number;
|
completed: number;
|
||||||
creditsUsed: number;
|
creditsUsed: number;
|
||||||
@@ -245,23 +134,6 @@ export interface CrawlStatusResponse {
|
|||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Response interface for job status checks on v0.
|
|
||||||
* Tailored for API version v0, provides status and partial data of a crawl job.
|
|
||||||
*/
|
|
||||||
export interface CrawlStatusResponseV0 {
|
|
||||||
success: boolean;
|
|
||||||
status: string;
|
|
||||||
current?: number;
|
|
||||||
current_url?: string;
|
|
||||||
current_step?: string;
|
|
||||||
total?: number;
|
|
||||||
data?: FirecrawlDocumentV0[];
|
|
||||||
partial_data?: FirecrawlDocumentV0[];
|
|
||||||
error?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameters for mapping operations.
|
* Parameters for mapping operations.
|
||||||
* Defines options for mapping URLs during a crawl.
|
* Defines options for mapping URLs during a crawl.
|
||||||
@@ -278,57 +150,35 @@ export interface MapParams {
|
|||||||
* Defines the structure of the response received after a mapping operation.
|
* Defines the structure of the response received after a mapping operation.
|
||||||
*/
|
*/
|
||||||
export interface MapResponse {
|
export interface MapResponse {
|
||||||
success: boolean;
|
success: true;
|
||||||
links?: string[];
|
links?: string[];
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameters for searching operations on v0.
|
* Error response interface.
|
||||||
* Tailored for API version v0, includes specific options for searching content.
|
* Defines the structure of the response received when an error occurs.
|
||||||
*/
|
*/
|
||||||
export interface SearchParamsV0 {
|
export interface ErrorResponse {
|
||||||
pageOptions?: {
|
success: false;
|
||||||
onlyMainContent?: boolean;
|
error: string;
|
||||||
fetchPageContent?: boolean;
|
|
||||||
includeHtml?: boolean;
|
|
||||||
includeRawHtml?: boolean;
|
|
||||||
};
|
|
||||||
searchOptions?: {
|
|
||||||
limit?: number;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response interface for searching operations on v0.
|
|
||||||
* Defines the structure of the response received after a search operation on v0.
|
|
||||||
*/
|
|
||||||
export interface SearchResponseV0 {
|
|
||||||
success: boolean;
|
|
||||||
data?: FirecrawlDocumentV0[];
|
|
||||||
error?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main class for interacting with the Firecrawl API.
|
* Main class for interacting with the Firecrawl API.
|
||||||
* Provides methods for scraping, searching, crawling, and mapping web content.
|
* Provides methods for scraping, searching, crawling, and mapping web content.
|
||||||
*/
|
*/
|
||||||
export default class FirecrawlApp<T extends "v0" | "v1"> {
|
export default class FirecrawlApp {
|
||||||
public apiKey: string;
|
public apiKey: string;
|
||||||
public apiUrl: string;
|
public apiUrl: string;
|
||||||
public version: T;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new instance of the FirecrawlApp class.
|
* Initializes a new instance of the FirecrawlApp class.
|
||||||
* @param config - Configuration options for the FirecrawlApp instance.
|
* @param config - Configuration options for the FirecrawlApp instance.
|
||||||
*/
|
*/
|
||||||
constructor({ apiKey = null, apiUrl = null, version = "v1" }: 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";
|
||||||
this.version = version as T;
|
|
||||||
if (!this.apiKey) {
|
|
||||||
throw new Error("No API key provided");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -339,8 +189,8 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
*/
|
*/
|
||||||
async scrapeUrl(
|
async scrapeUrl(
|
||||||
url: string,
|
url: string,
|
||||||
params?: ScrapeParams | ScrapeParamsV0
|
params?: ScrapeParams
|
||||||
): Promise<this['version'] extends 'v0' ? ScrapeResponseV0 : ScrapeResponse> {
|
): Promise<ScrapeResponse | ErrorResponse> {
|
||||||
const headers: AxiosRequestHeaders = {
|
const headers: AxiosRequestHeaders = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${this.apiKey}`,
|
Authorization: `Bearer ${this.apiKey}`,
|
||||||
@@ -363,19 +213,19 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const response: AxiosResponse = await axios.post(
|
const response: AxiosResponse = await axios.post(
|
||||||
this.apiUrl + `/${this.version}/scrape`,
|
this.apiUrl + `/v1/scrape`,
|
||||||
jsonData,
|
jsonData,
|
||||||
{ headers }
|
{ headers }
|
||||||
);
|
);
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
const responseData = response.data;
|
const responseData = response.data;
|
||||||
if (responseData.success) {
|
if (responseData.success) {
|
||||||
return (this.version === 'v0' ? responseData as ScrapeResponseV0 : {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
warning: responseData.warning,
|
warning: responseData.warning,
|
||||||
error: responseData.error,
|
error: responseData.error,
|
||||||
...responseData.data
|
...responseData.data
|
||||||
}) as ScrapeResponse;
|
};
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Failed to scrape URL. Error: ${responseData.error}`);
|
throw new Error(`Failed to scrape URL. Error: ${responseData.error}`);
|
||||||
}
|
}
|
||||||
@@ -385,100 +235,47 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
throw new Error(error.message);
|
throw new Error(error.message);
|
||||||
}
|
}
|
||||||
return { success: false, error: "Internal server error." } as this['version'] extends 'v0' ? ScrapeResponseV0 : ScrapeResponse;
|
return { success: false, error: "Internal server error." };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches for a query using the Firecrawl API.
|
* This method is intended to search for a query using the Firecrawl API. However, it is not supported in version 1 of the API.
|
||||||
* @param query - The query to search for.
|
* @param query - The search query string.
|
||||||
* @param params - Additional parameters for the search request.
|
* @param params - Additional parameters for the search.
|
||||||
* @returns The response from the search operation.
|
* @returns Throws an error advising to use version 0 of the API.
|
||||||
*/
|
*/
|
||||||
async search(
|
async search(
|
||||||
query: string,
|
query: string,
|
||||||
params?: SearchParamsV0
|
params?: any
|
||||||
): Promise<SearchResponseV0> {
|
): Promise<any> {
|
||||||
if (this.version === "v1") {
|
|
||||||
throw new Error("Search is not supported in v1, please update FirecrawlApp() initialization to use v0.");
|
throw new Error("Search is not supported in v1, please update FirecrawlApp() initialization to use v0.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const headers: AxiosRequestHeaders = {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: `Bearer ${this.apiKey}`,
|
|
||||||
} as AxiosRequestHeaders;
|
|
||||||
let jsonData: any = { query };
|
|
||||||
if (params) {
|
|
||||||
jsonData = { ...jsonData, ...params };
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const response: AxiosResponse = await axios.post(
|
|
||||||
this.apiUrl + "/v0/search",
|
|
||||||
jsonData,
|
|
||||||
{ headers }
|
|
||||||
);
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseData = response.data;
|
|
||||||
if (responseData.success) {
|
|
||||||
return responseData;
|
|
||||||
} else {
|
|
||||||
throw new Error(`Failed to search. Error: ${responseData.error}`);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.handleError(response, "search");
|
|
||||||
}
|
|
||||||
} catch (error: any) {
|
|
||||||
throw new Error(error.message);
|
|
||||||
}
|
|
||||||
return { success: false, error: "Internal server error." };
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initiates a crawl job for a URL using the Firecrawl API.
|
* Initiates a crawl job for a URL using the Firecrawl API.
|
||||||
* @param url - The URL to crawl.
|
* @param url - The URL to crawl.
|
||||||
* @param params - Additional parameters for the crawl request.
|
* @param params - Additional parameters for the crawl request.
|
||||||
* @param waitUntilDone - Whether to wait for the crawl job to complete.
|
|
||||||
* @param pollInterval - Time in seconds for job status checks.
|
* @param pollInterval - Time in seconds for job status checks.
|
||||||
* @param idempotencyKey - Optional idempotency key for the request.
|
* @param idempotencyKey - Optional idempotency key for the request.
|
||||||
* @returns The response from the crawl operation.
|
* @returns The response from the crawl operation.
|
||||||
*/
|
*/
|
||||||
async crawlUrl(
|
async crawlUrl(
|
||||||
url: string,
|
url: string,
|
||||||
params?: this['version'] extends 'v0' ? CrawlParamsV0 : CrawlParams,
|
params?: CrawlParams,
|
||||||
waitUntilDone: boolean = true,
|
|
||||||
pollInterval: number = 2,
|
pollInterval: number = 2,
|
||||||
idempotencyKey?: string
|
idempotencyKey?: string
|
||||||
): Promise<
|
): Promise<CrawlStatusResponse | ErrorResponse> {
|
||||||
this['version'] extends 'v0'
|
|
||||||
? CrawlResponseV0 | CrawlStatusResponseV0 | FirecrawlDocumentV0[]
|
|
||||||
: CrawlResponse | CrawlStatusResponse
|
|
||||||
> {
|
|
||||||
const headers = this.prepareHeaders(idempotencyKey);
|
const headers = this.prepareHeaders(idempotencyKey);
|
||||||
let jsonData: any = { url, ...params };
|
let jsonData: any = { url, ...params };
|
||||||
try {
|
try {
|
||||||
const response: AxiosResponse = await this.postRequest(
|
const response: AxiosResponse = await this.postRequest(
|
||||||
this.apiUrl + `/${this.version}/crawl`,
|
this.apiUrl + `/v1/crawl`,
|
||||||
jsonData,
|
jsonData,
|
||||||
headers
|
headers
|
||||||
);
|
);
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
const id: string = this.version === 'v0' ? response.data.jobId : response.data.id;
|
const id: string = response.data.id;
|
||||||
let checkUrl: string | undefined = undefined;
|
return this.monitorJobStatus(id, headers, pollInterval);
|
||||||
if (waitUntilDone) {
|
|
||||||
if (this.version === 'v1') { checkUrl = response.data.url }
|
|
||||||
return this.monitorJobStatus(id, headers, pollInterval, checkUrl);
|
|
||||||
} else {
|
|
||||||
if (this.version === 'v0') {
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
jobId: id
|
|
||||||
} as CrawlResponseV0;
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
id: id
|
|
||||||
} as CrawlResponse;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.handleError(response, "start crawl job");
|
this.handleError(response, "start crawl job");
|
||||||
}
|
}
|
||||||
@@ -489,7 +286,35 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
throw new Error(error.message);
|
throw new Error(error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { success: false, error: "Internal server error." } as this['version'] extends 'v0' ? CrawlResponseV0 : CrawlResponse;
|
return { success: false, error: "Internal server error." };
|
||||||
|
}
|
||||||
|
|
||||||
|
async asyncCrawlUrl(
|
||||||
|
url: string,
|
||||||
|
params?: CrawlParams,
|
||||||
|
idempotencyKey?: string
|
||||||
|
): Promise<CrawlResponse | ErrorResponse> {
|
||||||
|
const headers = this.prepareHeaders(idempotencyKey);
|
||||||
|
let jsonData: any = { url, ...params };
|
||||||
|
try {
|
||||||
|
const response: AxiosResponse = await this.postRequest(
|
||||||
|
this.apiUrl + `/v1/crawl`,
|
||||||
|
jsonData,
|
||||||
|
headers
|
||||||
|
);
|
||||||
|
if (response.status === 200) {
|
||||||
|
return response.data;
|
||||||
|
} else {
|
||||||
|
this.handleError(response, "start crawl job");
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.response?.data?.error) {
|
||||||
|
throw new Error(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`);
|
||||||
|
} else {
|
||||||
|
throw new Error(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { success: false, error: "Internal server error." };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -497,7 +322,7 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
* @param id - The ID of the crawl operation.
|
* @param id - The ID of the crawl operation.
|
||||||
* @returns The response containing the job status.
|
* @returns The response containing the job status.
|
||||||
*/
|
*/
|
||||||
async checkCrawlStatus(id?: string): Promise<this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse> {
|
async checkCrawlStatus(id?: string): Promise<CrawlStatusResponse | ErrorResponse> {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
throw new Error("No crawl ID provided");
|
throw new Error("No crawl ID provided");
|
||||||
}
|
}
|
||||||
@@ -505,26 +330,10 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
const headers: AxiosRequestHeaders = this.prepareHeaders();
|
const headers: AxiosRequestHeaders = this.prepareHeaders();
|
||||||
try {
|
try {
|
||||||
const response: AxiosResponse = await this.getRequest(
|
const response: AxiosResponse = await this.getRequest(
|
||||||
this.version === 'v1' ?
|
`${this.apiUrl}/v1/crawl/${id}`,
|
||||||
`${this.apiUrl}/${this.version}/crawl/${id}` :
|
|
||||||
`${this.apiUrl}/${this.version}/crawl/status/${id}`,
|
|
||||||
headers
|
headers
|
||||||
);
|
);
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
if (this.version === 'v0') {
|
|
||||||
return ({
|
|
||||||
success: true,
|
|
||||||
status: response.data.status,
|
|
||||||
current: response.data.current,
|
|
||||||
current_url: response.data.current_url,
|
|
||||||
current_step: response.data.current_step,
|
|
||||||
total: response.data.total,
|
|
||||||
data: response.data.data,
|
|
||||||
partial_data: !response.data.data
|
|
||||||
? response.data.partial_data
|
|
||||||
: undefined,
|
|
||||||
} as CrawlStatusResponseV0) as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse;
|
|
||||||
} else {
|
|
||||||
return ({
|
return ({
|
||||||
success: true,
|
success: true,
|
||||||
status: response.data.status,
|
status: response.data.status,
|
||||||
@@ -535,56 +344,38 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
next: response.data.next,
|
next: response.data.next,
|
||||||
data: response.data.data,
|
data: response.data.data,
|
||||||
error: response.data.error
|
error: response.data.error
|
||||||
} as CrawlStatusResponse) as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse;
|
})
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.handleError(response, "check crawl status");
|
this.handleError(response, "check crawl status");
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
throw new Error(error.message);
|
throw new Error(error.message);
|
||||||
}
|
}
|
||||||
|
return { success: false, error: "Internal server error." };
|
||||||
return this.version === 'v0' ?
|
|
||||||
({
|
|
||||||
success: false,
|
|
||||||
status: "unknown",
|
|
||||||
current: 0,
|
|
||||||
current_url: "",
|
|
||||||
current_step: "",
|
|
||||||
total: 0,
|
|
||||||
error: "Internal server error.",
|
|
||||||
} as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse) :
|
|
||||||
({
|
|
||||||
success: false,
|
|
||||||
error: "Internal server error.",
|
|
||||||
} as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async crawlUrlAndWatch(
|
async crawlUrlAndWatch(
|
||||||
url: string,
|
url: string,
|
||||||
params?: this['version'] extends 'v0' ? CrawlParamsV0 : CrawlParams,
|
params?: CrawlParams,
|
||||||
idempotencyKey?: string,
|
idempotencyKey?: string,
|
||||||
) {
|
) {
|
||||||
if (this.version === 'v0') {
|
const crawl = await this.asyncCrawlUrl(url, params, idempotencyKey);
|
||||||
throw new Error("crawlUrlAndWatch is only available on v1");
|
|
||||||
|
if (crawl.success && crawl.id) {
|
||||||
|
const id = crawl.id;
|
||||||
|
return new CrawlWatcher(id, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
const crawl = await this.crawlUrl(url, params, false, 0, idempotencyKey);
|
throw new Error("Crawl job failed to start");
|
||||||
const id = this.version === 'v0' ? (crawl as CrawlResponseV0).jobId : (crawl as CrawlResponse).id;
|
|
||||||
|
|
||||||
return new CrawlWatcher(id as string, this as FirecrawlApp<"v1">);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async mapUrl(url: string, params?: MapParams): Promise<MapResponse> {
|
async mapUrl(url: string, params?: MapParams): Promise<MapResponse | ErrorResponse> {
|
||||||
if (this.version == 'v0') {
|
|
||||||
throw new Error("Map is not supported in v0");
|
|
||||||
}
|
|
||||||
const headers = this.prepareHeaders();
|
const headers = this.prepareHeaders();
|
||||||
let jsonData: { url: string } & MapParams = { url, ...params };
|
let jsonData: { url: string } & MapParams = { url, ...params };
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response: AxiosResponse = await this.postRequest(
|
const response: AxiosResponse = await this.postRequest(
|
||||||
this.apiUrl + `/${this.version}/map`,
|
this.apiUrl + `/v1/map`,
|
||||||
jsonData,
|
jsonData,
|
||||||
headers
|
headers
|
||||||
);
|
);
|
||||||
@@ -596,7 +387,7 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
throw new Error(error.message);
|
throw new Error(error.message);
|
||||||
}
|
}
|
||||||
return { success: false, error: "Internal server error." } as MapResponse;
|
return { success: false, error: "Internal server error." };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -651,25 +442,18 @@ export default class FirecrawlApp<T extends "v0" | "v1"> {
|
|||||||
async monitorJobStatus(
|
async monitorJobStatus(
|
||||||
id: string,
|
id: string,
|
||||||
headers: AxiosRequestHeaders,
|
headers: AxiosRequestHeaders,
|
||||||
checkInterval: number,
|
checkInterval: number
|
||||||
checkUrl?: string
|
): Promise<CrawlStatusResponse> {
|
||||||
): Promise<this['version'] extends 'v0' ? CrawlStatusResponseV0 | FirecrawlDocumentV0[] : CrawlStatusResponse> {
|
|
||||||
let apiUrl: string = '';
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (this.version === 'v1') {
|
|
||||||
apiUrl = checkUrl ?? `${this.apiUrl}/v1/crawl/${id}`;
|
|
||||||
} else if (this.version === 'v0') {
|
|
||||||
apiUrl = `${this.apiUrl}/v0/crawl/status/${id}`;
|
|
||||||
}
|
|
||||||
const statusResponse: AxiosResponse = await this.getRequest(
|
const statusResponse: AxiosResponse = await this.getRequest(
|
||||||
apiUrl,
|
`${this.apiUrl}/v1/crawl/${id}`,
|
||||||
headers
|
headers
|
||||||
);
|
);
|
||||||
if (statusResponse.status === 200) {
|
if (statusResponse.status === 200) {
|
||||||
const statusData = statusResponse.data;
|
const statusData = statusResponse.data;
|
||||||
if (statusData.status === "completed") {
|
if (statusData.status === "completed") {
|
||||||
if ("data" in statusData) {
|
if ("data" in statusData) {
|
||||||
return this.version === 'v0' ? statusData.data : statusData;
|
return statusData;
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Crawl job completed but no data was returned");
|
throw new Error("Crawl job completed but no data was returned");
|
||||||
}
|
}
|
||||||
@@ -729,7 +513,7 @@ export class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
|
|||||||
public data: FirecrawlDocument[];
|
public data: FirecrawlDocument[];
|
||||||
public status: CrawlStatusResponse["status"];
|
public status: CrawlStatusResponse["status"];
|
||||||
|
|
||||||
constructor(id: string, app: FirecrawlApp<"v1">) {
|
constructor(id: string, app: FirecrawlApp) {
|
||||||
super();
|
super();
|
||||||
this.ws = new WebSocket(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey);
|
this.ws = new WebSocket(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey);
|
||||||
this.status = "scraping";
|
this.status = "scraping";
|
||||||
|
|||||||
Reference in New Issue
Block a user