@nordhavnhq/beacon 0.5.0 → 0.6.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/dist/index.cjs +64 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +62 -1
- package/dist/index.d.ts +62 -1
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -208,6 +208,7 @@ var VersionTracker = class {
|
|
|
208
208
|
var PREVIEW_PARAM = "sl-preview";
|
|
209
209
|
var DEFAULT_BASE_URL = "https://beacon.nordhavn.ai";
|
|
210
210
|
var DEFAULT_TIMEOUT = 1e4;
|
|
211
|
+
var MIN_UPLOAD_TIMEOUT = 6e4;
|
|
211
212
|
var VERSION_TIMEOUT = 3e3;
|
|
212
213
|
var DEFAULT_WAIT_FOR_FRESH = 1e3;
|
|
213
214
|
var MAX_TIMER = 2147483647;
|
|
@@ -459,6 +460,58 @@ var HavenClient = class {
|
|
|
459
460
|
);
|
|
460
461
|
return res.record;
|
|
461
462
|
}
|
|
463
|
+
/**
|
|
464
|
+
* Upload one file for a `file` field, for a form that sends an attachment.
|
|
465
|
+
* Requires write scope on the dataset. Returns the file; hang it on a record
|
|
466
|
+
* in a second call by writing `{ uid }` into that field:
|
|
467
|
+
*
|
|
468
|
+
* ```ts
|
|
469
|
+
* const attachment = form.get('attachment')
|
|
470
|
+
* if (attachment instanceof File && attachment.size > 0) {
|
|
471
|
+
* const file = await studio.uploadFile('contact', 'requests', 'attachment', attachment)
|
|
472
|
+
* await studio.createRecord('contact', 'requests', { name, attachment: { uid: file.uid } })
|
|
473
|
+
* }
|
|
474
|
+
* ```
|
|
475
|
+
*
|
|
476
|
+
* The field decides what it takes: its `mimes` and `max_size` rules, within
|
|
477
|
+
* what the studio accepts from a key at all (pdf, jpg, png, webp; 10 MB). The
|
|
478
|
+
* studio reads the type from the content, so a renamed file is refused. Only
|
|
479
|
+
* top-level fields. A file of the wrong type or size throws `HavenError` 400
|
|
480
|
+
* with a message fit to show; other refusals (400, 403, 404) are setup
|
|
481
|
+
* errors. More than 10 uploads a minute per key throws 429. From a Node
|
|
482
|
+
* `Buffer`, send `new Blob([buffer])` with a `filename`.
|
|
483
|
+
*
|
|
484
|
+
* Server-side only: the key that uploads can write, and must never reach a
|
|
485
|
+
* browser. Not retried, not cached, and it leaves cached reads alone: until a
|
|
486
|
+
* record takes it up, the file belongs to no content. An upload that failed
|
|
487
|
+
* may still have been stored; an unused one is removed after 24 hours.
|
|
488
|
+
*/
|
|
489
|
+
async uploadFile(nodeSlug, datasetSlug, fieldSlug, file, opts) {
|
|
490
|
+
const filename = opts?.filename ?? file.name;
|
|
491
|
+
if (!filename) {
|
|
492
|
+
throw new Error("HavenClient.uploadFile: the file has no name; pass `filename` (with its extension)");
|
|
493
|
+
}
|
|
494
|
+
const form = new FormData();
|
|
495
|
+
form.set("file", file, filename);
|
|
496
|
+
form.set("nodeSlug", nodeSlug);
|
|
497
|
+
form.set("datasetSlug", datasetSlug);
|
|
498
|
+
form.set("fieldSlug", fieldSlug);
|
|
499
|
+
const url = `${this.baseUrl}/api/content/files`;
|
|
500
|
+
const timeout = uploadTimeout(this.timeout, opts?.timeout);
|
|
501
|
+
return withTimeout(url, timeout, async (signal) => {
|
|
502
|
+
const res = await this.fetchImpl(url, {
|
|
503
|
+
method: "POST",
|
|
504
|
+
// No Content-Type: fetch sets the multipart one, with its boundary.
|
|
505
|
+
headers: { Authorization: `Bearer ${this.apiKey}`, ...withoutContentType(this.headers) },
|
|
506
|
+
body: form,
|
|
507
|
+
signal
|
|
508
|
+
});
|
|
509
|
+
if (!res.ok) throw await this.toError(res);
|
|
510
|
+
const value = await res.json().catch(() => void 0);
|
|
511
|
+
if (!has("uid", (uid) => typeof uid === "string" && uid !== "")(value)) throw unexpectedBody(url);
|
|
512
|
+
return value;
|
|
513
|
+
});
|
|
514
|
+
}
|
|
462
515
|
/**
|
|
463
516
|
* Sends a write and settles the cached reads of its dataset with the outcome.
|
|
464
517
|
*
|
|
@@ -852,6 +905,10 @@ var DatasetHandle = class {
|
|
|
852
905
|
update(recordUid, data) {
|
|
853
906
|
return this.client.updateRecord(this.nodeSlug, this.datasetSlug, recordUid, data);
|
|
854
907
|
}
|
|
908
|
+
/** Upload a file for one of this dataset's file fields; see `HavenClient.uploadFile`. */
|
|
909
|
+
uploadFile(fieldSlug, file, opts) {
|
|
910
|
+
return this.client.uploadFile(this.nodeSlug, this.datasetSlug, fieldSlug, file, opts);
|
|
911
|
+
}
|
|
855
912
|
/** Clear cached reads for this dataset. */
|
|
856
913
|
clearCache() {
|
|
857
914
|
this.client.clearCache({ node: this.nodeSlug, dataset: this.datasetSlug });
|
|
@@ -892,6 +949,13 @@ function normalizeTimeout(value) {
|
|
|
892
949
|
if (typeof value !== "number" || Number.isNaN(value) || value < 0) return DEFAULT_TIMEOUT;
|
|
893
950
|
return value > MAX_TIMER ? 0 : value;
|
|
894
951
|
}
|
|
952
|
+
function uploadTimeout(clientTimeout, perCall) {
|
|
953
|
+
if (typeof perCall === "number" && perCall >= 0) return normalizeTimeout(perCall);
|
|
954
|
+
return clientTimeout === 0 ? 0 : Math.max(clientTimeout, MIN_UPLOAD_TIMEOUT);
|
|
955
|
+
}
|
|
956
|
+
function withoutContentType(headers) {
|
|
957
|
+
return Object.fromEntries(Object.entries(headers).filter(([name]) => name.toLowerCase() !== "content-type"));
|
|
958
|
+
}
|
|
895
959
|
function normalizeGatewayRetry(value) {
|
|
896
960
|
if (value === false) return void 0;
|
|
897
961
|
if (typeof value !== "number" || Number.isNaN(value) || value < 0) return DEFAULT_GATEWAY_RETRY;
|