@howells/fieldwork-sdk 0.11.0 → 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 +72 -15
- 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-B8kAAAgF.d.ts → errors-gMtejKHc.d.ts} +87 -69
- 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 +66 -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 +8 -8
- 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-CAPSX7yQ.d.ts → schema-C9xzRkKJ.d.ts} +17 -6
- package/dist/{schema--iU4AXSM.js → schema-CJXFAZ-x.js} +12 -3
- package/dist/serpapi.d.ts +3 -3
- package/dist/serpapi.js +176 -171
- package/dist/tavily.d.ts +4 -3
- package/dist/tavily.js +247 -234
- package/dist/tools.d.ts +76 -15
- package/dist/tools.js +351 -33
- package/dist/{util-DFUXQX6v.js → util-LvME_uOj.js} +35 -11
- package/package.json +6 -1
- package/dist/fieldwork-BrfoehvG.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/README.md
CHANGED
|
@@ -7,11 +7,11 @@ npm i @howells/fieldwork-sdk
|
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
|
-
import {
|
|
10
|
+
import { fieldwork } from "@howells/fieldwork-sdk";
|
|
11
11
|
import { firecrawl } from "@howells/fieldwork-sdk/firecrawl";
|
|
12
12
|
|
|
13
13
|
// Reads FIRECRAWL_API_KEY
|
|
14
|
-
const web =
|
|
14
|
+
const web = fieldwork({ adapter: firecrawl() });
|
|
15
15
|
|
|
16
16
|
const { results } = await web.search("TileBar Portmore Aqua 3x8");
|
|
17
17
|
const page = await web.read(results[0].url);
|
|
@@ -22,7 +22,7 @@ Swap the adapter to switch providers. Everything below the constructor stays the
|
|
|
22
22
|
|
|
23
23
|
## Capabilities
|
|
24
24
|
|
|
25
|
-
Each instance has only the methods its provider offers, so a call the provider cannot serve does not compile. `
|
|
25
|
+
Each instance has only the methods its provider offers, so a call the provider cannot serve does not compile. `fieldwork({ adapter: tavily() }).act(...)` is a type error, because Tavily has no browser. Untyped JavaScript gets a `FieldworkError` with code `Unsupported`, and no request is sent. Nothing falls back to another provider, and nothing is emulated: a provider without an answer endpoint has no `answer`, even though search plus a model could imitate one.
|
|
26
26
|
|
|
27
27
|
| Method | Exa | Firecrawl | Tavily | Parallel | Perplexity | Brave | SerpApi | Browserbase | Kernel | LlamaParse | Reducto |
|
|
28
28
|
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
|
@@ -39,7 +39,7 @@ Each instance has only the methods its provider offers, so a call the provider c
|
|
|
39
39
|
| `research(question)` | yes | yes | yes | yes | yes | | | | | | |
|
|
40
40
|
| `discover(objective, { entityType, conditions })` | | | | yes | | | | | | | |
|
|
41
41
|
| `decide(context, questions)` | | | | yes | yes | | | | | | |
|
|
42
|
-
| `act(code)` | | yes | | | | | | |
|
|
42
|
+
| `act(code)` | | yes | | | | | | | yes | | |
|
|
43
43
|
| `browser.open()`, `browser.close(id)` | | yes | | | | | | yes | yes | | |
|
|
44
44
|
| `parse(document)` | | yes | | | | | | | | yes | yes |
|
|
45
45
|
| `download(url)` | | yes | | | | | | yes | yes | | |
|
|
@@ -50,8 +50,8 @@ Each instance has only the methods its provider offers, so a call the provider c
|
|
|
50
50
|
|
|
51
51
|
- `answer` is a provider's own cited answer, returned in one call. `research` is a longer job of many steps, polled until it finishes.
|
|
52
52
|
- `discover` returns a list of entities, such as companies or products, that meet every condition you give.
|
|
53
|
-
- `decide` answers closed questions about a context: yes or no, a choice, or a score on a rubric, each with a probability.
|
|
54
|
-
- `browser.open()` starts a remote browser and returns its CDP URL for Playwright or Puppeteer; `act(code)` runs one script in a fresh session
|
|
53
|
+
- `decide` answers closed questions about a context: yes or no, a choice, or a score on a rubric, each with a probability. Parallel's `decide` searches the live web and returns citations; Perplexity's `decide` judges only the given `context` (`citations` is empty), which suits pure-context checks such as verifying whether retrieved passages support a claim.
|
|
54
|
+
- `browser.open()` starts a remote browser and returns its CDP URL for Playwright or Puppeteer; `act(code)` runs one script in a fresh session; on Kernel it is a Playwright script with `page`, `context` and `browser` in scope, and what it returns comes back as `result`.
|
|
55
55
|
- `download` returns a file's bytes, its content type and the final URL after redirects, fetched through the provider's network. See [Downloading files](#downloading-files).
|
|
56
56
|
- `reverseImage` takes an image's URL and returns the pages it appears on. Only SerpApi offers it, because it is the only provider that accepts an image as input. `engine` chooses where it looks: `"google_lens"` (the default), `"google_reverse_image"`, `"bing_reverse_image"` or `"yandex_images"`. Each match has `sourceUrl`, the page, on every engine; `url` is the matched image, and is absent from a Lens exact match, which names only the page. `kind` is `"exact"` for the same image and `"visual"` for a similar one, and is left out where the engine does not say. See [Reverse image search](#reverse-image-search).
|
|
57
57
|
- `monitor` repeats a search or a page check on a schedule and reports what changed. Exa and Parallel monitors watch a query; Firecrawl monitors also watch specific pages (`urls`).
|
|
@@ -73,6 +73,9 @@ Options are typed per provider. An option means the same thing under the same na
|
|
|
73
73
|
| `query` | On `read`: return the passages that answer it as `excerpts`. On `reverseImage`: text to refine the search with. |
|
|
74
74
|
| `match` | On `reverseImage`: `"exact"` for pages showing the same image, `"visual"` for similar ones. |
|
|
75
75
|
| `content` | On `search`: include each result's page text. |
|
|
76
|
+
| `maxTokens`, `maxTokensPerResult` | On `search`: total and per-result token caps for returned passages (Brave with `content: true`, Perplexity). |
|
|
77
|
+
| `maxCharsPerResult`, `maxCharsTotal` | On `search`, `read` and `readMany`: per-result and total character caps for returned excerpts or highlights (Exa, Parallel). |
|
|
78
|
+
| `maxContentChars` | On `read` and `readMany`: per-page character cap for full page `markdown` (Exa, Parallel). |
|
|
76
79
|
| `continueFrom` | On `answer` and `research`: the `continuationId` of an earlier result, to ask a follow-up. |
|
|
77
80
|
| `maxBytes` | On `download`: the largest file accepted, in bytes. Defaults to, and may not exceed, the provider's ceiling. |
|
|
78
81
|
| `resume`, `interval` | On jobs: collect an earlier job by its id, and the milliseconds between status checks (unset: 500, doubling to 2000). |
|
|
@@ -94,7 +97,7 @@ page.data; // { price: number; title: string }
|
|
|
94
97
|
|
|
95
98
|
Fieldwork turns the schema into the JSON Schema the provider reads, and checks the provider's output against your schema before returning it. Output that does not match throws `ProviderError` with the first paths that failed, such as `price: Invalid input: expected number, received string`. The error carries `jobId` when the call was a job. `data` and `output` are what your schema returns, so defaults and transforms apply.
|
|
96
99
|
|
|
97
|
-
A plain JSON Schema is sent as written and the output is not checked
|
|
100
|
+
A plain JSON Schema is sent as written and the output is not checked, so `data` is `unknown`: narrow it before reading it. Only a Standard Schema gives the result a type. Agent tools take plain JSON Schema only.
|
|
98
101
|
|
|
99
102
|
Providers differ in which keywords they accept; Exa reads draft 7 and the others draft 2020-12, and Fieldwork drops the `$schema` key. Tavily and Parallel accept only a subset, so keep schemas for them simple.
|
|
100
103
|
|
|
@@ -179,7 +182,7 @@ A response that is not 2xx throws (`NotFound` for 404 and 410), a web page throw
|
|
|
179
182
|
`reverseImage(imageUrl, options)` is SerpApi's. The image must be at a public `http` or `https` URL.
|
|
180
183
|
|
|
181
184
|
```ts
|
|
182
|
-
const web =
|
|
185
|
+
const web = fieldwork({ adapter: serpapi() });
|
|
183
186
|
|
|
184
187
|
const { results } = await web.reverseImage("https://example.com/chair.jpg", {
|
|
185
188
|
match: "exact",
|
|
@@ -206,13 +209,13 @@ results[0].sourceUrl; // the page the image appears on
|
|
|
206
209
|
`router` sends each capability to exactly one adapter:
|
|
207
210
|
|
|
208
211
|
```ts
|
|
209
|
-
import {
|
|
212
|
+
import { fieldwork, router } from "@howells/fieldwork-sdk";
|
|
210
213
|
import { brave } from "@howells/fieldwork-sdk/brave";
|
|
211
214
|
import { exa } from "@howells/fieldwork-sdk/exa";
|
|
212
215
|
import { firecrawl } from "@howells/fieldwork-sdk/firecrawl";
|
|
213
216
|
import { parallel } from "@howells/fieldwork-sdk/parallel";
|
|
214
217
|
|
|
215
|
-
const web =
|
|
218
|
+
const web = fieldwork({
|
|
216
219
|
adapter: router({
|
|
217
220
|
search: brave(),
|
|
218
221
|
read: firecrawl(),
|
|
@@ -262,13 +265,51 @@ Fieldwork repeats a request that fails in a way that can succeed on repeat, twic
|
|
|
262
265
|
A request that starts a job, session or monitor, or runs a browser script, is repeated only after a rate limit. After a 5xx the provider may already have started it, and a second attempt could charge twice. `timeout` bounds the whole call, retries included: a wait that would outlast it throws the error at once. `signal` stops the wait between attempts.
|
|
263
266
|
|
|
264
267
|
```ts
|
|
265
|
-
const web =
|
|
268
|
+
const web = fieldwork({ adapter: exa({ retries: 4 }) }); // every call
|
|
266
269
|
await web.search("tiles", { retries: 0 }); // this call only
|
|
267
270
|
```
|
|
268
271
|
|
|
272
|
+
## Best images
|
|
273
|
+
|
|
274
|
+
`@howells/fieldwork-sdk/image` returns the images on a page, best first, each at its largest known URL. It reads the page through your instance's `read` and renders it through its `act`, so every request goes through the provider; Fieldwork fetches nothing itself. The instance needs a `read` that returns raw HTML with `format: "raw"` and an `act` that runs a Playwright script: Kernel has both. An instance without a raw `read` and an `act` fails to compile, and one whose `act` is not Kernel's (Firecrawl's runs no Playwright script) throws `Unsupported` before any call, unless `render` is `"never"`.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
import { fieldwork } from "@howells/fieldwork-sdk";
|
|
278
|
+
import { kernel } from "@howells/fieldwork-sdk/kernel";
|
|
279
|
+
import { bestImages } from "@howells/fieldwork-sdk/image";
|
|
280
|
+
|
|
281
|
+
const web = fieldwork({ adapter: kernel() });
|
|
282
|
+
const { images } = await bestImages(web, "https://shop.example/linen-shirt", {
|
|
283
|
+
limit: 5,
|
|
284
|
+
});
|
|
285
|
+
images[0]; // { url, verified, variants, width, height, measured, sources: ["img", "srcset"], score }
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
It finds images named by og:image and twitter:image, JSON-LD, `img` (src, srcset and lazy-loading attributes such as `data-src`), `picture` sources, `noscript` fallbacks and inline background images. A srcset's entries, an image's lazy attributes and its placeholder are one entry: the largest is `url` and the others are `variants`.
|
|
289
|
+
|
|
290
|
+
Each image is then resolved to its largest original by the URL patterns of its image CDN: Condé Nast, Cloudinary, Cloudflare Images, Fastly IO, Shopify, Sanity, imgix, WordPress, Squarespace, Contentful, Prismic, Bunny, ImageKit, Storyblok, Builder and SpeedSize, and the usual resize parameters (`w`, `h`, `width`, `q` and the like) on any other host. A pattern strips resize parameters and size suffixes, or rewrites the URL to the master or original path. Resolution never fetches, so a URL a pattern built is a guess: it comes back with `verified: false`, and the page's own URL is first in `variants` to fall back to. `resolveOriginal(url)` is the same resolution on its own.
|
|
291
|
+
|
|
292
|
+
`render` says when the page is rendered in the provider's browser, which finds images only JavaScript loads and measures real sizes:
|
|
293
|
+
|
|
294
|
+
- `"auto"`, the default: read the HTML first, and render only when the read finds no image, its top pick has no size, or the top pick leads the next by less than 1.2×.
|
|
295
|
+
- `"never"`: read only.
|
|
296
|
+
- `"always"`: render without reading.
|
|
297
|
+
|
|
298
|
+
The render loads the page, notes its largest contentful paint before scrolling, scrolls in steps to wake lazy loading (8 seconds at most), collects the images with their rendered boxes, and loads the originals of the 12 likeliest as images to read their natural size (5 seconds at most); the whole script is limited to 90 seconds. A page whose own scripts keep the browser busy, as ads, trackers and chat widgets can, stops answering the script; each call into the page gets 10 seconds past its own cap, then the render fails with "the page stopped responding". A page that fails in the browser (it stops responding, times out or crashes the tab) is rendered once more with other sites' scripts blocked, which keeps ads, trackers and chat widgets out; the result's `meta.warnings` says so. Scripts are blocked only then, since some sites serve their own code from another domain and render nothing without it. An image that loaded has `measured: true` and its real size. After a render every `url` is one the page showed or one that loaded: a CDN original that did not load gives way to the page's own URL.
|
|
299
|
+
|
|
300
|
+
Images rank by pixel area: measured, else declared, else the original's size named in the CDN's URL (Sanity and Storyblok). The largest contentful paint counts double, and has `"lcp"` among its `sources`. Images in the site's header, nav or footer, SVGs (by a `.svg` path; `data:` URLs are never kept), and images whose known size is under 100 pixels on a side (icons, badges, small logos, 1×1 tracking pixels), are left out.
|
|
301
|
+
|
|
302
|
+
`timeout` bounds the read and the render together in milliseconds, `signal` cancels both, and `retries` applies to each, as on any call.
|
|
303
|
+
|
|
304
|
+
The `find_images` tool runs `bestImages` with `url`, `limit` and `render`. `toolsFor` makes it only when the instance's `read` returns raw HTML (Browserbase or Kernel) and its `act` runs the render script (Kernel), so Kernel on its own or a router that reads with Browserbase and acts with Kernel. It is not left out with `act`: it runs only its own script.
|
|
305
|
+
|
|
306
|
+
`rendered` says whether the page was rendered. `raw` holds each call's response, as `raw.read` and `raw.act`, a list with one entry per render; `meta` adds up the usage of every call, and keeps `requestId` only when one call ran. A page that answers 404 or 410 throws `NotFound`, and any other error status or an empty page throws `ProviderError`, with the `meta` of the calls so far, so an empty `images` always means the page has no images. For the same reason, a page with no images that is a bot check (Cloudflare's "Just a moment", a PerimeterX or DataDome captcha, or a reCAPTCHA or hCaptcha under a title that asks for a check), an "Access Denied" page or a consent wall throws `ProviderError` naming which, read or rendered. Under `"auto"`, a render that fails after a read that found images returns the read's images with `rendered: false` and the failure, a `FieldworkError`, as `renderError`; `meta` still counts the render. A render that fails anywhere else throws, with the usage so far.
|
|
307
|
+
|
|
308
|
+
`collectImages(document, baseUrl, probe?)` is the collector on its own: give it any DOM document and it returns every candidate it finds; in a rendered page, `probe` lays out each element. `collectImagesSource` is its source for a page script: an expression that evaluates to `collectImages`, with everything it calls defined beside it and nothing else in scope. `resolveOriginal` is one self-contained function, so `resolveOriginal.toString()` runs in a page the same way. `rankImages(candidates, measured?)` groups and ranks candidates as `bestImages` does; pass the natural sizes you measured, by URL, as `measured`, with `null` for a URL the browser could not load, which leaves that image out.
|
|
309
|
+
|
|
269
310
|
## Agent tools
|
|
270
311
|
|
|
271
|
-
`@howells/fieldwork-sdk/tools` turns an instance into tool definitions for a model, one per capability or group method: `search_web`, `search_images`, `search_by_image`, `read_page`, `read_pages`, `extract_values`, `read_product`, `map_site`, `crawl_site`, `answer_question`, `research_question`, `find_entities`, `make_decisions`, `parse_document`, `check_usage`, `run_browser_script`, `open_browser`, `close_browser`, `create_monitor`, `get_monitor`, `list_monitors`, `delete_monitor` and `list_monitor_events`. Each tool has:
|
|
312
|
+
`@howells/fieldwork-sdk/tools` turns an instance into tool definitions for a model, one per capability or group method: `search_web`, `search_images`, `search_by_image`, `read_page`, `read_pages`, `extract_values`, `read_product`, `find_images`, `map_site`, `crawl_site`, `answer_question`, `research_question`, `find_entities`, `make_decisions`, `parse_document`, `check_usage`, `run_browser_script`, `open_browser`, `close_browser`, `create_monitor`, `get_monitor`, `list_monitors`, `delete_monitor` and `list_monitor_events`. Each tool has:
|
|
272
313
|
|
|
273
314
|
- a description that says when to use it and which tool to use instead;
|
|
274
315
|
- a JSON Schema input that lists only the options its provider supports;
|
|
@@ -302,14 +343,30 @@ Exa extraction refuses JSON Schema `type` arrays (including nullable unions) wit
|
|
|
302
343
|
|
|
303
344
|
Tools that start remote browsers or create lasting, billed resources are left out unless you allow them: `toolsFor(web, { allow: ["act", "browser", "monitor"] })`. `toolError(error)` formats an error for a model.
|
|
304
345
|
|
|
346
|
+
To expose multiple providers behind one tool name without bloating the tool list — for example, both lexical search (`brave`) and semantic search (`exa`) behind one `search_web` tool — use `toolCatalog(clients, { defaults })`:
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
import { fieldwork } from "@howells/fieldwork-sdk";
|
|
350
|
+
import { brave } from "@howells/fieldwork-sdk/brave";
|
|
351
|
+
import { exa } from "@howells/fieldwork-sdk/exa";
|
|
352
|
+
import { toolCatalog } from "@howells/fieldwork-sdk/tools";
|
|
353
|
+
|
|
354
|
+
const { tools } = toolCatalog(
|
|
355
|
+
[fieldwork({ adapter: exa() }), fieldwork({ adapter: brave() })],
|
|
356
|
+
{ defaults: { search: "exa" } }
|
|
357
|
+
);
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Multi-provider tools advertise a `provider` enum with per-provider `oneOf` option branches and query-style guidance in `search_web`'s description; single-provider tools keep their plain schema. Fieldwork never substitutes another provider on error.
|
|
361
|
+
|
|
305
362
|
For MCP clients such as Claude, Cursor and Codex, [`@howells/fieldwork-mcp`](https://www.npmjs.com/package/@howells/fieldwork-mcp) serves these tools over stdio.
|
|
306
363
|
|
|
307
364
|
## The provider's own API
|
|
308
365
|
|
|
309
|
-
`web.raw.request(method, path, options)` calls the provider's HTTP API with authentication applied, for anything Fieldwork does not wrap:
|
|
366
|
+
`web.raw.request(method, path, options)` calls the provider's HTTP API with authentication applied, for anything Fieldwork does not wrap. It returns the response's JSON as a `JsonValue`, unchecked; parse it before reading it:
|
|
310
367
|
|
|
311
368
|
```ts
|
|
312
|
-
const check = await
|
|
369
|
+
const check = await fieldwork({ adapter: firecrawl() }).raw.request(
|
|
313
370
|
"GET",
|
|
314
371
|
`/monitor/${monitorId}/checks/${checkId}`
|
|
315
372
|
);
|
|
@@ -335,4 +392,4 @@ On a router, `raw` is keyed by provider name.
|
|
|
335
392
|
|
|
336
393
|
Pass `apiKey` (and Browserbase's `projectId`) to override, and `fetch` to supply your own. Requests follow redirects only within the provider's origin; a redirect to any other host, scheme or port fails before the key is sent there.
|
|
337
394
|
|
|
338
|
-
ESM only, Node 22 or later. The dependencies are Zod
|
|
395
|
+
ESM only, Node 22 or later. The dependencies are Zod, Turndown for Kernel's Markdown, and domino for parsing HTML in `/image`.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/adapter.ts
|
|
2
2
|
/**
|
|
3
|
-
* Types an adapter literal without widening it, so `
|
|
3
|
+
* Types an adapter literal without widening it, so `fieldwork` keeps each operation's own options.
|
|
4
4
|
*
|
|
5
5
|
* It returns its argument unchanged; write a custom adapter with it.
|
|
6
6
|
*/
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { l as CallOptions, ot as JsonValue, u as Capability } from "./errors-gMtejKHc.js";
|
|
2
|
+
/** Options for one request through an adapter's `raw` client. */
|
|
3
|
+
interface RequestOptions extends CallOptions {
|
|
4
|
+
/** A JSON body. */
|
|
5
|
+
body?: unknown;
|
|
6
|
+
/** A multipart body, sent as is. */
|
|
7
|
+
form?: FormData;
|
|
8
|
+
/** Query parameters. Undefined values are left out. */
|
|
9
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* The request creates a job, session or monitor that the provider bills for
|
|
12
|
+
* or keeps. After a 5xx or a dropped connection the provider may already
|
|
13
|
+
* have created it, so only a rate limit, which it has certainly not acted
|
|
14
|
+
* on, is retried, and any other failure is thrown not `retryable`.
|
|
15
|
+
*/
|
|
16
|
+
creates?: boolean;
|
|
17
|
+
/** The endpoint answers in text, such as an event stream, so its body is returned as a string. */
|
|
18
|
+
text?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The provider's HTTP API with authentication applied: the escape hatch for
|
|
22
|
+
* anything Fieldwork does not wrap. Paths are relative to the provider's base
|
|
23
|
+
* URL, so `raw.request("GET", "/usage")` calls the provider directly.
|
|
24
|
+
*/
|
|
25
|
+
interface RawClient {
|
|
26
|
+
readonly provider: string;
|
|
27
|
+
readonly baseUrl: string;
|
|
28
|
+
/**
|
|
29
|
+
* Sends a request and returns the response body: its JSON (an empty body is
|
|
30
|
+
* undefined), or its text when `options.text` is set. Parse the body before
|
|
31
|
+
* reading it. Retries as `CallOptions.retries` says.
|
|
32
|
+
*
|
|
33
|
+
* Throws `FieldworkError`: from the status for an error response, and
|
|
34
|
+
* `ProviderError` for a success whose body is not JSON, for a connection that
|
|
35
|
+
* failed, and for a redirect to another origin. A stopped call is `Timeout` or
|
|
36
|
+
* `Aborted`.
|
|
37
|
+
*/
|
|
38
|
+
request: (method: string, path: string, options?: RequestOptions) => Promise<JsonValue | undefined>;
|
|
39
|
+
}
|
|
40
|
+
/** An operation: it takes its own arguments and resolves to its result, or to nothing. */
|
|
41
|
+
type Method = (...args: never[]) => Promise<object> | Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* The methods an adapter implements, keyed by capability. Most capabilities
|
|
44
|
+
* are one method; `monitor` and `browser` are a group of methods.
|
|
45
|
+
*/
|
|
46
|
+
type Operations = Partial<Record<Capability, Method | Readonly<Record<string, Method>>>>;
|
|
47
|
+
/**
|
|
48
|
+
* A provider bound to its credentials. `operations` holds only the
|
|
49
|
+
* capabilities the provider offers, each typed with that provider's options,
|
|
50
|
+
* so a capability or option the provider lacks cannot be called.
|
|
51
|
+
*/
|
|
52
|
+
interface Adapter<Name extends string = string, Ops extends Operations = Operations, Raw = RawClient> {
|
|
53
|
+
/** The provider's name, such as "exa"; "router" for a router. It becomes `Fieldwork.provider`. */
|
|
54
|
+
readonly name: Name;
|
|
55
|
+
/** The capabilities the provider offers. Their absence is what makes `fieldwork` refuse a call with `Unsupported`. */
|
|
56
|
+
readonly operations: Ops;
|
|
57
|
+
/** The provider's HTTP API, for anything Fieldwork does not wrap. */
|
|
58
|
+
readonly raw: Raw;
|
|
59
|
+
/**
|
|
60
|
+
* The provider behind each capability, when it is not `name`. Only a router
|
|
61
|
+
* sets this.
|
|
62
|
+
*/
|
|
63
|
+
readonly providers?: Partial<Record<Capability, string>>;
|
|
64
|
+
}
|
|
65
|
+
/** Any adapter, whatever its provider and operations. Use it to accept an adapter generically. */
|
|
66
|
+
type AnyAdapter = Adapter<string, any, unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* Types an adapter literal without widening it, so `fieldwork` keeps each operation's own options.
|
|
69
|
+
*
|
|
70
|
+
* It returns its argument unchanged; write a custom adapter with it.
|
|
71
|
+
*/
|
|
72
|
+
declare const defineAdapter: <Name extends string, Ops extends Operations, Raw = RawClient>(adapter: Adapter<Name, Ops, Raw>) => Adapter<Name, Ops, Raw>;
|
|
73
|
+
export { RawClient as a, defineAdapter as i, AnyAdapter as n, RequestOptions as o, Operations as r, Adapter as t };
|