@howells/fieldwork-sdk 0.10.2 → 0.12.0
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 -16
- package/dist/{adapter-DWYJys20.js → adapter-CBSAfErv.js} +1 -1
- package/dist/adapter-J2yatZJ0.d.ts +73 -0
- package/dist/best-CO9liYDa.js +1436 -0
- package/dist/brave.d.ts +7 -6
- package/dist/brave.js +180 -173
- package/dist/browserbase.d.ts +4 -3
- package/dist/browserbase.js +185 -170
- package/dist/{document-DMIUCoDA.js → document-BSCwJgBM.js} +4 -4
- package/dist/{download-CP8noBdX.js → download-kuTqNnuZ.js} +1 -1
- package/dist/{adapter-DQrBP1tj.d.ts → errors-gMtejKHc.d.ts} +111 -70
- package/dist/exa.d.ts +18 -11
- package/dist/exa.js +306 -275
- package/dist/fieldwork-D7npPoUf.d.ts +35 -0
- package/dist/firecrawl.d.ts +4 -3
- package/dist/firecrawl.js +450 -417
- package/dist/{parse-Q7H4mrnO.js → http-CJcvTPnE.js} +223 -225
- package/dist/image.d.ts +253 -0
- package/dist/image.js +1 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.js +67 -82
- package/dist/{job-DnavRq_s.js → job-JLEpszjr.js} +9 -9
- package/dist/kernel.d.ts +20 -2
- package/dist/kernel.js +65 -19
- package/dist/llamaparse.d.ts +3 -2
- package/dist/llamaparse.js +16 -16
- package/dist/parallel.d.ts +9 -9
- package/dist/parallel.js +423 -389
- package/dist/path-DO2llIdp.js +10 -0
- package/dist/perplexity.d.ts +6 -5
- package/dist/perplexity.js +21 -21
- package/dist/reducto.d.ts +3 -2
- package/dist/reducto.js +14 -14
- package/dist/{schema-BFjS6CSz.d.ts → schema-C9xzRkKJ.d.ts} +17 -6
- package/dist/{schema--iU4AXSM.js → schema-CJXFAZ-x.js} +12 -3
- package/dist/serpapi.d.ts +63 -5
- package/dist/serpapi.js +374 -144
- package/dist/tavily.d.ts +4 -3
- package/dist/tavily.js +247 -234
- package/dist/tools.d.ts +76 -15
- package/dist/tools.js +522 -154
- package/dist/{types-rc8bl8Em.js → types-mq6d6YOK.js} +1 -0
- package/dist/{util-DFUXQX6v.js → util-LvME_uOj.js} +35 -11
- package/package.json +7 -2
- package/dist/fieldwork-JCfyw58T.d.ts +0 -113
- package/dist/path-DL5P8XH2.js +0 -17
- package/dist/util-DhYr1OS3.d.ts +0 -1
- package/dist/{errors-DkFJysIM.js → errors-gpxg6Tnh.js} +5 -5
package/dist/serpapi.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { t as defineAdapter } from "./adapter-
|
|
2
|
-
import { r as invalidRequest, t as FieldworkError } from "./errors-
|
|
3
|
-
import {
|
|
4
|
-
import { a as compact,
|
|
5
|
-
import * as z from "zod/mini";
|
|
1
|
+
import { t as defineAdapter } from "./adapter-CBSAfErv.js";
|
|
2
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
3
|
+
import { D as warnings, b as optionalScalar, d as looseObject, f as nonEmptyString, l as arrayOf, r as requireKey, t as createRawClient, v as optionalNumber, x as optionalString } from "./http-CJcvTPnE.js";
|
|
4
|
+
import { a as compact, d as refuseTogether, s as meta, t as callSettings } from "./util-LvME_uOj.js";
|
|
5
|
+
import * as z$1 from "zod/mini";
|
|
6
6
|
// src/adapters/serpapi/parse.ts
|
|
7
7
|
/** The name this adapter reports in errors and results. */
|
|
8
8
|
const provider = "serpapi";
|
|
@@ -164,6 +164,92 @@ const searchMeta = (body) => meta({
|
|
|
164
164
|
});
|
|
165
165
|
/** The reason a search failed, or undefined when it did not. */
|
|
166
166
|
const searchFailure = (body) => body.search_metadata.status === "Error" ? `serpapi search failed: ${body.error.message ?? "no reason given"}` : void 0;
|
|
167
|
+
/** Lens-shaped `visual_matches`, kind "visual": `link` is the page, `image` the matched image. */
|
|
168
|
+
const visualMatches = (body) => body.visual_matches.flatMap((item) => item.link === void 0 ? [] : [compact({
|
|
169
|
+
height: item.image_height,
|
|
170
|
+
kind: "visual",
|
|
171
|
+
siteName: item.source,
|
|
172
|
+
sourceUrl: item.link,
|
|
173
|
+
thumbnailUrl: item.thumbnail,
|
|
174
|
+
title: item.title,
|
|
175
|
+
url: item.image,
|
|
176
|
+
width: item.image_width
|
|
177
|
+
})]);
|
|
178
|
+
/**
|
|
179
|
+
* Google Lens: `exact_matches` (kind "exact"; the response names only the page and
|
|
180
|
+
* a thumbnail) then `visual_matches` (kind "visual", with the matched image).
|
|
181
|
+
* Whichever list the request's `type` did not ask for is empty. The `exact_matches`
|
|
182
|
+
* flag inside a visual match says exact matches of it exist; it does not make the
|
|
183
|
+
* match exact. A match with no `link` is left out.
|
|
184
|
+
*/
|
|
185
|
+
const lensMatches = (body) => [...body.exact_matches.flatMap((item) => item.link === void 0 ? [] : [compact({
|
|
186
|
+
height: item.actual_image_height,
|
|
187
|
+
kind: "exact",
|
|
188
|
+
publishedAt: item.date,
|
|
189
|
+
siteName: item.source,
|
|
190
|
+
sourceUrl: item.link,
|
|
191
|
+
thumbnailUrl: item.thumbnail,
|
|
192
|
+
title: item.title,
|
|
193
|
+
width: item.actual_image_width
|
|
194
|
+
})]), ...visualMatches(body)];
|
|
195
|
+
/**
|
|
196
|
+
* Google reverse image: `image_results`, the pages that show the image (the
|
|
197
|
+
* response does not say how closely, so no kind), then `visual_matches`.
|
|
198
|
+
*/
|
|
199
|
+
const googleReverseMatches = (body) => [...body.image_results.flatMap((item) => item.link === void 0 ? [] : [compact({
|
|
200
|
+
description: item.snippet,
|
|
201
|
+
siteName: item.source,
|
|
202
|
+
sourceUrl: item.link,
|
|
203
|
+
thumbnailUrl: item.thumbnail.link,
|
|
204
|
+
title: item.title
|
|
205
|
+
})]), ...visualMatches(body)];
|
|
206
|
+
/**
|
|
207
|
+
* Bing: `pages_with_this_image`, kind "exact". `source` is the page (the item's
|
|
208
|
+
* `link` is a Bing page) and `original` the image. `related_content`, similar
|
|
209
|
+
* images, is left in `raw`.
|
|
210
|
+
*/
|
|
211
|
+
const bingMatches = (body) => body.pages_with_this_image.flatMap((item) => item.source === void 0 ? [] : [compact({
|
|
212
|
+
height: item.height,
|
|
213
|
+
kind: "exact",
|
|
214
|
+
sourceUrl: item.source,
|
|
215
|
+
thumbnailUrl: item.thumbnail,
|
|
216
|
+
title: item.title,
|
|
217
|
+
url: item.original,
|
|
218
|
+
width: item.width
|
|
219
|
+
})]);
|
|
220
|
+
/** Yandex's "about the image" tab: `link` is the page, `original_image` the image. */
|
|
221
|
+
const yandexMatches = (body) => body.image_results.flatMap((item) => item.link === void 0 ? [] : [compact({
|
|
222
|
+
description: item.snippet,
|
|
223
|
+
height: item.original_image.height,
|
|
224
|
+
siteName: item.source,
|
|
225
|
+
sourceUrl: item.link,
|
|
226
|
+
thumbnailUrl: item.thumbnail.link,
|
|
227
|
+
title: item.title,
|
|
228
|
+
url: item.original_image.link,
|
|
229
|
+
width: item.original_image.width
|
|
230
|
+
})]);
|
|
231
|
+
/**
|
|
232
|
+
* Whether a match's page is a Google redirect instead of the page itself: `/url`,
|
|
233
|
+
* or the opaque `/goto?url=…` links Lens returns for most exact matches. A GET on
|
|
234
|
+
* either answers with a redirect to the page.
|
|
235
|
+
*/
|
|
236
|
+
const isGoogleRedirect = (url) => {
|
|
237
|
+
if (!URL.canParse(url)) return false;
|
|
238
|
+
const { hostname, pathname } = new URL(url);
|
|
239
|
+
return (hostname === "google.com" || hostname.endsWith(".google.com")) && (pathname === "/url" || pathname === "/goto");
|
|
240
|
+
};
|
|
241
|
+
/**
|
|
242
|
+
* `searchMeta`, plus a warning when some `sourceUrl` is a Google redirect link,
|
|
243
|
+
* which Lens returns for most exact matches, `noCache` or not. The link is kept
|
|
244
|
+
* as received.
|
|
245
|
+
*/
|
|
246
|
+
const reverseImageMeta = (body, results) => {
|
|
247
|
+
const redirects = results.filter((match) => isGoogleRedirect(match.sourceUrl)).length;
|
|
248
|
+
return meta({
|
|
249
|
+
requestId: body.search_metadata.id,
|
|
250
|
+
warnings: [...body.error.warnings, ...redirects === 0 ? [] : [`${redirects} sourceUrl ${redirects === 1 ? "is a Google redirect link" : "values are Google redirect links"}, not the page itself; a GET on the link redirects to the page`]]
|
|
251
|
+
});
|
|
252
|
+
};
|
|
167
253
|
// src/adapters/serpapi/options.ts
|
|
168
254
|
/**
|
|
169
255
|
* SerpApi's single-letter codes for the recency windows, as Google's `qdr` and
|
|
@@ -176,16 +262,12 @@ const RECENCY_CODE = {
|
|
|
176
262
|
week: "w",
|
|
177
263
|
year: "y"
|
|
178
264
|
};
|
|
179
|
-
/** The value if it is a string, else undefined. Engine specs read options as `Json`. */
|
|
180
|
-
const str = (value) => typeof value === "string" ? value : void 0;
|
|
181
|
-
/** The value if it is a number, else undefined. */
|
|
182
|
-
const count = (value) => typeof value === "number" ? value : void 0;
|
|
183
265
|
/**
|
|
184
266
|
* Google's `gl` code for a country: lower case, and "uk" for "gb", as SerpApi
|
|
185
267
|
* documents.
|
|
186
268
|
*/
|
|
187
269
|
const googleCountry = (value) => {
|
|
188
|
-
const code =
|
|
270
|
+
const code = value?.toLowerCase();
|
|
189
271
|
return code === "gb" ? "uk" : code;
|
|
190
272
|
};
|
|
191
273
|
/**
|
|
@@ -194,7 +276,7 @@ const googleCountry = (value) => {
|
|
|
194
276
|
*/
|
|
195
277
|
const isoDate = (name, value) => {
|
|
196
278
|
if (value === void 0) return;
|
|
197
|
-
if (
|
|
279
|
+
if (!/^\d{4}-\d{2}-\d{2}$/u.test(value)) throw invalidRequest(provider, `${name} must be YYYY-MM-DD`);
|
|
198
280
|
return value;
|
|
199
281
|
};
|
|
200
282
|
/** A YYYY-MM-DD date as YYYYMMDD. */
|
|
@@ -210,7 +292,7 @@ const usDate = (name, value) => {
|
|
|
210
292
|
/** A result count that is a whole number from `min` to `max`; refuses any other. */
|
|
211
293
|
const bounded = (engine, value, min, max) => {
|
|
212
294
|
if (value === void 0) return;
|
|
213
|
-
if (
|
|
295
|
+
if (!Number.isInteger(value) || value < min || value > max) throw invalidRequest(provider, `${engine} limit must be a whole number from ${min} to ${max}`);
|
|
214
296
|
return value;
|
|
215
297
|
};
|
|
216
298
|
/** The code for the recency window the caller set; undefined when it names none. */
|
|
@@ -265,10 +347,10 @@ const mapsOrigin = (o) => {
|
|
|
265
347
|
if (origin && o.zoom === void 0) throw invalidRequest(provider, "google_maps needs zoom with location or latitude and longitude");
|
|
266
348
|
if (!origin && o.zoom !== void 0) throw invalidRequest(provider, "google_maps zoom needs location or latitude and longitude");
|
|
267
349
|
return {
|
|
268
|
-
lat:
|
|
269
|
-
location:
|
|
270
|
-
lon:
|
|
271
|
-
z:
|
|
350
|
+
lat: o.latitude,
|
|
351
|
+
location: o.location,
|
|
352
|
+
lon: o.longitude,
|
|
353
|
+
z: o.zoom
|
|
272
354
|
};
|
|
273
355
|
};
|
|
274
356
|
// src/adapters/serpapi/engines.ts
|
|
@@ -293,12 +375,12 @@ const ENGINES = {
|
|
|
293
375
|
params: (o) => {
|
|
294
376
|
refuseTogether(provider, o, "country", "market");
|
|
295
377
|
return {
|
|
296
|
-
cc:
|
|
297
|
-
device:
|
|
298
|
-
first:
|
|
299
|
-
location:
|
|
300
|
-
mkt:
|
|
301
|
-
safeSearch:
|
|
378
|
+
cc: o.country?.toLowerCase(),
|
|
379
|
+
device: o.device,
|
|
380
|
+
first: o.first,
|
|
381
|
+
location: o.location,
|
|
382
|
+
mkt: o.market,
|
|
383
|
+
safeSearch: o.safeSearch
|
|
302
384
|
};
|
|
303
385
|
},
|
|
304
386
|
results: webResults
|
|
@@ -317,10 +399,10 @@ const ENGINES = {
|
|
|
317
399
|
],
|
|
318
400
|
params: (o) => ({
|
|
319
401
|
df: duckDuckGoFilter(o),
|
|
320
|
-
kl:
|
|
402
|
+
kl: o.region,
|
|
321
403
|
m: bounded("duckduckgo", o.limit, 1, 50),
|
|
322
|
-
safe:
|
|
323
|
-
start:
|
|
404
|
+
safe: o.safe,
|
|
405
|
+
start: o.start
|
|
324
406
|
}),
|
|
325
407
|
results: webResults
|
|
326
408
|
},
|
|
@@ -342,12 +424,12 @@ const ENGINES = {
|
|
|
342
424
|
"noCache"
|
|
343
425
|
],
|
|
344
426
|
params: (o) => ({
|
|
345
|
-
device:
|
|
427
|
+
device: o.device,
|
|
346
428
|
gl: googleCountry(o.country),
|
|
347
|
-
hl:
|
|
348
|
-
location:
|
|
349
|
-
safe:
|
|
350
|
-
start:
|
|
429
|
+
hl: o.language,
|
|
430
|
+
location: o.location,
|
|
431
|
+
safe: o.safe,
|
|
432
|
+
start: o.start,
|
|
351
433
|
tbs: googleTbs(o)
|
|
352
434
|
}),
|
|
353
435
|
results: webResults
|
|
@@ -367,10 +449,10 @@ const ENGINES = {
|
|
|
367
449
|
"noCache"
|
|
368
450
|
],
|
|
369
451
|
params: (o) => ({
|
|
452
|
+
hl: o.language,
|
|
370
453
|
type: "search",
|
|
371
|
-
hl: str(o.language),
|
|
372
454
|
...mapsOrigin(o),
|
|
373
|
-
start:
|
|
455
|
+
start: o.start
|
|
374
456
|
}),
|
|
375
457
|
results: placeResults
|
|
376
458
|
},
|
|
@@ -383,7 +465,7 @@ const ENGINES = {
|
|
|
383
465
|
],
|
|
384
466
|
params: (o) => ({
|
|
385
467
|
gl: googleCountry(o.country),
|
|
386
|
-
hl:
|
|
468
|
+
hl: o.language
|
|
387
469
|
}),
|
|
388
470
|
results: newsResults
|
|
389
471
|
},
|
|
@@ -402,13 +484,13 @@ const ENGINES = {
|
|
|
402
484
|
],
|
|
403
485
|
params: (o) => ({
|
|
404
486
|
after: patentDate("publishedAfter", o.publishedAfter),
|
|
405
|
-
assignee:
|
|
487
|
+
assignee: o.assignee,
|
|
406
488
|
before: patentDate("publishedBefore", o.publishedBefore),
|
|
407
|
-
inventor:
|
|
489
|
+
inventor: o.inventor,
|
|
408
490
|
num: bounded("google_patents", o.limit, 10, 100),
|
|
409
|
-
page:
|
|
410
|
-
sort:
|
|
411
|
-
status:
|
|
491
|
+
page: o.page,
|
|
492
|
+
sort: o.sort,
|
|
493
|
+
status: o.status
|
|
412
494
|
}),
|
|
413
495
|
results: patentResults
|
|
414
496
|
},
|
|
@@ -424,12 +506,12 @@ const ENGINES = {
|
|
|
424
506
|
"noCache"
|
|
425
507
|
],
|
|
426
508
|
params: (o) => ({
|
|
427
|
-
as_yhi:
|
|
428
|
-
as_ylo:
|
|
429
|
-
hl:
|
|
509
|
+
as_yhi: o.yearTo,
|
|
510
|
+
as_ylo: o.yearFrom,
|
|
511
|
+
hl: o.language,
|
|
430
512
|
num: bounded("google_scholar", o.limit, 1, 20),
|
|
431
|
-
safe:
|
|
432
|
-
start:
|
|
513
|
+
safe: o.safe,
|
|
514
|
+
start: o.start
|
|
433
515
|
}),
|
|
434
516
|
results: scholarResults
|
|
435
517
|
},
|
|
@@ -450,21 +532,88 @@ const ENGINES = {
|
|
|
450
532
|
"noCache"
|
|
451
533
|
],
|
|
452
534
|
params: (o) => ({
|
|
453
|
-
device:
|
|
535
|
+
device: o.device,
|
|
454
536
|
free_shipping: o.freeShipping === true ? true : void 0,
|
|
455
537
|
gl: googleCountry(o.country),
|
|
456
|
-
hl:
|
|
457
|
-
location:
|
|
458
|
-
max_price:
|
|
459
|
-
min_price:
|
|
538
|
+
hl: o.language,
|
|
539
|
+
location: o.location,
|
|
540
|
+
max_price: o.maxPrice,
|
|
541
|
+
min_price: o.minPrice,
|
|
460
542
|
on_sale: o.onSale === true ? true : void 0,
|
|
461
|
-
safe:
|
|
543
|
+
safe: o.safe,
|
|
462
544
|
small_business: o.smallBusiness === true ? true : void 0,
|
|
463
|
-
sort_by:
|
|
545
|
+
sort_by: o.sortBy
|
|
464
546
|
}),
|
|
465
547
|
results: shoppingResults
|
|
466
548
|
}
|
|
467
549
|
};
|
|
550
|
+
/**
|
|
551
|
+
* Lens's `type` for `match`, or undefined when it is left out. Refuses a value
|
|
552
|
+
* other than "exact" or "visual", and `query` with "exact", which Lens ignores:
|
|
553
|
+
* `q` applies only to `all` and `visual_matches`.
|
|
554
|
+
*/
|
|
555
|
+
const lensType = (o) => {
|
|
556
|
+
if (o.match === void 0) return;
|
|
557
|
+
if (o.match === "visual") return "visual_matches";
|
|
558
|
+
if (o.match !== "exact") throw invalidRequest(provider, "match must be \"exact\" or \"visual\"");
|
|
559
|
+
if (o.query !== void 0) throw invalidRequest(provider, "google_lens does not accept query with match \"exact\"; Lens applies q only to all and visual matches");
|
|
560
|
+
return "exact_matches";
|
|
561
|
+
};
|
|
562
|
+
/** One spec per `SerpApiReverseImageEngine`. None has a result count except Bing's `count`. */
|
|
563
|
+
const REVERSE_ENGINES = {
|
|
564
|
+
/** `limit` is `count`, 0 to 150 as SerpApi documents it, and is only a suggestion. */
|
|
565
|
+
bing_reverse_image: {
|
|
566
|
+
allowed: [
|
|
567
|
+
"limit",
|
|
568
|
+
"market",
|
|
569
|
+
"noCache"
|
|
570
|
+
],
|
|
571
|
+
imageParam: "image_url",
|
|
572
|
+
params: (o) => ({
|
|
573
|
+
count: bounded("bing_reverse_image", o.limit, 1, 150),
|
|
574
|
+
mkt: o.market
|
|
575
|
+
}),
|
|
576
|
+
results: bingMatches
|
|
577
|
+
},
|
|
578
|
+
google_lens: {
|
|
579
|
+
allowed: [
|
|
580
|
+
"country",
|
|
581
|
+
"language",
|
|
582
|
+
"query",
|
|
583
|
+
"match",
|
|
584
|
+
"noCache"
|
|
585
|
+
],
|
|
586
|
+
imageParam: "url",
|
|
587
|
+
params: (o) => ({
|
|
588
|
+
country: o.country?.toLowerCase(),
|
|
589
|
+
hl: o.language,
|
|
590
|
+
q: o.query,
|
|
591
|
+
type: lensType(o)
|
|
592
|
+
}),
|
|
593
|
+
results: lensMatches
|
|
594
|
+
},
|
|
595
|
+
google_reverse_image: {
|
|
596
|
+
allowed: [
|
|
597
|
+
"country",
|
|
598
|
+
"language",
|
|
599
|
+
"query",
|
|
600
|
+
"noCache"
|
|
601
|
+
],
|
|
602
|
+
imageParam: "image_url",
|
|
603
|
+
params: (o) => ({
|
|
604
|
+
gl: googleCountry(o.country),
|
|
605
|
+
hl: o.language,
|
|
606
|
+
q: o.query
|
|
607
|
+
}),
|
|
608
|
+
results: googleReverseMatches
|
|
609
|
+
},
|
|
610
|
+
yandex_images: {
|
|
611
|
+
allowed: ["noCache"],
|
|
612
|
+
imageParam: "url",
|
|
613
|
+
params: () => ({}),
|
|
614
|
+
results: yandexMatches
|
|
615
|
+
}
|
|
616
|
+
};
|
|
468
617
|
/** The options `images` takes. Google Images has no `limit`. */
|
|
469
618
|
const IMAGE_OPTIONS = [
|
|
470
619
|
"country",
|
|
@@ -488,15 +637,15 @@ const IMAGE_OPTIONS = [
|
|
|
488
637
|
* undefined passes.
|
|
489
638
|
*/
|
|
490
639
|
const onlyAllowed = (engine, options, allowed) => {
|
|
491
|
-
const refused = Object.
|
|
640
|
+
const refused = Object.entries(options).filter(([name, value]) => value !== void 0 && !allowed.includes(name)).map(([name]) => name);
|
|
492
641
|
if (refused.length > 0) throw invalidRequest(provider, `${engine} does not accept ${refused.join(", ")}`);
|
|
493
642
|
};
|
|
494
643
|
// src/adapters/serpapi/response.ts
|
|
495
644
|
/** SerpApi's `error` note: its text when it is a non-empty string, and the warnings it amounts to. */
|
|
496
|
-
const ErrorNote = z.union([z.pipe(nonEmptyString, z.transform((message) => ({
|
|
645
|
+
const ErrorNote = z$1.union([z$1.pipe(nonEmptyString, z$1.transform((message) => ({
|
|
497
646
|
message,
|
|
498
647
|
warnings: [message]
|
|
499
|
-
}))), z.pipe(warnings, z.transform((entries) => ({
|
|
648
|
+
}))), z$1.pipe(warnings, z$1.transform((entries) => ({
|
|
500
649
|
message: void 0,
|
|
501
650
|
warnings: entries
|
|
502
651
|
})))]);
|
|
@@ -519,7 +668,7 @@ const storyShape = {
|
|
|
519
668
|
iso_date: optionalString,
|
|
520
669
|
link: optionalString,
|
|
521
670
|
source: looseObject({
|
|
522
|
-
authors: arrayOf(z.string()),
|
|
671
|
+
authors: arrayOf(z$1.string()),
|
|
523
672
|
icon: optionalString
|
|
524
673
|
}),
|
|
525
674
|
thumbnail: optionalString,
|
|
@@ -578,23 +727,74 @@ const ImageItem = looseObject({
|
|
|
578
727
|
thumbnail: optionalString,
|
|
579
728
|
title: optionalString
|
|
580
729
|
});
|
|
730
|
+
/** A Google Lens visual match: `link` is the page, `image` the matched image. */
|
|
731
|
+
const VisualMatch = looseObject({
|
|
732
|
+
image: optionalString,
|
|
733
|
+
image_height: optionalNumber,
|
|
734
|
+
image_width: optionalNumber,
|
|
735
|
+
link: optionalString,
|
|
736
|
+
source: optionalString,
|
|
737
|
+
thumbnail: optionalString,
|
|
738
|
+
title: optionalString
|
|
739
|
+
});
|
|
740
|
+
/** A Google Lens exact match: `link` is the page. It has no image URL, only a thumbnail and the image's size. */
|
|
741
|
+
const ExactMatch = looseObject({
|
|
742
|
+
actual_image_height: optionalNumber,
|
|
743
|
+
actual_image_width: optionalNumber,
|
|
744
|
+
date: optionalString,
|
|
745
|
+
link: optionalString,
|
|
746
|
+
source: optionalString,
|
|
747
|
+
thumbnail: optionalString,
|
|
748
|
+
title: optionalString
|
|
749
|
+
});
|
|
750
|
+
/** A Bing page with the image: `source` is the page, `original` the image. `link` is a Bing page, not the source. */
|
|
751
|
+
const BingPage = looseObject({
|
|
752
|
+
height: optionalNumber,
|
|
753
|
+
original: optionalString,
|
|
754
|
+
source: optionalString,
|
|
755
|
+
thumbnail: optionalString,
|
|
756
|
+
title: optionalString,
|
|
757
|
+
width: optionalNumber
|
|
758
|
+
});
|
|
759
|
+
/** An image given as a URL, or as Yandex gives it: an object with `link` and the size. Either way, an object. */
|
|
760
|
+
const Pictured = z$1.union([z$1.pipe(nonEmptyString, z$1.transform((link) => ({ link }))), looseObject({
|
|
761
|
+
height: optionalNumber,
|
|
762
|
+
link: optionalString,
|
|
763
|
+
width: optionalNumber
|
|
764
|
+
})]);
|
|
765
|
+
/**
|
|
766
|
+
* A reverse image page. Google gives `link`, `snippet`, `title` and a `thumbnail`
|
|
767
|
+
* URL; Yandex gives the same with `thumbnail` and `original_image` as objects.
|
|
768
|
+
*/
|
|
769
|
+
const ImagePage = looseObject({
|
|
770
|
+
link: optionalString,
|
|
771
|
+
original_image: Pictured,
|
|
772
|
+
snippet: optionalString,
|
|
773
|
+
source: optionalString,
|
|
774
|
+
thumbnail: Pictured,
|
|
775
|
+
title: optionalString
|
|
776
|
+
});
|
|
581
777
|
/** A `/search` response for any engine; each engine reads only its own sections. */
|
|
582
778
|
const SearchResponse = looseObject({
|
|
583
|
-
answer_box: z.union([z.pipe(z.optional(z.null()), z.transform(() => void 0)), AnswerBox]),
|
|
779
|
+
answer_box: z$1.union([z$1.pipe(z$1.optional(z$1.null()), z$1.transform(() => void 0)), AnswerBox]),
|
|
584
780
|
answer_box_list: arrayOf(AnswerBox),
|
|
585
781
|
categorized_shopping_results: arrayOf(looseObject({ shopping_results: arrayOf(ShoppingItem) })),
|
|
586
782
|
error: ErrorNote,
|
|
783
|
+
exact_matches: arrayOf(ExactMatch),
|
|
784
|
+
image_results: arrayOf(ImagePage),
|
|
587
785
|
images_results: arrayOf(ImageItem),
|
|
588
786
|
inline_shopping_results: arrayOf(ShoppingItem),
|
|
589
787
|
local_results: arrayOf(Place),
|
|
590
788
|
news_results: arrayOf(NewsResult),
|
|
591
789
|
organic_results: arrayOf(Organic),
|
|
790
|
+
pages_with_this_image: arrayOf(BingPage),
|
|
592
791
|
place_results: Place,
|
|
593
792
|
search_metadata: looseObject({
|
|
594
793
|
id: optionalScalar,
|
|
595
794
|
status: optionalString
|
|
596
795
|
}),
|
|
597
|
-
shopping_results: arrayOf(ShoppingItem)
|
|
796
|
+
shopping_results: arrayOf(ShoppingItem),
|
|
797
|
+
visual_matches: arrayOf(VisualMatch)
|
|
598
798
|
});
|
|
599
799
|
/** `/account`: the plan and its usage. */
|
|
600
800
|
const AccountResponse = looseObject({
|
|
@@ -606,8 +806,120 @@ const AccountResponse = looseObject({
|
|
|
606
806
|
this_month_usage: optionalNumber
|
|
607
807
|
});
|
|
608
808
|
// src/adapters/serpapi.ts
|
|
809
|
+
/** One `/search` call: the response as received, and its parsed body. A failed search throws. */
|
|
810
|
+
const searchCall = async (raw, call, query) => {
|
|
811
|
+
const response = await raw.request("GET", "/search", {
|
|
812
|
+
...call,
|
|
813
|
+
query
|
|
814
|
+
});
|
|
815
|
+
const body = z$1.parse(SearchResponse, response);
|
|
816
|
+
const failure = searchFailure(body);
|
|
817
|
+
if (failure !== void 0) throw new FieldworkError("ProviderError", failure, {
|
|
818
|
+
cause: response,
|
|
819
|
+
provider,
|
|
820
|
+
retryable: true
|
|
821
|
+
});
|
|
822
|
+
return {
|
|
823
|
+
body,
|
|
824
|
+
response
|
|
825
|
+
};
|
|
826
|
+
};
|
|
827
|
+
/** `search` and `images`: one engine query each. */
|
|
828
|
+
const searching = (raw) => ({
|
|
829
|
+
async images(query, options = {}) {
|
|
830
|
+
const { call, rest: o } = callSettings(options);
|
|
831
|
+
onlyAllowed("google_images", o, IMAGE_OPTIONS);
|
|
832
|
+
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
833
|
+
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
834
|
+
const { body, response } = await searchCall(raw, call, compact({
|
|
835
|
+
device: o.device,
|
|
836
|
+
end_date: compactDate("publishedBefore", o.publishedBefore),
|
|
837
|
+
engine: "google_images",
|
|
838
|
+
gl: googleCountry(o.country),
|
|
839
|
+
hl: o.language,
|
|
840
|
+
ijn: o.page,
|
|
841
|
+
image_color: o.color,
|
|
842
|
+
image_type: o.imageType,
|
|
843
|
+
imgar: o.aspectRatio,
|
|
844
|
+
imgsz: o.size,
|
|
845
|
+
licenses: o.license,
|
|
846
|
+
location: o.location,
|
|
847
|
+
no_cache: o.noCache === true ? true : void 0,
|
|
848
|
+
period_unit: o.recency === void 0 ? void 0 : RECENCY_CODE[o.recency],
|
|
849
|
+
q: query,
|
|
850
|
+
safe: o.safe,
|
|
851
|
+
start_date: compactDate("publishedAfter", o.publishedAfter)
|
|
852
|
+
}));
|
|
853
|
+
return compact({
|
|
854
|
+
meta: searchMeta(body),
|
|
855
|
+
raw: response,
|
|
856
|
+
results: imageResults(body)
|
|
857
|
+
});
|
|
858
|
+
},
|
|
859
|
+
async search(query, options = {}) {
|
|
860
|
+
const { call, rest } = callSettings(options);
|
|
861
|
+
const { engine = "google", ...chosen } = rest;
|
|
862
|
+
const o = chosen;
|
|
863
|
+
if (!Object.hasOwn(ENGINES, engine)) throw invalidRequest(provider, `does not offer the engine "${engine}"`);
|
|
864
|
+
const spec = ENGINES[engine];
|
|
865
|
+
onlyAllowed(engine, o, spec.allowed);
|
|
866
|
+
const { body, response } = await searchCall(raw, call, compact({
|
|
867
|
+
engine,
|
|
868
|
+
q: query,
|
|
869
|
+
...spec.params(o),
|
|
870
|
+
no_cache: o.noCache === true ? true : void 0
|
|
871
|
+
}));
|
|
872
|
+
return compact({
|
|
873
|
+
answer: engine === "google" ? answerText(body) : void 0,
|
|
874
|
+
meta: searchMeta(body),
|
|
875
|
+
raw: response,
|
|
876
|
+
results: spec.results(body)
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
});
|
|
880
|
+
/** `reverseImage`: the pages an image appears on, through Google Lens. */
|
|
881
|
+
const matching = (raw) => ({ async reverseImage(imageUrl, options = {}) {
|
|
882
|
+
const { call, rest } = callSettings(options);
|
|
883
|
+
const { engine = "google_lens", ...chosen } = rest;
|
|
884
|
+
const o = chosen;
|
|
885
|
+
if (!Object.hasOwn(REVERSE_ENGINES, engine)) throw invalidRequest(provider, `does not offer the reverse image engine "${engine}"`);
|
|
886
|
+
if (!URL.canParse(imageUrl) || !/^https?:$/u.test(new URL(imageUrl).protocol)) throw invalidRequest(provider, "imageUrl must be an http or https URL");
|
|
887
|
+
const spec = REVERSE_ENGINES[engine];
|
|
888
|
+
onlyAllowed(engine, o, spec.allowed);
|
|
889
|
+
const { body, response } = await searchCall(raw, call, compact({
|
|
890
|
+
engine,
|
|
891
|
+
[spec.imageParam]: imageUrl,
|
|
892
|
+
...spec.params(o),
|
|
893
|
+
no_cache: o.noCache === true ? true : void 0
|
|
894
|
+
}));
|
|
895
|
+
const results = spec.results(body);
|
|
896
|
+
return compact({
|
|
897
|
+
meta: reverseImageMeta(body, results),
|
|
898
|
+
raw: response,
|
|
899
|
+
results
|
|
900
|
+
});
|
|
901
|
+
} });
|
|
902
|
+
/** `usage`: the account's searches this month. */
|
|
903
|
+
const accounting = (raw) => ({ async usage(options = {}) {
|
|
904
|
+
const { call } = callSettings(options);
|
|
905
|
+
const response = await raw.request("GET", "/account", call);
|
|
906
|
+
const body = z$1.parse(AccountResponse, response);
|
|
907
|
+
const fields = z$1.safeParse(z$1.record(z$1.string(), z$1.unknown()), response);
|
|
908
|
+
const safe = fields.success ? Object.fromEntries(Object.entries(fields.data).filter(([name]) => name !== "api_key")) : response;
|
|
909
|
+
const breakdown = {};
|
|
910
|
+
if (body.this_hour_searches !== void 0) breakdown.this_hour_searches = body.this_hour_searches;
|
|
911
|
+
if (body.last_hour_searches !== void 0) breakdown.last_hour_searches = body.last_hour_searches;
|
|
912
|
+
return compact({
|
|
913
|
+
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
914
|
+
limit: body.searches_per_month,
|
|
915
|
+
periodEnd: body.plan_renewal_date,
|
|
916
|
+
raw: safe,
|
|
917
|
+
remaining: body.plan_searches_left,
|
|
918
|
+
used: body.this_month_usage
|
|
919
|
+
});
|
|
920
|
+
} });
|
|
609
921
|
/**
|
|
610
|
-
* An adapter for SerpApi: search, images and usage. Pass it to `
|
|
922
|
+
* An adapter for SerpApi: search, images, reverse image and usage. Pass it to `fieldwork({ adapter })`.
|
|
611
923
|
*
|
|
612
924
|
* Throws `Unauthorized` when neither `apiKey` nor `SERPAPI_API_KEY` is set.
|
|
613
925
|
*/
|
|
@@ -625,96 +937,14 @@ const serpapi = (config = {}) => {
|
|
|
625
937
|
provider,
|
|
626
938
|
retries: config.retries
|
|
627
939
|
});
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
query
|
|
633
|
-
});
|
|
634
|
-
const body = z.parse(SearchResponse, response);
|
|
635
|
-
const failure = searchFailure(body);
|
|
636
|
-
if (failure !== void 0) throw new FieldworkError("ProviderError", failure, {
|
|
637
|
-
cause: response,
|
|
638
|
-
provider,
|
|
639
|
-
retryable: true
|
|
640
|
-
});
|
|
641
|
-
return {
|
|
642
|
-
body,
|
|
643
|
-
response
|
|
644
|
-
};
|
|
940
|
+
const operations = {
|
|
941
|
+
...accounting(raw),
|
|
942
|
+
...matching(raw),
|
|
943
|
+
...searching(raw)
|
|
645
944
|
};
|
|
646
945
|
return defineAdapter({
|
|
647
946
|
name: provider,
|
|
648
|
-
operations
|
|
649
|
-
async images(query, options) {
|
|
650
|
-
const { call, rest: o } = callSettings(options);
|
|
651
|
-
onlyAllowed("google_images", o, IMAGE_OPTIONS);
|
|
652
|
-
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
653
|
-
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
654
|
-
const { body, response } = await searchCall(call, compact({
|
|
655
|
-
device: o.device,
|
|
656
|
-
end_date: compactDate("publishedBefore", o.publishedBefore),
|
|
657
|
-
engine: "google_images",
|
|
658
|
-
gl: googleCountry(o.country),
|
|
659
|
-
hl: o.language,
|
|
660
|
-
ijn: o.page,
|
|
661
|
-
image_color: o.color,
|
|
662
|
-
image_type: o.imageType,
|
|
663
|
-
imgar: o.aspectRatio,
|
|
664
|
-
imgsz: o.size,
|
|
665
|
-
licenses: o.license,
|
|
666
|
-
location: o.location,
|
|
667
|
-
no_cache: o.noCache === true ? true : void 0,
|
|
668
|
-
period_unit: o.recency === void 0 ? void 0 : RECENCY_CODE[o.recency],
|
|
669
|
-
q: query,
|
|
670
|
-
safe: o.safe,
|
|
671
|
-
start_date: compactDate("publishedAfter", o.publishedAfter)
|
|
672
|
-
}));
|
|
673
|
-
return compact({
|
|
674
|
-
meta: searchMeta(body),
|
|
675
|
-
raw: response,
|
|
676
|
-
results: imageResults(body)
|
|
677
|
-
});
|
|
678
|
-
},
|
|
679
|
-
async search(query, options) {
|
|
680
|
-
const { call, rest } = callSettings(options);
|
|
681
|
-
const { engine = "google", ...chosen } = rest;
|
|
682
|
-
const o = { ...chosen };
|
|
683
|
-
if (!Object.hasOwn(ENGINES, engine)) throw invalidRequest(provider, `does not offer the engine "${engine}"`);
|
|
684
|
-
const spec = ENGINES[engine];
|
|
685
|
-
onlyAllowed(engine, o, spec.allowed);
|
|
686
|
-
const { body, response } = await searchCall(call, compact({
|
|
687
|
-
engine,
|
|
688
|
-
q: query,
|
|
689
|
-
...spec.params(o),
|
|
690
|
-
no_cache: o.noCache === true ? true : void 0
|
|
691
|
-
}));
|
|
692
|
-
return compact({
|
|
693
|
-
answer: engine === "google" ? answerText(body) : void 0,
|
|
694
|
-
meta: searchMeta(body),
|
|
695
|
-
raw: response,
|
|
696
|
-
results: spec.results(body)
|
|
697
|
-
});
|
|
698
|
-
},
|
|
699
|
-
async usage(options) {
|
|
700
|
-
const { call } = callSettings(options);
|
|
701
|
-
const response = await raw.request("GET", "/account", call);
|
|
702
|
-
const body = z.parse(AccountResponse, response);
|
|
703
|
-
const fields = z.safeParse(z.record(z.string(), z.unknown()), response);
|
|
704
|
-
const safe = fields.success ? Object.fromEntries(Object.entries(fields.data).filter(([name]) => name !== "api_key")) : response;
|
|
705
|
-
const breakdown = {};
|
|
706
|
-
if (body.this_hour_searches !== void 0) breakdown.this_hour_searches = body.this_hour_searches;
|
|
707
|
-
if (body.last_hour_searches !== void 0) breakdown.last_hour_searches = body.last_hour_searches;
|
|
708
|
-
return compact({
|
|
709
|
-
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
710
|
-
limit: body.searches_per_month,
|
|
711
|
-
periodEnd: body.plan_renewal_date,
|
|
712
|
-
raw: safe,
|
|
713
|
-
remaining: body.plan_searches_left,
|
|
714
|
-
used: body.this_month_usage
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
},
|
|
947
|
+
operations,
|
|
718
948
|
raw
|
|
719
949
|
});
|
|
720
950
|
};
|