@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
|
@@ -1,6 +1,137 @@
|
|
|
1
|
-
import { n as codeForStatus, t as FieldworkError } from "./errors-
|
|
2
|
-
import {
|
|
3
|
-
import * as z from "zod/mini";
|
|
1
|
+
import { n as codeForStatus, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
2
|
+
import { b as parseJsonText, n as checkCallSettings, y as jsonValue } from "./util-LvME_uOj.js";
|
|
3
|
+
import * as z$1 from "zod/mini";
|
|
4
|
+
import { env } from "node:process";
|
|
5
|
+
// src/parse.ts
|
|
6
|
+
/** The result of a field that is absent or of the wrong type. */
|
|
7
|
+
const nothing = () => void 0;
|
|
8
|
+
/** Reads any value as absent. A union's last member, so a value of the wrong type does not fail. */
|
|
9
|
+
const absent = z$1.pipe(z$1.unknown(), z$1.transform(nothing));
|
|
10
|
+
/** A string that is not empty; any other value fails. Use inside `arrayOf`. */
|
|
11
|
+
const nonEmptyString = z$1.string().check(z$1.minLength(1));
|
|
12
|
+
/** The string, or undefined when the value is absent, empty or not a string. */
|
|
13
|
+
const optionalString = z$1.optional(z$1.union([nonEmptyString, absent]));
|
|
14
|
+
/** The string, an empty one too, or undefined when the value is absent or not a string. */
|
|
15
|
+
const optionalAnyString = z$1.optional(z$1.union([z$1.string(), absent]));
|
|
16
|
+
/** The number, or undefined when the value is absent, not a number, NaN or infinite. */
|
|
17
|
+
const optionalNumber = z$1.optional(z$1.union([z$1.number(), absent]));
|
|
18
|
+
/** The boolean, or undefined when the value is absent or not a boolean. */
|
|
19
|
+
const optionalBoolean = z$1.optional(z$1.union([z$1.boolean(), absent]));
|
|
20
|
+
/**
|
|
21
|
+
* An object that keeps unknown keys. A value that is not an object (absent,
|
|
22
|
+
* null, an array, a string) reads as an object with every field absent, so
|
|
23
|
+
* nested reads need no `?.`. Declare every field with these blocks, so the
|
|
24
|
+
* object itself never fails.
|
|
25
|
+
*/
|
|
26
|
+
const looseObject = (shape) => {
|
|
27
|
+
const object = z$1.looseObject(shape);
|
|
28
|
+
return z$1.prefault(z$1.union([object, z$1.pipe(z$1.pipe(z$1.unknown(), z$1.transform(() => ({}))), object)]), {});
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* An array of the items that parse. A value that is not an array, or is
|
|
32
|
+
* absent, reads as an empty one. An item schema built from the blocks above
|
|
33
|
+
* never fails, so every position is kept; a strict one such as
|
|
34
|
+
* `nonEmptyString` drops the bad items.
|
|
35
|
+
*/
|
|
36
|
+
const arrayOf = (item) => z$1.prefault(z$1.union([z$1.pipe(z$1.array(z$1.unknown()), z$1.transform((entries) => entries.flatMap((entry) => {
|
|
37
|
+
const parsed = z$1.safeParse(item, entry);
|
|
38
|
+
return parsed.success ? [parsed.data] : [];
|
|
39
|
+
}))), z$1.pipe(z$1.unknown(), z$1.transform(() => []))]), []);
|
|
40
|
+
/**
|
|
41
|
+
* A dictionary keyed by string, its values read through `item`. A value that
|
|
42
|
+
* is not an object, or is absent, reads as an empty dictionary.
|
|
43
|
+
*/
|
|
44
|
+
const recordOf = (item) => z$1.prefault(z$1.union([z$1.record(z$1.string(), item), z$1.pipe(z$1.unknown(), z$1.transform(() => ({})))]), {});
|
|
45
|
+
/**
|
|
46
|
+
* A string, number or boolean read as a string: `"a"` is `"a"` (an empty
|
|
47
|
+
* string stays empty), `3` is `"3"` and `true` is `"true"`. Any other value,
|
|
48
|
+
* or an absent one, reads as undefined. Numbers must be finite.
|
|
49
|
+
*/
|
|
50
|
+
const optionalScalar = z$1.optional(z$1.union([
|
|
51
|
+
z$1.string(),
|
|
52
|
+
z$1.pipe(z$1.number(), z$1.transform(String)),
|
|
53
|
+
z$1.pipe(z$1.boolean(), z$1.transform(String)),
|
|
54
|
+
absent
|
|
55
|
+
]));
|
|
56
|
+
/** A warning entry: a string as it is, or an object's non-empty `message`, else its non-empty `detail`. */
|
|
57
|
+
const warningEntry = z$1.union([z$1.string(), z$1.pipe(looseObject({
|
|
58
|
+
detail: optionalString,
|
|
59
|
+
message: optionalString
|
|
60
|
+
}), z$1.transform((entry) => entry.message ?? entry.detail))]);
|
|
61
|
+
/**
|
|
62
|
+
* The warnings a provider reports, as a list of strings. A string (an empty
|
|
63
|
+
* one too) is one warning. An array gives its strings as they are and, for
|
|
64
|
+
* each object, its non-empty `message` or else its non-empty `detail`; any
|
|
65
|
+
* other entry is dropped. Anything else, or an absent value, is no warnings.
|
|
66
|
+
*/
|
|
67
|
+
const warnings = z$1.pipe(z$1.union([z$1.pipe(z$1.string(), z$1.transform((message) => [message])), arrayOf(warningEntry)]), z$1.transform((entries) => entries.filter((entry) => entry !== void 0)));
|
|
68
|
+
/** Any value, as it arrived. Absent reads as undefined. */
|
|
69
|
+
const anything = z$1.optional(jsonValue);
|
|
70
|
+
/**
|
|
71
|
+
* The item read through `item`, and the value as it arrived beside it, for a
|
|
72
|
+
* result that must keep its own `raw`. An absent value gives an `original` of
|
|
73
|
+
* undefined. `item` must never fail, so build it from the blocks above.
|
|
74
|
+
*/
|
|
75
|
+
const withOriginal = (item) => z$1.prefault(z$1.pipe(z$1.unknown(), z$1.transform((original) => ({
|
|
76
|
+
original,
|
|
77
|
+
value: z$1.parse(item, original)
|
|
78
|
+
}))), nothing);
|
|
79
|
+
/**
|
|
80
|
+
* An id that is a finite number or a non-empty string, as a string: `7`
|
|
81
|
+
* is `"7"`. Any other value, or an absent one, reads as undefined.
|
|
82
|
+
*/
|
|
83
|
+
const optionalId = z$1.optional(z$1.union([
|
|
84
|
+
z$1.pipe(z$1.number(), z$1.transform(String)),
|
|
85
|
+
nonEmptyString,
|
|
86
|
+
absent
|
|
87
|
+
]));
|
|
88
|
+
/**
|
|
89
|
+
* One value read two ways at once, for a key a provider's result needs
|
|
90
|
+
* twice: `first` is the value through `first`, `second` through `second`.
|
|
91
|
+
*/
|
|
92
|
+
const both = (first, second) => z$1.prefault(z$1.pipe(z$1.unknown(), z$1.transform((value) => ({
|
|
93
|
+
first: z$1.parse(first, value),
|
|
94
|
+
second: z$1.parse(second, value)
|
|
95
|
+
}))), nothing);
|
|
96
|
+
/** What a provider says about a cited source: its URL, title and excerpts. */
|
|
97
|
+
const sourceShape = {
|
|
98
|
+
excerpts: z$1.pipe(z$1.union([z$1.pipe(z$1.string(), z$1.transform((passage) => [passage])), arrayOf(z$1.string())]), z$1.transform((passages) => passages.length > 0 ? passages : void 0)),
|
|
99
|
+
title: optionalString,
|
|
100
|
+
url: optionalString
|
|
101
|
+
};
|
|
102
|
+
/** A cited source. */
|
|
103
|
+
const Source = looseObject(sourceShape);
|
|
104
|
+
/** A non-empty string, else a finite number, else undefined. */
|
|
105
|
+
const optionalStringOrNumber = z$1.optional(z$1.union([
|
|
106
|
+
nonEmptyString,
|
|
107
|
+
z$1.number(),
|
|
108
|
+
absent
|
|
109
|
+
]));
|
|
110
|
+
/** An object or an array as it arrived, or undefined for any other value, or an absent one. */
|
|
111
|
+
const optionalStructure = z$1.optional(z$1.union([
|
|
112
|
+
z$1.array(jsonValue),
|
|
113
|
+
z$1.record(z$1.string(), jsonValue),
|
|
114
|
+
absent
|
|
115
|
+
]));
|
|
116
|
+
/**
|
|
117
|
+
* A non-empty string; or, for an array, its first non-empty string. Any other
|
|
118
|
+
* value, or an absent one, reads as undefined.
|
|
119
|
+
*/
|
|
120
|
+
const optionalFirstString = z$1.optional(z$1.union([nonEmptyString, z$1.pipe(arrayOf(nonEmptyString), z$1.transform((items) => items[0]))]));
|
|
121
|
+
/**
|
|
122
|
+
* An object that keeps unknown keys, or undefined when the value is absent or
|
|
123
|
+
* is not an object (null, an array, a string). Declare every field with the
|
|
124
|
+
* blocks above, so an object never fails.
|
|
125
|
+
*/
|
|
126
|
+
const optionalObject = (shape) => z$1.optional(z$1.union([z$1.looseObject(shape), absent]));
|
|
127
|
+
/**
|
|
128
|
+
* Like `arrayOf`, but a value that is not an array, or is absent, reads as
|
|
129
|
+
* undefined instead of an empty array.
|
|
130
|
+
*/
|
|
131
|
+
const optionalArrayOf = (item) => z$1.optional(z$1.union([z$1.pipe(z$1.array(z$1.unknown()), z$1.transform((entries) => entries.flatMap((entry) => {
|
|
132
|
+
const parsed = z$1.safeParse(item, entry);
|
|
133
|
+
return parsed.success ? [parsed.data] : [];
|
|
134
|
+
}))), absent]));
|
|
4
135
|
/**
|
|
5
136
|
* Waits `ms`. When `signal` aborts it resolves early rather than rejecting, so
|
|
6
137
|
* the caller checks the signal afterwards. A signal already aborted on entry
|
|
@@ -10,16 +141,18 @@ import * as z from "zod/mini";
|
|
|
10
141
|
const sleep = async (ms, signal) => {
|
|
11
142
|
if (signal?.aborted === true) return;
|
|
12
143
|
const cleanup = new AbortController();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
144
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
145
|
+
const timer = setTimeout(() => {
|
|
146
|
+
resolve("elapsed");
|
|
147
|
+
}, ms);
|
|
148
|
+
signal?.addEventListener("abort", () => {
|
|
149
|
+
clearTimeout(timer);
|
|
150
|
+
resolve("aborted");
|
|
151
|
+
}, {
|
|
152
|
+
once: true,
|
|
153
|
+
signal: cleanup.signal
|
|
22
154
|
});
|
|
155
|
+
await promise;
|
|
23
156
|
cleanup.abort();
|
|
24
157
|
};
|
|
25
158
|
/** The longest wait between attempts; a provider asking for longer gets the error instead. */
|
|
@@ -64,17 +197,17 @@ const createFailure = (error) => {
|
|
|
64
197
|
});
|
|
65
198
|
};
|
|
66
199
|
/** The error for a fetch that threw: stopped by the signal (`Timeout` or `Aborted`), timed out on its own, or a lost connection, which can succeed on repeat. */
|
|
67
|
-
const transportError = (provider,
|
|
200
|
+
const transportError = (provider, cause, signal) => {
|
|
68
201
|
if (signal?.aborted === true) return stopped(signal, `${provider} request stopped: ${timedOut(signal) ? "timed out" : "cancelled"}`, {
|
|
69
|
-
cause
|
|
202
|
+
cause,
|
|
70
203
|
provider
|
|
71
204
|
});
|
|
72
|
-
if (
|
|
73
|
-
cause
|
|
205
|
+
if (cause instanceof DOMException && cause.name === "TimeoutError") return new FieldworkError("Timeout", `${provider} request timed out`, {
|
|
206
|
+
cause,
|
|
74
207
|
provider
|
|
75
208
|
});
|
|
76
|
-
return new FieldworkError("ProviderError", `${provider} request failed: ${String(
|
|
77
|
-
cause
|
|
209
|
+
return new FieldworkError("ProviderError", `${provider} request failed: ${String(cause)}`, {
|
|
210
|
+
cause,
|
|
78
211
|
provider,
|
|
79
212
|
retryable: true
|
|
80
213
|
});
|
|
@@ -87,6 +220,75 @@ const readText = async (provider, response, signal) => {
|
|
|
87
220
|
throw transportError(provider, error, signal);
|
|
88
221
|
}
|
|
89
222
|
};
|
|
223
|
+
const REDIRECTS = /* @__PURE__ */ new Set([
|
|
224
|
+
301,
|
|
225
|
+
302,
|
|
226
|
+
303,
|
|
227
|
+
307,
|
|
228
|
+
308
|
|
229
|
+
]);
|
|
230
|
+
const MAX_REDIRECTS = 5;
|
|
231
|
+
/**
|
|
232
|
+
* Follows a provider's redirects only within its own origin, so the key in
|
|
233
|
+
* the request's headers or query never reaches another host. A redirect to
|
|
234
|
+
* another origin, scheme or port fails before anything is sent there.
|
|
235
|
+
*/
|
|
236
|
+
const followRedirects = async (provider, send, first) => {
|
|
237
|
+
let hop = first;
|
|
238
|
+
for (let count = 0; count <= MAX_REDIRECTS; count += 1) {
|
|
239
|
+
const response = await send(hop);
|
|
240
|
+
if (response.type === "opaqueredirect") throw new FieldworkError("ProviderError", `${provider} redirected the request, and this runtime hides where to; Fieldwork does not follow it`, { provider });
|
|
241
|
+
const location = response.headers.get("location");
|
|
242
|
+
if (!REDIRECTS.has(response.status) || location === null) return response;
|
|
243
|
+
const target = new URL(location, hop.url);
|
|
244
|
+
if (target.origin !== first.url.origin) throw new FieldworkError("ProviderError", `${provider} redirected the request to ${target.origin}; Fieldwork follows redirects only within ${first.url.origin}, so the key was not sent`, {
|
|
245
|
+
provider,
|
|
246
|
+
status: response.status
|
|
247
|
+
});
|
|
248
|
+
hop = response.status === 303 || (response.status === 301 || response.status === 302) && hop.method === "POST" ? {
|
|
249
|
+
body: void 0,
|
|
250
|
+
method: "GET",
|
|
251
|
+
url: target
|
|
252
|
+
} : {
|
|
253
|
+
...hop,
|
|
254
|
+
url: target
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
throw new FieldworkError("ProviderError", `${provider} redirected the request more than ${MAX_REDIRECTS} times`, { provider });
|
|
258
|
+
};
|
|
259
|
+
const combineSignals = (signal, timeout) => {
|
|
260
|
+
const signals = [signal, timeout === void 0 ? void 0 : AbortSignal.timeout(timeout)].filter((value) => value !== void 0);
|
|
261
|
+
return signals.length <= 1 ? signals[0] : AbortSignal.any(signals);
|
|
262
|
+
};
|
|
263
|
+
/** The body's JSON, or undefined when the body is not JSON. An empty body is JSON with no value. */
|
|
264
|
+
const jsonOf = (text) => {
|
|
265
|
+
if (text === "") return { value: void 0 };
|
|
266
|
+
try {
|
|
267
|
+
return { value: parseJsonText(text) };
|
|
268
|
+
} catch {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
/** One message field of an error body: a string as it is, an object or array as JSON. */
|
|
273
|
+
const errorDetail = z$1.union([z$1.string(), z$1.pipe(optionalStructure, z$1.transform((value) => value === void 0 ? void 0 : JSON.stringify(value).slice(0, 300)))]);
|
|
274
|
+
/** An error body's message: its `error`, else `message`, else `detail`. */
|
|
275
|
+
const ErrorBody = z$1.pipe(looseObject({
|
|
276
|
+
detail: errorDetail,
|
|
277
|
+
error: errorDetail,
|
|
278
|
+
message: errorDetail
|
|
279
|
+
}), z$1.transform((body) => body.error ?? body.message ?? body.detail));
|
|
280
|
+
/**
|
|
281
|
+
* Seconds to wait from a `Retry-After` header, which is a number of seconds or
|
|
282
|
+
* an HTTP date. Undefined when the header is absent, empty or neither. `now`
|
|
283
|
+
* is for tests.
|
|
284
|
+
*/
|
|
285
|
+
const retryAfterSeconds = (header, now = Date.now()) => {
|
|
286
|
+
if (header === null || header === "") return;
|
|
287
|
+
const seconds = Number(header);
|
|
288
|
+
if (Number.isFinite(seconds)) return seconds;
|
|
289
|
+
const date = Date.parse(header);
|
|
290
|
+
return Number.isNaN(date) ? void 0 : Math.max(0, Math.ceil((date - now) / 1e3));
|
|
291
|
+
};
|
|
90
292
|
/**
|
|
91
293
|
* Builds the client for one provider. A request is sent to `baseUrl` plus its
|
|
92
294
|
* path, with JSON bodies and the config's headers, and `timeout` and `signal`
|
|
@@ -149,7 +351,7 @@ const createRawClient = (config) => {
|
|
|
149
351
|
});
|
|
150
352
|
return options.text === true ? text : parsed;
|
|
151
353
|
}
|
|
152
|
-
throw new FieldworkError(codeForStatus(response.status), `${config.provider} ${method} ${path} returned ${response.status}: ${
|
|
354
|
+
throw new FieldworkError(codeForStatus(response.status), `${config.provider} ${method} ${path} returned ${response.status}: ${z$1.parse(ErrorBody, parsed) ?? text.slice(0, 300)}`, {
|
|
153
355
|
cause: parsed,
|
|
154
356
|
provider: config.provider,
|
|
155
357
|
retryAfter: retryAfterSeconds(response.headers.get("retry-after")),
|
|
@@ -181,80 +383,6 @@ const createRawClient = (config) => {
|
|
|
181
383
|
}
|
|
182
384
|
};
|
|
183
385
|
};
|
|
184
|
-
const REDIRECTS = /* @__PURE__ */ new Set([
|
|
185
|
-
301,
|
|
186
|
-
302,
|
|
187
|
-
303,
|
|
188
|
-
307,
|
|
189
|
-
308
|
|
190
|
-
]);
|
|
191
|
-
const MAX_REDIRECTS = 5;
|
|
192
|
-
/**
|
|
193
|
-
* Follows a provider's redirects only within its own origin, so the key in
|
|
194
|
-
* the request's headers or query never reaches another host. A redirect to
|
|
195
|
-
* another origin, scheme or port fails before anything is sent there.
|
|
196
|
-
*/
|
|
197
|
-
const followRedirects = async (provider, send, first) => {
|
|
198
|
-
let hop = first;
|
|
199
|
-
for (let count = 0; count <= MAX_REDIRECTS; count += 1) {
|
|
200
|
-
const response = await send(hop);
|
|
201
|
-
if (response.type === "opaqueredirect") throw new FieldworkError("ProviderError", `${provider} redirected the request, and this runtime hides where to; Fieldwork does not follow it`, { provider });
|
|
202
|
-
const location = response.headers.get("location");
|
|
203
|
-
if (!REDIRECTS.has(response.status) || location === null) return response;
|
|
204
|
-
const target = new URL(location, hop.url);
|
|
205
|
-
if (target.origin !== first.url.origin) throw new FieldworkError("ProviderError", `${provider} redirected the request to ${target.origin}; Fieldwork follows redirects only within ${first.url.origin}, so the key was not sent`, {
|
|
206
|
-
provider,
|
|
207
|
-
status: response.status
|
|
208
|
-
});
|
|
209
|
-
hop = response.status === 303 || (response.status === 301 || response.status === 302) && hop.method === "POST" ? {
|
|
210
|
-
body: void 0,
|
|
211
|
-
method: "GET",
|
|
212
|
-
url: target
|
|
213
|
-
} : {
|
|
214
|
-
...hop,
|
|
215
|
-
url: target
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
throw new FieldworkError("ProviderError", `${provider} redirected the request more than ${MAX_REDIRECTS} times`, { provider });
|
|
219
|
-
};
|
|
220
|
-
const combineSignals = (signal, timeout) => {
|
|
221
|
-
const signals = [signal, timeout === void 0 ? void 0 : AbortSignal.timeout(timeout)].filter((value) => value !== void 0);
|
|
222
|
-
return signals.length <= 1 ? signals[0] : AbortSignal.any(signals);
|
|
223
|
-
};
|
|
224
|
-
/** The body's JSON, or undefined when the body is not JSON. An empty body is JSON with no value. */
|
|
225
|
-
const jsonOf = (text) => {
|
|
226
|
-
if (text === "") return { value: void 0 };
|
|
227
|
-
try {
|
|
228
|
-
return { value: JSON.parse(text) };
|
|
229
|
-
} catch {
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
const describe = (parsed, text) => {
|
|
234
|
-
const body = record(parsed);
|
|
235
|
-
for (const key of [
|
|
236
|
-
"error",
|
|
237
|
-
"message",
|
|
238
|
-
"detail"
|
|
239
|
-
]) {
|
|
240
|
-
const value = body[key];
|
|
241
|
-
if (typeof value === "string") return value;
|
|
242
|
-
if (typeof value === "object" && value !== null) return JSON.stringify(value).slice(0, 300);
|
|
243
|
-
}
|
|
244
|
-
return text.slice(0, 300);
|
|
245
|
-
};
|
|
246
|
-
/**
|
|
247
|
-
* Seconds to wait from a `Retry-After` header, which is a number of seconds or
|
|
248
|
-
* an HTTP date. Undefined when the header is absent, empty or neither. `now`
|
|
249
|
-
* is for tests.
|
|
250
|
-
*/
|
|
251
|
-
const retryAfterSeconds = (header, now = Date.now()) => {
|
|
252
|
-
if (header === null || header === "") return;
|
|
253
|
-
const seconds = Number(header);
|
|
254
|
-
if (Number.isFinite(seconds)) return seconds;
|
|
255
|
-
const date = Date.parse(header);
|
|
256
|
-
return Number.isNaN(date) ? void 0 : Math.max(0, Math.ceil((date - now) / 1e3));
|
|
257
|
-
};
|
|
258
386
|
/**
|
|
259
387
|
* An id as one path segment, so a caller's `resume` cannot reach another
|
|
260
388
|
* endpoint.
|
|
@@ -273,7 +401,7 @@ const segment = (id) => {
|
|
|
273
401
|
* Throws `Unauthorized` when there is no key or it is empty.
|
|
274
402
|
*/
|
|
275
403
|
const requireKey = (provider, given, envName) => {
|
|
276
|
-
const key = given ??
|
|
404
|
+
const key = given ?? env[envName];
|
|
277
405
|
if (key === void 0 || key === "") throw new FieldworkError("Unauthorized", `${provider} needs an API key: pass apiKey or set ${envName}`, { provider });
|
|
278
406
|
return key;
|
|
279
407
|
};
|
|
@@ -298,134 +426,4 @@ const readSigned = async (provider, url, options) => {
|
|
|
298
426
|
});
|
|
299
427
|
return text;
|
|
300
428
|
};
|
|
301
|
-
|
|
302
|
-
/** The result of a field that is absent or of the wrong type. */
|
|
303
|
-
const nothing = () => void 0;
|
|
304
|
-
/** Reads any value as absent. A union's last member, so a value of the wrong type does not fail. */
|
|
305
|
-
const absent = z.pipe(z.unknown(), z.transform(nothing));
|
|
306
|
-
/** A string that is not empty; any other value fails. Use inside `arrayOf`. */
|
|
307
|
-
const nonEmptyString = z.string().check(z.minLength(1));
|
|
308
|
-
/** The string, or undefined when the value is absent, empty or not a string. */
|
|
309
|
-
const optionalString = z.optional(z.union([nonEmptyString, absent]));
|
|
310
|
-
/** The string, an empty one too, or undefined when the value is absent or not a string. */
|
|
311
|
-
const optionalAnyString = z.optional(z.union([z.string(), absent]));
|
|
312
|
-
/** The number, or undefined when the value is absent, not a number, NaN or infinite. */
|
|
313
|
-
const optionalNumber = z.optional(z.union([z.number(), absent]));
|
|
314
|
-
/** The boolean, or undefined when the value is absent or not a boolean. */
|
|
315
|
-
const optionalBoolean = z.optional(z.union([z.boolean(), absent]));
|
|
316
|
-
/**
|
|
317
|
-
* An object that keeps unknown keys. A value that is not an object (absent,
|
|
318
|
-
* null, an array, a string) reads as an object with every field absent, so
|
|
319
|
-
* nested reads need no `?.`. Declare every field with these blocks, so the
|
|
320
|
-
* object itself never fails.
|
|
321
|
-
*/
|
|
322
|
-
const looseObject = (shape) => {
|
|
323
|
-
const object = z.looseObject(shape);
|
|
324
|
-
return z.prefault(z.union([object, z.pipe(z.pipe(z.unknown(), z.transform(() => ({}))), object)]), {});
|
|
325
|
-
};
|
|
326
|
-
/**
|
|
327
|
-
* An array of the items that parse. A value that is not an array, or is
|
|
328
|
-
* absent, reads as an empty one. An item schema built from the blocks above
|
|
329
|
-
* never fails, so every position is kept; a strict one such as
|
|
330
|
-
* `nonEmptyString` drops the bad items.
|
|
331
|
-
*/
|
|
332
|
-
const arrayOf = (item) => z.prefault(z.union([z.pipe(z.array(z.unknown()), z.transform((entries) => entries.flatMap((entry) => {
|
|
333
|
-
const parsed = z.safeParse(item, entry);
|
|
334
|
-
return parsed.success ? [parsed.data] : [];
|
|
335
|
-
}))), z.pipe(z.unknown(), z.transform(() => []))]), []);
|
|
336
|
-
/**
|
|
337
|
-
* A dictionary keyed by string, its values read through `item`. A value that
|
|
338
|
-
* is not an object, or is absent, reads as an empty dictionary.
|
|
339
|
-
*/
|
|
340
|
-
const recordOf = (item) => z.prefault(z.union([z.record(z.string(), item), z.pipe(z.unknown(), z.transform(() => ({})))]), {});
|
|
341
|
-
/**
|
|
342
|
-
* A string, number or boolean read as a string: `"a"` is `"a"` (an empty
|
|
343
|
-
* string stays empty), `3` is `"3"` and `true` is `"true"`. Any other value,
|
|
344
|
-
* or an absent one, reads as undefined. Numbers must be finite.
|
|
345
|
-
*/
|
|
346
|
-
const optionalScalar = z.optional(z.union([
|
|
347
|
-
z.string(),
|
|
348
|
-
z.pipe(z.number(), z.transform(String)),
|
|
349
|
-
z.pipe(z.boolean(), z.transform(String)),
|
|
350
|
-
absent
|
|
351
|
-
]));
|
|
352
|
-
/** A warning entry: a string as it is, or an object's non-empty `message`, else its non-empty `detail`. */
|
|
353
|
-
const warningEntry = z.union([z.string(), z.pipe(looseObject({
|
|
354
|
-
detail: optionalString,
|
|
355
|
-
message: optionalString
|
|
356
|
-
}), z.transform((entry) => entry.message ?? entry.detail))]);
|
|
357
|
-
/**
|
|
358
|
-
* The warnings a provider reports, as a list of strings. A string (an empty
|
|
359
|
-
* one too) is one warning. An array gives its strings as they are and, for
|
|
360
|
-
* each object, its non-empty `message` or else its non-empty `detail`; any
|
|
361
|
-
* other entry is dropped. Anything else, or an absent value, is no warnings.
|
|
362
|
-
*/
|
|
363
|
-
const warnings = z.pipe(z.union([z.pipe(z.string(), z.transform((message) => [message])), arrayOf(warningEntry)]), z.transform((entries) => entries.filter((entry) => entry !== void 0)));
|
|
364
|
-
/** Any value, as it arrived. Absent reads as undefined. */
|
|
365
|
-
const anything = z.optional(z.unknown());
|
|
366
|
-
/**
|
|
367
|
-
* The item read through `item`, and the value as it arrived beside it, for a
|
|
368
|
-
* result that must keep its own `raw`. An absent value gives an `original` of
|
|
369
|
-
* undefined. `item` must never fail, so build it from the blocks above.
|
|
370
|
-
*/
|
|
371
|
-
const withOriginal = (item) => z.prefault(z.pipe(z.unknown(), z.transform((original) => ({
|
|
372
|
-
original,
|
|
373
|
-
value: z.parse(item, original)
|
|
374
|
-
}))), nothing);
|
|
375
|
-
/**
|
|
376
|
-
* An id that is a finite number or a non-empty string, as a string: `7`
|
|
377
|
-
* is `"7"`. Any other value, or an absent one, reads as undefined.
|
|
378
|
-
*/
|
|
379
|
-
const optionalId = z.optional(z.union([
|
|
380
|
-
z.pipe(z.number(), z.transform(String)),
|
|
381
|
-
nonEmptyString,
|
|
382
|
-
absent
|
|
383
|
-
]));
|
|
384
|
-
/**
|
|
385
|
-
* One value read two ways at once, for a key a provider's result needs
|
|
386
|
-
* twice: `first` is the value through `first`, `second` through `second`.
|
|
387
|
-
*/
|
|
388
|
-
const both = (first, second) => z.prefault(z.pipe(z.unknown(), z.transform((value) => ({
|
|
389
|
-
first: z.parse(first, value),
|
|
390
|
-
second: z.parse(second, value)
|
|
391
|
-
}))), nothing);
|
|
392
|
-
/** What a provider says about a cited source: its URL, title and excerpts. */
|
|
393
|
-
const sourceShape = {
|
|
394
|
-
excerpts: z.pipe(z.union([z.pipe(z.string(), z.transform((passage) => [passage])), arrayOf(z.string())]), z.transform((passages) => passages.length > 0 ? passages : void 0)),
|
|
395
|
-
title: optionalString,
|
|
396
|
-
url: optionalString
|
|
397
|
-
};
|
|
398
|
-
/** A cited source. */
|
|
399
|
-
const Source = looseObject(sourceShape);
|
|
400
|
-
/** A non-empty string, else a finite number, else undefined. */
|
|
401
|
-
const optionalStringOrNumber = z.optional(z.union([
|
|
402
|
-
nonEmptyString,
|
|
403
|
-
z.number(),
|
|
404
|
-
absent
|
|
405
|
-
]));
|
|
406
|
-
/** An object or an array as it arrived, or undefined for any other value, or an absent one. */
|
|
407
|
-
const optionalStructure = z.optional(z.union([
|
|
408
|
-
z.array(z.unknown()),
|
|
409
|
-
z.record(z.string(), z.unknown()),
|
|
410
|
-
absent
|
|
411
|
-
]));
|
|
412
|
-
/**
|
|
413
|
-
* A non-empty string; or, for an array, its first non-empty string. Any other
|
|
414
|
-
* value, or an absent one, reads as undefined.
|
|
415
|
-
*/
|
|
416
|
-
const optionalFirstString = z.optional(z.union([nonEmptyString, z.pipe(arrayOf(nonEmptyString), z.transform((items) => items[0]))]));
|
|
417
|
-
/**
|
|
418
|
-
* An object that keeps unknown keys, or undefined when the value is absent or
|
|
419
|
-
* is not an object (null, an array, a string). Declare every field with the
|
|
420
|
-
* blocks above, so an object never fails.
|
|
421
|
-
*/
|
|
422
|
-
const optionalObject = (shape) => z.optional(z.union([z.looseObject(shape), absent]));
|
|
423
|
-
/**
|
|
424
|
-
* Like `arrayOf`, but a value that is not an array, or is absent, reads as
|
|
425
|
-
* undefined instead of an empty array.
|
|
426
|
-
*/
|
|
427
|
-
const optionalArrayOf = (item) => z.optional(z.union([z.pipe(z.array(z.unknown()), z.transform((entries) => entries.flatMap((entry) => {
|
|
428
|
-
const parsed = z.safeParse(item, entry);
|
|
429
|
-
return parsed.success ? [parsed.data] : [];
|
|
430
|
-
}))), absent]));
|
|
431
|
-
export { createRawClient as C, sleep as D, segment as E, stopped as O, withOriginal as S, requireKey as T, optionalStructure as _, looseObject as a, warningEntry as b, optionalArrayOf as c, optionalId as d, optionalNumber as f, optionalStringOrNumber as g, optionalString as h, both as i, optionalBoolean as l, optionalScalar as m, anything as n, nonEmptyString as o, optionalObject as p, arrayOf as r, optionalAnyString as s, Source as t, optionalFirstString as u, recordOf as v, readSigned as w, warnings as x, sourceShape as y };
|
|
429
|
+
export { optionalStructure as C, warnings as D, warningEntry as E, withOriginal as O, optionalStringOrNumber as S, sourceShape as T, optionalId as _, sleep as a, optionalScalar as b, anything as c, looseObject as d, nonEmptyString as f, optionalFirstString as g, optionalBoolean as h, segment as i, arrayOf as l, optionalArrayOf as m, readSigned as n, stopped as o, optionalAnyString as p, requireKey as r, Source as s, createRawClient as t, both as u, optionalNumber as v, recordOf as w, optionalString as x, optionalObject as y };
|