Nick: fixed extract types on node sdk

This commit is contained in:
Nicolas 2024-11-26 18:00:49 -03:00
parent 6c33b978f3
commit bc0d66e96e

View File

@ -240,9 +240,9 @@ export interface MapResponse {
* Parameters for extracting information from URLs. * Parameters for extracting information from URLs.
* Defines options for extracting information from URLs. * Defines options for extracting information from URLs.
*/ */
export interface ExtractParams { export interface ExtractParams<LLMSchema extends zt.ZodSchema = any> {
prompt: string; prompt: string;
schema?: zt.ZodSchema; schema?: LLMSchema;
systemPrompt?: string; systemPrompt?: string;
allowExternalLinks?: boolean; allowExternalLinks?: boolean;
} }
@ -251,10 +251,11 @@ export interface ExtractParams {
* Response interface for extracting information from URLs. * Response interface for extracting information from URLs.
* Defines the structure of the response received after extracting information from URLs. * Defines the structure of the response received after extracting information from URLs.
*/ */
export interface ExtractResponse { export interface ExtractResponse<LLMSchema extends zt.ZodSchema = any> {
success: true; success: boolean;
data: zt.infer<zt.ZodSchema>; data: LLMSchema;
error?: string; error?: string;
warning?: string;
} }
/** /**
@ -701,18 +702,19 @@ export default class FirecrawlApp {
/** /**
* Extracts information from URLs using the Firecrawl API. * Extracts information from URLs using the Firecrawl API.
* Currently in Beta. Expect breaking changes on future minor versions.
* @param url - The URL to extract information from. * @param url - The URL to extract information from.
* @param params - Additional parameters for the extract request. * @param params - Additional parameters for the extract request.
* @returns The response from the extract operation. * @returns The response from the extract operation.
*/ */
async extract(urls: string[], params?: ExtractParams): Promise<ExtractResponse | ErrorResponse> { async extract<T extends zt.ZodSchema = any>(urls: string[], params?: ExtractParams<T>): Promise<ExtractResponse<zt.infer<T>> | ErrorResponse> {
const headers = this.prepareHeaders(); const headers = this.prepareHeaders();
if (!params?.prompt) { if (!params?.prompt) {
throw new FirecrawlError("Prompt is required", 400); throw new FirecrawlError("Prompt is required", 400);
} }
let jsonData: { urls: string[] } & ExtractParams= { urls, ...params }; let jsonData: { urls: string[] } & ExtractParams<T> = { urls, ...params };
let jsonSchema: any; let jsonSchema: any;
try { try {
jsonSchema = params?.schema ? zodToJsonSchema(params.schema) : undefined; jsonSchema = params?.schema ? zodToJsonSchema(params.schema) : undefined;
@ -727,7 +729,17 @@ export default class FirecrawlApp {
headers headers
); );
if (response.status === 200) { if (response.status === 200) {
return response.data as ExtractResponse; const responseData = response.data as ExtractResponse<T>;
if (responseData.success) {
return {
success: true,
data: responseData.data,
warning: responseData.warning,
error: responseData.error
};
} else {
throw new FirecrawlError(`Failed to scrape URL. Error: ${responseData.error}`, response.status);
}
} else { } else {
this.handleError(response, "extract"); this.handleError(response, "extract");
} }