@howells/fieldwork-sdk 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +104 -38
- package/dist/adapter-CEAQuT7F.js +3 -0
- package/dist/adapter-DGehHCMo.d.ts +470 -0
- package/dist/brave.d.ts +164 -0
- package/dist/brave.js +399 -0
- package/dist/browserbase.d.ts +128 -0
- package/dist/browserbase.js +180 -0
- package/dist/{errors-pzPMh82H.js → errors-BiIBG9zN.js} +14 -18
- package/dist/exa.d.ts +150 -24
- package/dist/exa.js +427 -119
- package/dist/{fieldwork-nPzWEelg.d.ts → fieldwork-B3x3cxDJ.d.ts} +5 -5
- package/dist/firecrawl.d.ts +220 -28
- package/dist/firecrawl.js +855 -233
- package/dist/index.d.ts +11 -5
- package/dist/index.js +53 -35
- package/dist/parallel.d.ts +188 -19
- package/dist/parallel.js +644 -113
- package/dist/perplexity.d.ts +78 -22
- package/dist/perplexity.js +318 -92
- package/dist/serpapi.d.ts +172 -0
- package/dist/serpapi.js +517 -0
- package/dist/tavily.d.ts +104 -31
- package/dist/tavily.js +347 -102
- package/dist/tools.d.ts +42 -14
- package/dist/tools.js +1274 -484
- package/dist/types-CnomnF-6.js +27 -0
- package/dist/util-YaZYZDSk.js +287 -0
- package/package.json +19 -3
- package/dist/adapter-C_kRm6TT.d.ts +0 -178
- package/dist/adapter-D-6-GFhJ.js +0 -5
- package/dist/util-D0PLE6Rl.d.ts +0 -2
- package/dist/util-DgcIZI2K.js +0 -214
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { W as Recency, a as RawClient, f as CallOptions, k as ImageSearch, q as SearchResponse, s as AccountUsage, t as Adapter } from "./adapter-DGehHCMo.js";
|
|
2
|
+
interface SerpApiConfig {
|
|
3
|
+
/** Defaults to the SERPAPI_API_KEY environment variable. */
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
fetch?: typeof fetch;
|
|
7
|
+
}
|
|
8
|
+
type Device = "desktop" | "tablet" | "mobile";
|
|
9
|
+
interface SerpApiBaseOptions extends CallOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Skip SerpApi's one-hour cache and fetch fresh results. Cached searches
|
|
12
|
+
* are free and do not count toward the monthly allowance.
|
|
13
|
+
*/
|
|
14
|
+
noCache?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** Google web search: `GET /search?engine=google`, organic results and the answer box. */
|
|
17
|
+
interface SerpApiGoogleOptions extends SerpApiBaseOptions {
|
|
18
|
+
engine?: "google";
|
|
19
|
+
/** ISO 3166-1 alpha-2 code such as "GB"; sent as `gl` ("uk" for GB, as SerpApi documents). */
|
|
20
|
+
country?: string;
|
|
21
|
+
/** Sent as `hl`: a language code such as "en", or "en-gb". */
|
|
22
|
+
language?: string;
|
|
23
|
+
/** Where the search originates, as text: "Austin, Texas, United States". */
|
|
24
|
+
location?: string;
|
|
25
|
+
/** Sent as `tbs` `qdr`. Not allowed with `publishedAfter` or `publishedBefore`. */
|
|
26
|
+
recency?: Recency;
|
|
27
|
+
/** YYYY-MM-DD. Sent as `tbs` `cdr`. */
|
|
28
|
+
publishedAfter?: string;
|
|
29
|
+
/** YYYY-MM-DD. Sent as `tbs` `cdr`. */
|
|
30
|
+
publishedBefore?: string;
|
|
31
|
+
safe?: "active" | "off";
|
|
32
|
+
device?: Device;
|
|
33
|
+
/** Result offset: 0 is the first page, 10 the second. */
|
|
34
|
+
start?: number;
|
|
35
|
+
}
|
|
36
|
+
/** Google News: `engine=google_news`. SerpApi documents `q`, `gl` and `hl` for a news search. */
|
|
37
|
+
interface SerpApiNewsOptions extends SerpApiBaseOptions {
|
|
38
|
+
engine: "google_news";
|
|
39
|
+
country?: string;
|
|
40
|
+
language?: string;
|
|
41
|
+
}
|
|
42
|
+
/** Google Shopping: `engine=google_shopping`. */
|
|
43
|
+
interface SerpApiShoppingOptions extends SerpApiBaseOptions {
|
|
44
|
+
engine: "google_shopping";
|
|
45
|
+
country?: string;
|
|
46
|
+
language?: string;
|
|
47
|
+
location?: string;
|
|
48
|
+
safe?: "active" | "off";
|
|
49
|
+
device?: Device;
|
|
50
|
+
minPrice?: number;
|
|
51
|
+
maxPrice?: number;
|
|
52
|
+
/** 1 sorts by price, low to high; 2 by price, high to low. */
|
|
53
|
+
sortBy?: 1 | 2;
|
|
54
|
+
freeShipping?: boolean;
|
|
55
|
+
onSale?: boolean;
|
|
56
|
+
smallBusiness?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Google Maps: `engine=google_maps`, `type=search`. Give the search origin as
|
|
60
|
+
* `latitude` and `longitude`, or as `location`, and always with `zoom`.
|
|
61
|
+
*/
|
|
62
|
+
interface SerpApiMapsOptions extends SerpApiBaseOptions {
|
|
63
|
+
engine: "google_maps";
|
|
64
|
+
language?: string;
|
|
65
|
+
latitude?: number;
|
|
66
|
+
longitude?: number;
|
|
67
|
+
/** A place name for the search origin. Needs `zoom`. */
|
|
68
|
+
location?: string;
|
|
69
|
+
/** Map zoom, 3 (whole world) to 30. Sent as `z`. */
|
|
70
|
+
zoom?: number;
|
|
71
|
+
/** Result offset in steps of 20. */
|
|
72
|
+
start?: number;
|
|
73
|
+
}
|
|
74
|
+
/** Google Scholar: `engine=google_scholar`. */
|
|
75
|
+
interface SerpApiScholarOptions extends SerpApiBaseOptions {
|
|
76
|
+
engine: "google_scholar";
|
|
77
|
+
/** Results to return, 1 to 20. Sent as `num`. */
|
|
78
|
+
limit?: number;
|
|
79
|
+
language?: string;
|
|
80
|
+
safe?: "active" | "off";
|
|
81
|
+
/** Leaves out results before this year (`as_ylo`). Scholar filters by year only. */
|
|
82
|
+
yearFrom?: number;
|
|
83
|
+
/** Leaves out results after this year (`as_yhi`). */
|
|
84
|
+
yearTo?: number;
|
|
85
|
+
/** Result offset: 0 is the first page, 10 the second. */
|
|
86
|
+
start?: number;
|
|
87
|
+
}
|
|
88
|
+
/** Google Patents: `engine=google_patents`. */
|
|
89
|
+
interface SerpApiPatentsOptions extends SerpApiBaseOptions {
|
|
90
|
+
engine: "google_patents";
|
|
91
|
+
/** Results per page, 10 to 100. Sent as `num`. */
|
|
92
|
+
limit?: number;
|
|
93
|
+
/** Page number, starting at 1. */
|
|
94
|
+
page?: number;
|
|
95
|
+
/** "new" and "old" sort by filing date. The default is relevance. */
|
|
96
|
+
sort?: "new" | "old";
|
|
97
|
+
/** YYYY-MM-DD. Sent as `after=publication:YYYYMMDD`. */
|
|
98
|
+
publishedAfter?: string;
|
|
99
|
+
/** YYYY-MM-DD. Sent as `before=publication:YYYYMMDD`. */
|
|
100
|
+
publishedBefore?: string;
|
|
101
|
+
/** Inventor names, separated by commas. */
|
|
102
|
+
inventor?: string;
|
|
103
|
+
/** Assignee names, separated by commas. */
|
|
104
|
+
assignee?: string;
|
|
105
|
+
status?: "GRANT" | "APPLICATION";
|
|
106
|
+
}
|
|
107
|
+
/** Bing: `engine=bing`. */
|
|
108
|
+
interface SerpApiBingOptions extends SerpApiBaseOptions {
|
|
109
|
+
engine: "bing";
|
|
110
|
+
/** ISO 3166-1 alpha-2 code such as "GB"; sent as `cc`. Not allowed with `market`. */
|
|
111
|
+
country?: string;
|
|
112
|
+
/** Bing market code such as "en-US"; sent as `mkt`. Not allowed with `country`. */
|
|
113
|
+
market?: string;
|
|
114
|
+
location?: string;
|
|
115
|
+
/** Sent as `safeSearch`. */
|
|
116
|
+
safeSearch?: "Off" | "Moderate" | "Strict";
|
|
117
|
+
device?: Device;
|
|
118
|
+
/** Position of the first organic result to return, starting at 1. */
|
|
119
|
+
first?: number;
|
|
120
|
+
}
|
|
121
|
+
/** DuckDuckGo: `engine=duckduckgo`. */
|
|
122
|
+
interface SerpApiDuckDuckGoOptions extends SerpApiBaseOptions {
|
|
123
|
+
engine: "duckduckgo";
|
|
124
|
+
/** Results to return, 1 to 50. Sent as `m`. */
|
|
125
|
+
limit?: number;
|
|
126
|
+
/** DuckDuckGo region such as "us-en" or "fr-fr"; sent as `kl`. */
|
|
127
|
+
region?: string;
|
|
128
|
+
/** 1 is strict, -1 moderate (the default), -2 off. */
|
|
129
|
+
safe?: 1 | -1 | -2;
|
|
130
|
+
/** Sent as `df`. DuckDuckGo has no hour window. Not allowed with the date options. */
|
|
131
|
+
recency?: Exclude<Recency, "hour">;
|
|
132
|
+
/** YYYY-MM-DD. DuckDuckGo needs both dates for a range. */
|
|
133
|
+
publishedAfter?: string;
|
|
134
|
+
/** YYYY-MM-DD. DuckDuckGo needs both dates for a range. */
|
|
135
|
+
publishedBefore?: string;
|
|
136
|
+
start?: number;
|
|
137
|
+
}
|
|
138
|
+
type SerpApiSearchOptions = SerpApiGoogleOptions | SerpApiNewsOptions | SerpApiShoppingOptions | SerpApiMapsOptions | SerpApiScholarOptions | SerpApiPatentsOptions | SerpApiBingOptions | SerpApiDuckDuckGoOptions;
|
|
139
|
+
type SerpApiEngine = NonNullable<SerpApiSearchOptions["engine"]>;
|
|
140
|
+
/** Google Images: `engine=google_images`. */
|
|
141
|
+
interface SerpApiImagesOptions extends SerpApiBaseOptions {
|
|
142
|
+
country?: string;
|
|
143
|
+
language?: string;
|
|
144
|
+
location?: string;
|
|
145
|
+
/** Sent as `period_unit`. Not allowed with `publishedAfter` or `publishedBefore`. */
|
|
146
|
+
recency?: Recency;
|
|
147
|
+
/** YYYY-MM-DD. Sent as `start_date`. */
|
|
148
|
+
publishedAfter?: string;
|
|
149
|
+
/** YYYY-MM-DD. Sent as `end_date`. */
|
|
150
|
+
publishedBefore?: string;
|
|
151
|
+
safe?: "active" | "off";
|
|
152
|
+
device?: Device;
|
|
153
|
+
/** Page number, 0 to 99. Sent as `ijn`. */
|
|
154
|
+
page?: number;
|
|
155
|
+
/** s square, t tall, w wide, xw panoramic. Sent as `imgar`. */
|
|
156
|
+
aspectRatio?: "s" | "t" | "w" | "xw";
|
|
157
|
+
/** Sent as `imgsz`: l, m, i, or a minimum such as "qsvga" or "2mp". */
|
|
158
|
+
size?: "l" | "m" | "i" | "qsvga" | "vga" | "svga" | "xga" | "2mp" | "4mp" | "6mp" | "8mp" | "10mp" | "12mp" | "15mp" | "20mp" | "40mp" | "70mp";
|
|
159
|
+
/** Sent as `image_color`. */
|
|
160
|
+
color?: "bw" | "trans" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "purple" | "pink" | "white" | "gray" | "black" | "brown";
|
|
161
|
+
/** Sent as `image_type`. */
|
|
162
|
+
imageType?: "face" | "photo" | "clipart" | "lineart" | "animated";
|
|
163
|
+
/** Sent as `licenses`: f, fc, fm, fmc, cl or ol. */
|
|
164
|
+
license?: "f" | "fc" | "fm" | "fmc" | "cl" | "ol";
|
|
165
|
+
}
|
|
166
|
+
interface SerpApiOperations {
|
|
167
|
+
search(query: string, options?: SerpApiSearchOptions): Promise<SearchResponse>;
|
|
168
|
+
images(query: string, options?: SerpApiImagesOptions): Promise<ImageSearch>;
|
|
169
|
+
usage(options?: CallOptions): Promise<AccountUsage>;
|
|
170
|
+
}
|
|
171
|
+
export declare const serpapi: (config?: SerpApiConfig) => Adapter<"serpapi", SerpApiOperations, RawClient>;
|
|
172
|
+
export type { SerpApiBingOptions, SerpApiConfig, SerpApiDuckDuckGoOptions, SerpApiEngine, SerpApiGoogleOptions, SerpApiImagesOptions, SerpApiMapsOptions, SerpApiNewsOptions, SerpApiOperations, SerpApiPatentsOptions, SerpApiScholarOptions, SerpApiSearchOptions, SerpApiShoppingOptions };
|
package/dist/serpapi.js
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
import { t as defineAdapter } from "./adapter-CEAQuT7F.js";
|
|
2
|
+
import { S as requireKey, _ as strings, b as createRawClient, c as num, g as scalar, i as compact, m as refuseTogether, o as list, p as record, s as meta, t as callSettings, v as text } from "./util-YaZYZDSk.js";
|
|
3
|
+
import { t as FieldworkError } from "./errors-BiIBG9zN.js";
|
|
4
|
+
// src/adapters/serpapi/parse.ts
|
|
5
|
+
const provider = "serpapi";
|
|
6
|
+
const invalid = (message) => new FieldworkError("InvalidRequest", `${provider} ${message}`, { provider });
|
|
7
|
+
const priceOf = (item) => {
|
|
8
|
+
const price = compact({
|
|
9
|
+
amount: scalar(item.extracted_price),
|
|
10
|
+
formatted: text(item.price)
|
|
11
|
+
});
|
|
12
|
+
return Object.keys(price).length > 0 ? price : void 0;
|
|
13
|
+
};
|
|
14
|
+
const webResults = (response) => list(response.organic_results).flatMap((entry) => {
|
|
15
|
+
const item = record(entry);
|
|
16
|
+
const url = text(item.link);
|
|
17
|
+
return url ? [compact({
|
|
18
|
+
author: text(item.author),
|
|
19
|
+
favicon: text(item.favicon),
|
|
20
|
+
image: text(item.thumbnail),
|
|
21
|
+
kind: "web",
|
|
22
|
+
publishedAt: text(item.date),
|
|
23
|
+
snippet: text(item.snippet),
|
|
24
|
+
title: text(item.title),
|
|
25
|
+
url
|
|
26
|
+
})] : [];
|
|
27
|
+
});
|
|
28
|
+
const newsItem = (entry) => {
|
|
29
|
+
const item = record(entry);
|
|
30
|
+
const url = text(item.link);
|
|
31
|
+
if (!url) return [];
|
|
32
|
+
const source = record(item.source);
|
|
33
|
+
const authors = strings(source.authors);
|
|
34
|
+
return [compact({
|
|
35
|
+
author: authors?.join(", "),
|
|
36
|
+
favicon: text(source.icon),
|
|
37
|
+
image: text(item.thumbnail),
|
|
38
|
+
kind: "news",
|
|
39
|
+
publishedAt: text(item.iso_date) ?? text(item.date),
|
|
40
|
+
title: text(item.title),
|
|
41
|
+
url
|
|
42
|
+
})];
|
|
43
|
+
};
|
|
44
|
+
const newsResults = (response) => list(response.news_results).flatMap((entry) => {
|
|
45
|
+
const item = record(entry);
|
|
46
|
+
return [
|
|
47
|
+
...newsItem(item),
|
|
48
|
+
...newsItem(item.highlight),
|
|
49
|
+
...list(item.stories).flatMap((story) => newsItem(story))
|
|
50
|
+
];
|
|
51
|
+
});
|
|
52
|
+
const shoppingResults = (response) => {
|
|
53
|
+
const categorized = list(response.categorized_shopping_results).flatMap((group) => list(record(group).shopping_results));
|
|
54
|
+
return ([
|
|
55
|
+
list(response.shopping_results),
|
|
56
|
+
categorized,
|
|
57
|
+
list(response.inline_shopping_results)
|
|
58
|
+
].find((group) => group.length > 0) ?? []).flatMap((entry) => {
|
|
59
|
+
const item = record(entry);
|
|
60
|
+
const url = text(item.link) ?? text(item.product_link);
|
|
61
|
+
return url ? [compact({
|
|
62
|
+
image: text(item.thumbnail),
|
|
63
|
+
kind: "shopping",
|
|
64
|
+
price: priceOf(item),
|
|
65
|
+
rating: num(item.rating),
|
|
66
|
+
reviewCount: num(item.reviews),
|
|
67
|
+
snippet: text(item.snippet),
|
|
68
|
+
title: text(item.title),
|
|
69
|
+
url
|
|
70
|
+
})] : [];
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
const mapsUrl = (item) => {
|
|
74
|
+
const website = text(item.website);
|
|
75
|
+
if (website) return website;
|
|
76
|
+
const id = text(item.place_id);
|
|
77
|
+
const title = text(item.title);
|
|
78
|
+
return id && title ? `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(title)}&query_place_id=${encodeURIComponent(id)}` : void 0;
|
|
79
|
+
};
|
|
80
|
+
const placeResults = (response) => {
|
|
81
|
+
const single = record(response.place_results);
|
|
82
|
+
const places = list(response.local_results);
|
|
83
|
+
return (places.length === 0 && Object.keys(single).length > 0 ? [single] : places).flatMap((entry) => {
|
|
84
|
+
const item = record(entry);
|
|
85
|
+
const url = mapsUrl(item);
|
|
86
|
+
if (!url) return [];
|
|
87
|
+
const coordinates = record(item.gps_coordinates);
|
|
88
|
+
const place = compact({
|
|
89
|
+
address: text(item.address),
|
|
90
|
+
latitude: num(coordinates.latitude),
|
|
91
|
+
longitude: num(coordinates.longitude)
|
|
92
|
+
});
|
|
93
|
+
return [compact({
|
|
94
|
+
image: text(item.thumbnail),
|
|
95
|
+
kind: "place",
|
|
96
|
+
place: Object.keys(place).length > 0 ? place : void 0,
|
|
97
|
+
rating: num(item.rating),
|
|
98
|
+
reviewCount: num(item.reviews),
|
|
99
|
+
snippet: text(item.description),
|
|
100
|
+
title: text(item.title),
|
|
101
|
+
url
|
|
102
|
+
})];
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
const scholarResults = (response) => list(response.organic_results).flatMap((entry) => {
|
|
106
|
+
const item = record(entry);
|
|
107
|
+
const url = text(item.link) ?? text(record(list(item.resources)[0]).link);
|
|
108
|
+
return url ? [compact({
|
|
109
|
+
kind: "scholar",
|
|
110
|
+
snippet: text(item.snippet),
|
|
111
|
+
title: text(item.title),
|
|
112
|
+
url
|
|
113
|
+
})] : [];
|
|
114
|
+
});
|
|
115
|
+
const patentResults = (response) => list(response.organic_results).flatMap((entry) => {
|
|
116
|
+
const item = record(entry);
|
|
117
|
+
const url = text(item.patent_link);
|
|
118
|
+
return url ? [compact({
|
|
119
|
+
author: text(item.inventor),
|
|
120
|
+
image: text(item.thumbnail),
|
|
121
|
+
kind: "patent",
|
|
122
|
+
publishedAt: text(item.publication_date),
|
|
123
|
+
snippet: text(item.snippet),
|
|
124
|
+
title: text(item.title),
|
|
125
|
+
url
|
|
126
|
+
})] : [];
|
|
127
|
+
});
|
|
128
|
+
const answerText = (response) => {
|
|
129
|
+
const box = record(response.answer_box ?? list(response.answer_box_list)[0]);
|
|
130
|
+
return text(box.answer) ?? text(scalar(box.result)) ?? text(box.snippet);
|
|
131
|
+
};
|
|
132
|
+
const searchMeta = (response) => meta({
|
|
133
|
+
requestId: record(response.search_metadata).id,
|
|
134
|
+
warnings: response.error
|
|
135
|
+
});
|
|
136
|
+
const failIfError = (response) => {
|
|
137
|
+
if (record(response.search_metadata).status === "Error") throw new FieldworkError("ProviderError", `serpapi search failed: ${text(response.error) ?? "no reason given"}`, {
|
|
138
|
+
cause: response,
|
|
139
|
+
provider,
|
|
140
|
+
retryable: true
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
// src/adapters/serpapi/options.ts
|
|
144
|
+
const RECENCY_CODE = {
|
|
145
|
+
day: "d",
|
|
146
|
+
hour: "h",
|
|
147
|
+
month: "m",
|
|
148
|
+
week: "w",
|
|
149
|
+
year: "y"
|
|
150
|
+
};
|
|
151
|
+
const str = (value) => typeof value === "string" ? value : void 0;
|
|
152
|
+
const count = (value) => typeof value === "number" ? value : void 0;
|
|
153
|
+
const googleCountry = (value) => {
|
|
154
|
+
const code = str(value)?.toLowerCase();
|
|
155
|
+
return code === "gb" ? "uk" : code;
|
|
156
|
+
};
|
|
157
|
+
const isoDate = (name, value) => {
|
|
158
|
+
if (value === void 0) return;
|
|
159
|
+
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/u.test(value)) throw invalid(`${name} must be YYYY-MM-DD`);
|
|
160
|
+
return value;
|
|
161
|
+
};
|
|
162
|
+
const compactDate = (name, value) => isoDate(name, value)?.replaceAll("-", "");
|
|
163
|
+
const usDate = (name, value) => {
|
|
164
|
+
const [year, month, day] = isoDate(name, value)?.split("-") ?? [];
|
|
165
|
+
return year === void 0 ? void 0 : `${Number(month)}/${Number(day)}/${year}`;
|
|
166
|
+
};
|
|
167
|
+
const bounded = (engine, value, min, max) => {
|
|
168
|
+
if (value === void 0) return;
|
|
169
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) throw invalid(`${engine} limit must be a whole number from ${min} to ${max}`);
|
|
170
|
+
return value;
|
|
171
|
+
};
|
|
172
|
+
const googleTbs = (o) => {
|
|
173
|
+
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
174
|
+
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
175
|
+
if (o.recency !== void 0) return `qdr:${RECENCY_CODE[o.recency]}`;
|
|
176
|
+
const after = usDate("publishedAfter", o.publishedAfter);
|
|
177
|
+
const before = usDate("publishedBefore", o.publishedBefore);
|
|
178
|
+
if (after === void 0 && before === void 0) return;
|
|
179
|
+
return [
|
|
180
|
+
"cdr:1",
|
|
181
|
+
after === void 0 ? void 0 : `cd_min:${after}`,
|
|
182
|
+
before === void 0 ? void 0 : `cd_max:${before}`
|
|
183
|
+
].filter((part) => part !== void 0).join(",");
|
|
184
|
+
};
|
|
185
|
+
const duckDuckGoFilter = (o) => {
|
|
186
|
+
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
187
|
+
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
188
|
+
if (o.recency === "hour") throw invalid("duckduckgo has no hour window; use day, week, month or year");
|
|
189
|
+
if (o.recency !== void 0) return RECENCY_CODE[o.recency];
|
|
190
|
+
if (o.publishedAfter === void 0 && o.publishedBefore === void 0) return;
|
|
191
|
+
if (o.publishedAfter === void 0 || o.publishedBefore === void 0) throw invalid("duckduckgo filters a date range by both ends: pass publishedAfter and publishedBefore together");
|
|
192
|
+
return `${isoDate("publishedAfter", o.publishedAfter)}..${isoDate("publishedBefore", o.publishedBefore)}`;
|
|
193
|
+
};
|
|
194
|
+
const patentDate = (name, value) => {
|
|
195
|
+
const date = compactDate(name, value);
|
|
196
|
+
return date === void 0 ? void 0 : `publication:${date}`;
|
|
197
|
+
};
|
|
198
|
+
const mapsOrigin = (o) => {
|
|
199
|
+
refuseTogether(provider, o, "location", "latitude");
|
|
200
|
+
refuseTogether(provider, o, "location", "longitude");
|
|
201
|
+
const coordinates = o.latitude !== void 0 || o.longitude !== void 0;
|
|
202
|
+
if (coordinates && (o.latitude === void 0 || o.longitude === void 0)) throw invalid("google_maps needs latitude and longitude together");
|
|
203
|
+
const origin = coordinates || o.location !== void 0;
|
|
204
|
+
if (origin && o.zoom === void 0) throw invalid("google_maps needs zoom with location or latitude and longitude");
|
|
205
|
+
if (!origin && o.zoom !== void 0) throw invalid("google_maps zoom needs location or latitude and longitude");
|
|
206
|
+
return {
|
|
207
|
+
lat: count(o.latitude),
|
|
208
|
+
location: str(o.location),
|
|
209
|
+
lon: count(o.longitude),
|
|
210
|
+
z: count(o.zoom)
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
// src/adapters/serpapi/engines.ts
|
|
214
|
+
const ENGINES = {
|
|
215
|
+
bing: {
|
|
216
|
+
allowed: [
|
|
217
|
+
"country",
|
|
218
|
+
"market",
|
|
219
|
+
"location",
|
|
220
|
+
"safeSearch",
|
|
221
|
+
"device",
|
|
222
|
+
"first",
|
|
223
|
+
"noCache"
|
|
224
|
+
],
|
|
225
|
+
params: (o) => {
|
|
226
|
+
refuseTogether(provider, o, "country", "market");
|
|
227
|
+
return {
|
|
228
|
+
cc: str(o.country)?.toLowerCase(),
|
|
229
|
+
device: str(o.device),
|
|
230
|
+
first: count(o.first),
|
|
231
|
+
location: str(o.location),
|
|
232
|
+
mkt: str(o.market),
|
|
233
|
+
safeSearch: str(o.safeSearch)
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
results: webResults
|
|
237
|
+
},
|
|
238
|
+
duckduckgo: {
|
|
239
|
+
allowed: [
|
|
240
|
+
"limit",
|
|
241
|
+
"region",
|
|
242
|
+
"safe",
|
|
243
|
+
"recency",
|
|
244
|
+
"publishedAfter",
|
|
245
|
+
"publishedBefore",
|
|
246
|
+
"start",
|
|
247
|
+
"noCache"
|
|
248
|
+
],
|
|
249
|
+
params: (o) => ({
|
|
250
|
+
df: duckDuckGoFilter(o),
|
|
251
|
+
kl: str(o.region),
|
|
252
|
+
m: bounded("duckduckgo", o.limit, 1, 50),
|
|
253
|
+
safe: count(o.safe),
|
|
254
|
+
start: count(o.start)
|
|
255
|
+
}),
|
|
256
|
+
results: webResults
|
|
257
|
+
},
|
|
258
|
+
google: {
|
|
259
|
+
allowed: [
|
|
260
|
+
"country",
|
|
261
|
+
"language",
|
|
262
|
+
"location",
|
|
263
|
+
"recency",
|
|
264
|
+
"publishedAfter",
|
|
265
|
+
"publishedBefore",
|
|
266
|
+
"safe",
|
|
267
|
+
"device",
|
|
268
|
+
"start",
|
|
269
|
+
"noCache"
|
|
270
|
+
],
|
|
271
|
+
params: (o) => ({
|
|
272
|
+
device: str(o.device),
|
|
273
|
+
gl: googleCountry(o.country),
|
|
274
|
+
hl: str(o.language),
|
|
275
|
+
location: str(o.location),
|
|
276
|
+
safe: str(o.safe),
|
|
277
|
+
start: count(o.start),
|
|
278
|
+
tbs: googleTbs(o)
|
|
279
|
+
}),
|
|
280
|
+
results: webResults
|
|
281
|
+
},
|
|
282
|
+
google_maps: {
|
|
283
|
+
allowed: [
|
|
284
|
+
"language",
|
|
285
|
+
"latitude",
|
|
286
|
+
"longitude",
|
|
287
|
+
"location",
|
|
288
|
+
"zoom",
|
|
289
|
+
"start",
|
|
290
|
+
"noCache"
|
|
291
|
+
],
|
|
292
|
+
params: (o) => ({
|
|
293
|
+
type: "search",
|
|
294
|
+
hl: str(o.language),
|
|
295
|
+
...mapsOrigin(o),
|
|
296
|
+
start: count(o.start)
|
|
297
|
+
}),
|
|
298
|
+
results: placeResults
|
|
299
|
+
},
|
|
300
|
+
google_news: {
|
|
301
|
+
allowed: [
|
|
302
|
+
"country",
|
|
303
|
+
"language",
|
|
304
|
+
"noCache"
|
|
305
|
+
],
|
|
306
|
+
params: (o) => ({
|
|
307
|
+
gl: googleCountry(o.country),
|
|
308
|
+
hl: str(o.language)
|
|
309
|
+
}),
|
|
310
|
+
results: newsResults
|
|
311
|
+
},
|
|
312
|
+
google_patents: {
|
|
313
|
+
allowed: [
|
|
314
|
+
"limit",
|
|
315
|
+
"page",
|
|
316
|
+
"sort",
|
|
317
|
+
"publishedAfter",
|
|
318
|
+
"publishedBefore",
|
|
319
|
+
"inventor",
|
|
320
|
+
"assignee",
|
|
321
|
+
"status",
|
|
322
|
+
"noCache"
|
|
323
|
+
],
|
|
324
|
+
params: (o) => ({
|
|
325
|
+
after: patentDate("publishedAfter", o.publishedAfter),
|
|
326
|
+
assignee: str(o.assignee),
|
|
327
|
+
before: patentDate("publishedBefore", o.publishedBefore),
|
|
328
|
+
inventor: str(o.inventor),
|
|
329
|
+
num: bounded("google_patents", o.limit, 10, 100),
|
|
330
|
+
page: count(o.page),
|
|
331
|
+
sort: str(o.sort),
|
|
332
|
+
status: str(o.status)
|
|
333
|
+
}),
|
|
334
|
+
results: patentResults
|
|
335
|
+
},
|
|
336
|
+
google_scholar: {
|
|
337
|
+
allowed: [
|
|
338
|
+
"limit",
|
|
339
|
+
"language",
|
|
340
|
+
"safe",
|
|
341
|
+
"yearFrom",
|
|
342
|
+
"yearTo",
|
|
343
|
+
"start",
|
|
344
|
+
"noCache"
|
|
345
|
+
],
|
|
346
|
+
params: (o) => ({
|
|
347
|
+
as_yhi: count(o.yearTo),
|
|
348
|
+
as_ylo: count(o.yearFrom),
|
|
349
|
+
hl: str(o.language),
|
|
350
|
+
num: bounded("google_scholar", o.limit, 1, 20),
|
|
351
|
+
safe: str(o.safe),
|
|
352
|
+
start: count(o.start)
|
|
353
|
+
}),
|
|
354
|
+
results: scholarResults
|
|
355
|
+
},
|
|
356
|
+
google_shopping: {
|
|
357
|
+
allowed: [
|
|
358
|
+
"country",
|
|
359
|
+
"language",
|
|
360
|
+
"location",
|
|
361
|
+
"safe",
|
|
362
|
+
"device",
|
|
363
|
+
"minPrice",
|
|
364
|
+
"maxPrice",
|
|
365
|
+
"sortBy",
|
|
366
|
+
"freeShipping",
|
|
367
|
+
"onSale",
|
|
368
|
+
"smallBusiness",
|
|
369
|
+
"noCache"
|
|
370
|
+
],
|
|
371
|
+
params: (o) => ({
|
|
372
|
+
device: str(o.device),
|
|
373
|
+
free_shipping: o.freeShipping === true ? true : void 0,
|
|
374
|
+
gl: googleCountry(o.country),
|
|
375
|
+
hl: str(o.language),
|
|
376
|
+
location: str(o.location),
|
|
377
|
+
max_price: count(o.maxPrice),
|
|
378
|
+
min_price: count(o.minPrice),
|
|
379
|
+
on_sale: o.onSale === true ? true : void 0,
|
|
380
|
+
safe: str(o.safe),
|
|
381
|
+
small_business: o.smallBusiness === true ? true : void 0,
|
|
382
|
+
sort_by: count(o.sortBy)
|
|
383
|
+
}),
|
|
384
|
+
results: shoppingResults
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
const IMAGE_OPTIONS = [
|
|
388
|
+
"country",
|
|
389
|
+
"language",
|
|
390
|
+
"location",
|
|
391
|
+
"recency",
|
|
392
|
+
"publishedAfter",
|
|
393
|
+
"publishedBefore",
|
|
394
|
+
"safe",
|
|
395
|
+
"device",
|
|
396
|
+
"page",
|
|
397
|
+
"aspectRatio",
|
|
398
|
+
"size",
|
|
399
|
+
"color",
|
|
400
|
+
"imageType",
|
|
401
|
+
"license",
|
|
402
|
+
"noCache"
|
|
403
|
+
];
|
|
404
|
+
const onlyAllowed = (engine, options, allowed) => {
|
|
405
|
+
const refused = Object.keys(options).filter((name) => options[name] !== void 0 && !allowed.includes(name));
|
|
406
|
+
if (refused.length > 0) throw invalid(`${engine} does not accept ${refused.join(", ")}`);
|
|
407
|
+
};
|
|
408
|
+
// src/adapters/serpapi.ts
|
|
409
|
+
const serpapi = (config = {}) => {
|
|
410
|
+
const key = requireKey(provider, config.apiKey, "SERPAPI_API_KEY");
|
|
411
|
+
const authenticated = (async (input, init) => {
|
|
412
|
+
const url = new URL(input);
|
|
413
|
+
url.searchParams.set("api_key", key);
|
|
414
|
+
return await (config.fetch ?? globalThis.fetch)(url, init);
|
|
415
|
+
});
|
|
416
|
+
const raw = createRawClient({
|
|
417
|
+
baseUrl: config.baseUrl ?? "https://serpapi.com",
|
|
418
|
+
fetch: authenticated,
|
|
419
|
+
headers: {},
|
|
420
|
+
provider
|
|
421
|
+
});
|
|
422
|
+
return defineAdapter({
|
|
423
|
+
name: provider,
|
|
424
|
+
operations: {
|
|
425
|
+
async images(query, options) {
|
|
426
|
+
const { call, rest: o } = callSettings(options);
|
|
427
|
+
onlyAllowed("google_images", o, IMAGE_OPTIONS);
|
|
428
|
+
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
429
|
+
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
430
|
+
const response = record(await raw.request("GET", "/search", {
|
|
431
|
+
...call,
|
|
432
|
+
query: compact({
|
|
433
|
+
device: o.device,
|
|
434
|
+
end_date: compactDate("publishedBefore", o.publishedBefore),
|
|
435
|
+
engine: "google_images",
|
|
436
|
+
gl: googleCountry(o.country),
|
|
437
|
+
hl: o.language,
|
|
438
|
+
ijn: o.page,
|
|
439
|
+
image_color: o.color,
|
|
440
|
+
image_type: o.imageType,
|
|
441
|
+
imgar: o.aspectRatio,
|
|
442
|
+
imgsz: o.size,
|
|
443
|
+
licenses: o.license,
|
|
444
|
+
location: o.location,
|
|
445
|
+
no_cache: o.noCache === true ? true : void 0,
|
|
446
|
+
period_unit: o.recency === void 0 ? void 0 : RECENCY_CODE[o.recency],
|
|
447
|
+
q: query,
|
|
448
|
+
safe: o.safe,
|
|
449
|
+
start_date: compactDate("publishedAfter", o.publishedAfter)
|
|
450
|
+
})
|
|
451
|
+
}));
|
|
452
|
+
failIfError(response);
|
|
453
|
+
const results = list(response.images_results).flatMap((entry) => {
|
|
454
|
+
const item = record(entry);
|
|
455
|
+
const url = text(item.original);
|
|
456
|
+
return url ? [compact({
|
|
457
|
+
height: num(item.original_height),
|
|
458
|
+
sourceUrl: text(item.link),
|
|
459
|
+
thumbnailUrl: text(item.thumbnail),
|
|
460
|
+
title: text(item.title),
|
|
461
|
+
url,
|
|
462
|
+
width: num(item.original_width)
|
|
463
|
+
})] : [];
|
|
464
|
+
});
|
|
465
|
+
return compact({
|
|
466
|
+
meta: searchMeta(response),
|
|
467
|
+
raw: response,
|
|
468
|
+
results
|
|
469
|
+
});
|
|
470
|
+
},
|
|
471
|
+
async search(query, options) {
|
|
472
|
+
const { call, rest } = callSettings(options);
|
|
473
|
+
const { engine = "google", ...chosen } = rest;
|
|
474
|
+
const o = { ...chosen };
|
|
475
|
+
if (!Object.hasOwn(ENGINES, engine)) throw invalid(`does not offer the engine "${engine}"`);
|
|
476
|
+
const spec = ENGINES[engine];
|
|
477
|
+
onlyAllowed(engine, o, spec.allowed);
|
|
478
|
+
const response = record(await raw.request("GET", "/search", {
|
|
479
|
+
...call,
|
|
480
|
+
query: compact({
|
|
481
|
+
engine,
|
|
482
|
+
q: query,
|
|
483
|
+
...spec.params(o),
|
|
484
|
+
no_cache: o.noCache === true ? true : void 0
|
|
485
|
+
})
|
|
486
|
+
}));
|
|
487
|
+
failIfError(response);
|
|
488
|
+
return compact({
|
|
489
|
+
answer: engine === "google" ? answerText(response) : void 0,
|
|
490
|
+
meta: searchMeta(response),
|
|
491
|
+
raw: response,
|
|
492
|
+
results: spec.results(response)
|
|
493
|
+
});
|
|
494
|
+
},
|
|
495
|
+
async usage(options) {
|
|
496
|
+
const { call } = callSettings(options);
|
|
497
|
+
const response = record(await raw.request("GET", "/account", call));
|
|
498
|
+
const safe = Object.fromEntries(Object.entries(response).filter(([name]) => name !== "api_key"));
|
|
499
|
+
const breakdown = {};
|
|
500
|
+
for (const name of ["this_hour_searches", "last_hour_searches"]) {
|
|
501
|
+
const value = num(response[name]);
|
|
502
|
+
if (value !== void 0) breakdown[name] = value;
|
|
503
|
+
}
|
|
504
|
+
return compact({
|
|
505
|
+
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
506
|
+
limit: num(response.searches_per_month),
|
|
507
|
+
periodEnd: text(response.plan_renewal_date),
|
|
508
|
+
raw: safe,
|
|
509
|
+
remaining: num(response.plan_searches_left),
|
|
510
|
+
used: num(response.this_month_usage)
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
raw
|
|
515
|
+
});
|
|
516
|
+
};
|
|
517
|
+
export { serpapi };
|