Update index.ts

This commit is contained in:
rafaelsideguide 2024-08-06 17:34:13 -03:00
parent d599d31e63
commit 3fb2307010

View File

@ -7,12 +7,52 @@ import { zodToJsonSchema } from "zod-to-json-schema";
export interface FirecrawlAppConfig { export interface FirecrawlAppConfig {
apiKey?: string | null; apiKey?: string | null;
apiUrl?: string | null; apiUrl?: string | null;
version?: "v0" | "v1";
} }
/** /**
* Metadata for a Firecrawl document. * Metadata for a Firecrawl document.
*/ */
export interface FirecrawlDocumentMetadata { export interface FirecrawlDocumentMetadata {
title?: string;
description?: string;
language?: string;
keywords?: string;
robots?: string;
ogTitle?: string;
ogDescription?: string;
ogUrl?: string;
ogImage?: string;
ogAudio?: string;
ogDeterminer?: string;
ogLocale?: string;
ogLocaleAlternate?: string[];
ogSiteName?: string;
ogVideo?: string;
dctermsCreated?: string;
dcDateCreated?: string;
dcDate?: string;
dctermsType?: string;
dcType?: string;
dctermsAudience?: string;
dctermsSubject?: string;
dcSubject?: string;
dcDescription?: string;
dctermsKeywords?: string;
modifiedTime?: string;
publishedTime?: string;
articleTag?: string;
articleSection?: string;
sourceURL?: string;
statusCode?: number;
error?: string;
[key: string]: any;
}
/**
* Metadata for a Firecrawl document on v0.
*/
export interface FirecrawlDocumentMetadataV0 {
title?: string; title?: string;
description?: string; description?: string;
language?: string; language?: string;
@ -52,6 +92,19 @@ export interface FirecrawlDocumentMetadata {
* Document interface for Firecrawl. * Document interface for Firecrawl.
*/ */
export interface FirecrawlDocument { export interface FirecrawlDocument {
url?: string;
content: string;
markdown?: string;
html?: string;
rawHtml?: string;
linksOnPage?: string[];
metadata: FirecrawlDocumentMetadata;
}
/**
* Document interface for Firecrawl on v0.
*/
export interface FirecrawlDocumentV0 {
id?: string; id?: string;
url?: string; url?: string;
content: string; content: string;
@ -61,11 +114,10 @@ export interface FirecrawlDocument {
createdAt?: Date; createdAt?: Date;
updatedAt?: Date; updatedAt?: Date;
type?: string; type?: string;
metadata: FirecrawlDocumentMetadata; metadata: FirecrawlDocumentMetadataV0;
childrenLinks?: string[]; childrenLinks?: string[];
provider?: string; provider?: string;
warning?: string; warning?: string;
index?: number; index?: number;
} }
@ -74,17 +126,29 @@ export interface FirecrawlDocument {
*/ */
export interface ScrapeResponse { export interface ScrapeResponse {
success: boolean; success: boolean;
warning?: string;
data?: FirecrawlDocument; data?: FirecrawlDocument;
error?: string; error?: string;
} }
/**
* Response interface for scraping operations on v0.
*/
export interface ScrapeResponseV0 {
success: boolean;
data?: FirecrawlDocumentV0;
error?: string;
}
/** /**
* Response interface for searching operations. * Response interface for searching operations.
*/ */
export interface SearchResponse { export interface SearchResponseV0 {
success: boolean; success: boolean;
data?: FirecrawlDocument[]; data?: FirecrawlDocument[];
error?: string; error?: string;
} }
/** /**
* Response interface for crawling operations. * Response interface for crawling operations.
*/ */
@ -94,21 +158,46 @@ export interface CrawlResponse {
data?: FirecrawlDocument[]; data?: FirecrawlDocument[];
error?: string; error?: string;
} }
/**
* Response interface for crawling operations on v0.
*/
export interface CrawlResponseV0 {
success: boolean;
jobId?: string;
data?: FirecrawlDocument[];
error?: string;
}
/** /**
* Response interface for job status checks. * Response interface for job status checks.
*/ */
export interface JobStatusResponse { export interface JobStatusResponse {
success: boolean;
totalCount: number;
creditsUsed: number;
expiresAt: Date;
status: "scraping" | "completed" | "failed";
next: string;
data?: FirecrawlDocument[];
error?: string;
}
/**
* Response interface for job status checks on v0.
*/
export interface JobStatusResponseV0 {
success: boolean; success: boolean;
status: string; status: string;
current?: number; current?: number;
current_url?: string; current_url?: string;
current_step?: string; current_step?: string;
total?: number; total?: number;
jobId?: string; data?: FirecrawlDocumentV0[];
data?: FirecrawlDocument[]; partial_data?: FirecrawlDocumentV0[];
partial_data?: FirecrawlDocument[];
error?: string; error?: string;
} }
/** /**
* Generic parameter interface. * Generic parameter interface.
*/ */
@ -126,14 +215,16 @@ export interface Params {
export default class FirecrawlApp { export default class FirecrawlApp {
private apiKey: string; private apiKey: string;
private apiUrl: string; private apiUrl: string;
private version: "v0" | "v1";
/** /**
* Initializes a new instance of the FirecrawlApp class. * Initializes a new instance of the FirecrawlApp class.
* @param {FirecrawlAppConfig} config - Configuration options for the FirecrawlApp instance. * @param {FirecrawlAppConfig} config - Configuration options for the FirecrawlApp instance.
*/ */
constructor({ apiKey = null, apiUrl = null }: FirecrawlAppConfig) { constructor({ apiKey = null, apiUrl = null, version = "v1" }: FirecrawlAppConfig) {
this.apiKey = apiKey || ""; this.apiKey = apiKey || "";
this.apiUrl = apiUrl || "https://api.firecrawl.dev"; this.apiUrl = apiUrl || "https://api.firecrawl.dev";
this.version = version;
if (!this.apiKey) { if (!this.apiKey) {
throw new Error("No API key provided"); throw new Error("No API key provided");
} }
@ -143,12 +234,17 @@ export default class FirecrawlApp {
* Scrapes a URL using the Firecrawl API. * Scrapes a URL using the Firecrawl API.
* @param {string} url - The URL to scrape. * @param {string} url - The URL to scrape.
* @param {Params | null} params - Additional parameters for the scrape request. * @param {Params | null} params - Additional parameters for the scrape request.
* @returns {Promise<ScrapeResponse>} The response from the scrape operation. * @returns {Promise<ScrapeResponse | ScrapeResponseV0>} The response from the scrape operation.
*/ */
async scrapeUrl( async scrapeUrl(
url: string, url: string,
params: Params | null = null params: Params | null = null,
): Promise<ScrapeResponse> { version: "v0" | "v1" = "v1"
): Promise<ScrapeResponse | ScrapeResponseV0> {
if (version) {
this.version = version;
}
const headers: AxiosRequestHeaders = { const headers: AxiosRequestHeaders = {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`, Authorization: `Bearer ${this.apiKey}`,
@ -171,7 +267,7 @@ export default class FirecrawlApp {
} }
try { try {
const response: AxiosResponse = await axios.post( const response: AxiosResponse = await axios.post(
this.apiUrl + "/v0/scrape", this.apiUrl + `/${this.version}/scrape`,
jsonData, jsonData,
{ headers } { headers }
); );
@ -200,7 +296,11 @@ export default class FirecrawlApp {
async search( async search(
query: string, query: string,
params: Params | null = null params: Params | null = null
): Promise<SearchResponse> { ): Promise<SearchResponseV0> {
if (this.version === "v1") {
throw new Error("Search is not supported in v1");
}
const headers: AxiosRequestHeaders = { const headers: AxiosRequestHeaders = {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`, Authorization: `Bearer ${this.apiKey}`,
@ -245,8 +345,13 @@ export default class FirecrawlApp {
params: Params | null = null, params: Params | null = null,
waitUntilDone: boolean = true, waitUntilDone: boolean = true,
pollInterval: number = 2, pollInterval: number = 2,
idempotencyKey?: string idempotencyKey?: string,
): Promise<CrawlResponse | any> { version: "v0" | "v1" = "v1"
): Promise<CrawlResponse | CrawlResponseV0 | JobStatusResponse | JobStatusResponseV0> {
if (version) {
this.version = version;
}
const headers = this.prepareHeaders(idempotencyKey); const headers = this.prepareHeaders(idempotencyKey);
let jsonData: Params = { url }; let jsonData: Params = { url };
if (params) { if (params) {
@ -254,7 +359,7 @@ export default class FirecrawlApp {
} }
try { try {
const response: AxiosResponse = await this.postRequest( const response: AxiosResponse = await this.postRequest(
this.apiUrl + "/v0/crawl", this.apiUrl + `/${this.version}/crawl`,
jsonData, jsonData,
headers headers
); );
@ -278,13 +383,15 @@ export default class FirecrawlApp {
/** /**
* Checks the status of a crawl job using the Firecrawl API. * Checks the status of a crawl job using the Firecrawl API.
* @param {string} jobId - The job ID of the crawl operation. * @param {string} jobId - The job ID of the crawl operation.
* @returns {Promise<JobStatusResponse>} The response containing the job status. * @returns {Promise<JobStatusResponse | JobStatusResponseV0>} The response containing the job status.
*/ */
async checkCrawlStatus(jobId: string): Promise<JobStatusResponse> { async checkCrawlStatus(jobId: string): Promise<JobStatusResponse | JobStatusResponseV0> {
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.apiUrl + `/v0/crawl/status/${jobId}`, this.version == 'v1' ?
this.apiUrl + `/${this.version}/crawl/${jobId}` :
this.apiUrl + `/${this.version}/crawl/status/${jobId}`,
headers headers
); );
if (response.status === 200) { if (response.status === 200) {