Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(js-sdk/v2/search): result mapping (#2004)

Gergő Móricz011ebee93fb8
diff --git a/apps/js-sdk/firecrawl/package.json b/apps/js-sdk/firecrawl/package.json
index ced6199b5..5592435ec 100644
--- a/apps/js-sdk/firecrawl/package.json
+++ b/apps/js-sdk/firecrawl/package.json
@@ -1,6 +1,6 @@
{
"name": "@mendable/firecrawl-js",
- "version": "3.0.3",
+ "version": "3.1.0",
"description": "JavaScript SDK for Firecrawl API",
"main": "dist/index.js",
"types": "dist/index.d.ts",
diff --git a/apps/js-sdk/firecrawl/src/v2/methods/search.ts b/apps/js-sdk/firecrawl/src/v2/methods/search.ts
index 0812bf7bf..5653f3a5c 100644
--- a/apps/js-sdk/firecrawl/src/v2/methods/search.ts
+++ b/apps/js-sdk/firecrawl/src/v2/methods/search.ts
@@ -1,4 +1,4 @@
-import { type Document, type SearchData, type SearchRequest, type SearchResult, type ScrapeOptions } from "../types";
+import { type Document, type SearchData, type SearchRequest, type SearchResultWeb, type ScrapeOptions, type SearchResultNews, type SearchResultImages } from "../types";
import { HttpClient } from "../utils/httpClient";
import { ensureValidScrapeOptions } from "../utils/validation";
import { throwForBadResponse, normalizeAxiosError } from "../utils/errorHandler";
@@ -23,6 +23,31 @@ function prepareSearchPayload(req: SearchRequest): Record<string, unknown> {
return payload;
}
+function transformArray<ResultType>(arr: any[]): Array<ResultType | Document> {
+ const results: Array<ResultType | Document> = [] as any;
+ for (const item of arr) {
+ if (item && typeof item === "object") {
+ if (
+ "markdown" in item ||
+ "html" in item ||
+ "rawHtml" in item ||
+ "links" in item ||
+ "screenshot" in item ||
+ "changeTracking" in item ||
+ "summary" in item ||
+ "json" in item
+ ) {
+ results.push(item as Document);
+ } else {
+ results.push(item as ResultType);
+ }
+ } else {
+ results.push({ url: item } as ResultType);
+ }
+ }
+ return results;
+}
+
export async function search(http: HttpClient, request: SearchRequest): Promise<SearchData> {
const payload = prepareSearchPayload(request);
try {
@@ -32,34 +57,9 @@ export async function search(http: HttpClient, request: SearchRequest): Promise<
}
const data = (res.data.data || {}) as Record<string, any>;
const out: SearchData = {};
- for (const key of Object.keys(data)) {
- const arr = data[key];
- if (Array.isArray(arr)) {
- const results: Array<SearchResult | Document> = [] as any;
- for (const item of arr) {
- if (item && typeof item === "object") {
- // If scraped page fields present, treat as Document; otherwise SearchResult
- if (
- "markdown" in item ||
- "html" in item ||
- "rawHtml" in item ||
- "links" in item ||
- "screenshot" in item ||
- "changeTracking" in item ||
- "summary" in item ||
- "json" in item
- ) {
- results.push(item as Document);
- } else {
- results.push({ url: item.url, title: item.title, description: item.description } as SearchResult);
- }
- } else if (typeof item === "string") {
- results.push({ url: item } as SearchResult);
- }
- }
- (out as any)[key] = results;
- }
- }
+ if (data.web) out.web = transformArray<SearchResultWeb>(data.web);
+ if (data.news) out.news = transformArray<SearchResultNews>(data.news);
+ if (data.images) out.images = transformArray<SearchResultImages>(data.images);
return out;
} catch (err: any) {
if (err?.isAxiosError) return normalizeAxiosError(err, "search");
diff --git a/apps/js-sdk/firecrawl/src/v2/types.ts b/apps/js-sdk/firecrawl/src/v2/types.ts
index 838aae7aa..b6193da10 100644
--- a/apps/js-sdk/firecrawl/src/v2/types.ts
+++ b/apps/js-sdk/firecrawl/src/v2/types.ts
@@ -173,16 +173,34 @@ export interface Document {
changeTracking?: Record<string, unknown>;
}
-export interface SearchResult {
+export interface SearchResultWeb {
url: string;
title?: string;
description?: string;
}
+export interface SearchResultNews {
+ title?: string;
+ url?: string;
+ snippet?: string;
+ date?: string;
+ imageUrl?: string;
+ position?: number;
+}
+
+export interface SearchResultImages {
+ title?: string;
+ imageUrl?: string;
+ imageWidth?: number;
+ imageHeight?: number;
+ url?: string;
+ position?: number;
+}
+
export interface SearchData {
- web?: Array<SearchResult | Document>;
- news?: Array<SearchResult | Document>;
- images?: Array<SearchResult | Document>;
+ web?: Array<SearchResultWeb | Document>;
+ news?: Array<SearchResultNews | Document>;
+ images?: Array<SearchResultImages | Document>;
}
export interface SearchRequest {
@@ -257,7 +275,7 @@ export interface BatchScrapeJob {
}
export interface MapData {
- links: SearchResult[];
+ links: SearchResultWeb[];
}
export interface MapOptions {