@xberg-io/sdk 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -2
- package/dist/index.d.ts +3175 -677
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +87 -52
- package/dist/index.js.map +1 -1
- package/package.json +14 -11
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import createOpenApiClient from "openapi-fetch";
|
|
2
|
-
|
|
3
2
|
//#region src/errors.ts
|
|
4
3
|
var XbergError = class extends Error {
|
|
5
4
|
status;
|
|
@@ -109,7 +108,6 @@ function parseRetryAfter(value) {
|
|
|
109
108
|
if (Number.isNaN(parsed)) return;
|
|
110
109
|
return Math.max(0, Math.ceil((parsed - Date.now()) / 1e3));
|
|
111
110
|
}
|
|
112
|
-
|
|
113
111
|
//#endregion
|
|
114
112
|
//#region src/_internal.ts
|
|
115
113
|
/**
|
|
@@ -121,7 +119,6 @@ function parseRetryAfter(value) {
|
|
|
121
119
|
* because they appear in public client signatures.
|
|
122
120
|
*/
|
|
123
121
|
const DEFAULT_RETRY_BACKOFF_CAP_MS = 3e4;
|
|
124
|
-
const DEFAULT_BACKOFF_FACTOR = 2;
|
|
125
122
|
/**
|
|
126
123
|
* Caps an event-stream frame. The frames this endpoint sends are a few hundred
|
|
127
124
|
* bytes; the cap exists so a server that never closes a frame cannot grow this
|
|
@@ -172,7 +169,7 @@ function buildQueryString(params) {
|
|
|
172
169
|
}
|
|
173
170
|
function nextBackoffInterval(current, strategy) {
|
|
174
171
|
if (strategy === "constant") return current;
|
|
175
|
-
return Math.min(current *
|
|
172
|
+
return Math.min(current * 2, DEFAULT_RETRY_BACKOFF_CAP_MS);
|
|
176
173
|
}
|
|
177
174
|
function parseRetryAfterHeader(value) {
|
|
178
175
|
if (value === null) return;
|
|
@@ -256,7 +253,7 @@ var EventStreamDecoder = class {
|
|
|
256
253
|
for (;;) {
|
|
257
254
|
const boundary = this.nextLineBoundary();
|
|
258
255
|
if (boundary === void 0) {
|
|
259
|
-
if (this.buffer.length >
|
|
256
|
+
if (this.buffer.length > 1048576) throw new XbergError(`Crawl event stream sent a line longer than ${MAX_EVENT_STREAM_FRAME_BYTES} bytes`, {
|
|
260
257
|
status: 0,
|
|
261
258
|
body: null
|
|
262
259
|
});
|
|
@@ -300,7 +297,7 @@ var EventStreamDecoder = class {
|
|
|
300
297
|
if (value.startsWith(" ")) value = value.slice(1);
|
|
301
298
|
if (field === "data") {
|
|
302
299
|
this.size += value.length + 1;
|
|
303
|
-
if (this.size >
|
|
300
|
+
if (this.size > 1048576) throw new XbergError(`Crawl event frame exceeded ${MAX_EVENT_STREAM_FRAME_BYTES} bytes before the stream closed it`, {
|
|
304
301
|
status: 0,
|
|
305
302
|
body: null
|
|
306
303
|
});
|
|
@@ -324,7 +321,6 @@ function encodePathSegment(value) {
|
|
|
324
321
|
});
|
|
325
322
|
return encodeURIComponent(value);
|
|
326
323
|
}
|
|
327
|
-
|
|
328
324
|
//#endregion
|
|
329
325
|
//#region src/types.ts
|
|
330
326
|
/**
|
|
@@ -342,15 +338,6 @@ const TERMINAL_JOB_STATUSES = [
|
|
|
342
338
|
* "completed but check warnings" result from a hard failure.
|
|
343
339
|
*/
|
|
344
340
|
const SUCCESS_JOB_STATUSES = ["completed", "partial_success"];
|
|
345
|
-
|
|
346
|
-
//#endregion
|
|
347
|
-
//#region src/version.ts
|
|
348
|
-
/**
|
|
349
|
-
* Package version, synced from the repo-root `VERSION` file by
|
|
350
|
-
* `scripts/sync-versions.py`. Used to build the `User-Agent` header. ~keep
|
|
351
|
-
*/
|
|
352
|
-
const VERSION = "0.7.0";
|
|
353
|
-
|
|
354
341
|
//#endregion
|
|
355
342
|
//#region src/client.ts
|
|
356
343
|
/**
|
|
@@ -372,10 +359,10 @@ const VERSION = "0.7.0";
|
|
|
372
359
|
* and defaults to `baseUrl`. The backend methods use that origin and an optional
|
|
373
360
|
* separate `controlPlaneToken`; existing Pro methods retain their data-plane routing.
|
|
374
361
|
*/
|
|
375
|
-
const USER_AGENT = `xberg-io-sdk-typescript
|
|
362
|
+
const USER_AGENT = `xberg-io-sdk-typescript/0.8.1`;
|
|
376
363
|
const DEFAULT_TIMEOUT_MS = 3e4;
|
|
377
364
|
const DEFAULT_POLL_INTERVAL_MS = 1e3;
|
|
378
|
-
const DEFAULT_WAIT_TIMEOUT_MS =
|
|
365
|
+
const DEFAULT_WAIT_TIMEOUT_MS = 3e5;
|
|
379
366
|
const DEFAULT_RETRY_STATUSES = [
|
|
380
367
|
429,
|
|
381
368
|
502,
|
|
@@ -401,7 +388,7 @@ const OCTET_STREAM_MEDIA_TYPE = "application/octet-stream";
|
|
|
401
388
|
/** Media type of the crawl-event stream, sent as `Accept` and served as `Content-Type`. */
|
|
402
389
|
const EVENT_STREAM_MEDIA_TYPE = "text/event-stream";
|
|
403
390
|
/** The `kind` values `CrawlEventV1` declares. A frame carrying anything else is rejected. */
|
|
404
|
-
const CRAWL_EVENT_KINDS = new Set([
|
|
391
|
+
const CRAWL_EVENT_KINDS = /* @__PURE__ */ new Set([
|
|
405
392
|
"page",
|
|
406
393
|
"discovered",
|
|
407
394
|
"complete",
|
|
@@ -460,6 +447,14 @@ var XbergClient = class {
|
|
|
460
447
|
async getAuthConfig() {
|
|
461
448
|
return await this.backendRequest("GET", `/auth/config`, { backendPublic: true });
|
|
462
449
|
}
|
|
450
|
+
/** Enterprise backend `GET /auth/profile`. */
|
|
451
|
+
async getUserProfile() {
|
|
452
|
+
return await this.backendRequest("GET", "/auth/profile");
|
|
453
|
+
}
|
|
454
|
+
/** Enterprise backend `PATCH /auth/profile`. */
|
|
455
|
+
async updateUserProfile(body) {
|
|
456
|
+
return await this.backendRequest("PATCH", "/auth/profile", { json: body });
|
|
457
|
+
}
|
|
463
458
|
/** Enterprise backend `POST /auth/login`. */
|
|
464
459
|
async backendLogin(body) {
|
|
465
460
|
return await this.backendRequest("POST", `/auth/login`, {
|
|
@@ -713,6 +708,7 @@ var XbergClient = class {
|
|
|
713
708
|
async extract(params) {
|
|
714
709
|
const jobs = await this.extractBatch({
|
|
715
710
|
files: [params.file],
|
|
711
|
+
...params.projectId !== void 0 ? { projectId: params.projectId } : {},
|
|
716
712
|
...params.documentId !== void 0 ? { documentIds: [params.documentId] } : {},
|
|
717
713
|
...params.options !== void 0 ? { options: params.options } : {},
|
|
718
714
|
...params.webhook !== void 0 ? { webhook: params.webhook } : {},
|
|
@@ -737,6 +733,8 @@ var XbergClient = class {
|
|
|
737
733
|
const filenames = params.files.map(describeFile);
|
|
738
734
|
const documentIds = prepareDocumentLineage(filenames, params.documentIds);
|
|
739
735
|
if (documentIds.size > 0) await this.requireTier("enterprise", "extract document lineage");
|
|
736
|
+
if (params.projectId !== void 0) await this.requireTier("pro", "projectId");
|
|
737
|
+
const projectHeader = Object.keys(this.headers).find((name) => name.toLowerCase() === "x-project-id") ?? "X-Project-ID";
|
|
740
738
|
const form = new FormData();
|
|
741
739
|
for (const [filename, documentId] of documentIds) form.append(`document_id-${filename}`, documentId);
|
|
742
740
|
const parts = params.files.map(toBlob);
|
|
@@ -744,7 +742,10 @@ var XbergClient = class {
|
|
|
744
742
|
if (params.options !== void 0) form.append("options", JSON.stringify(params.options));
|
|
745
743
|
if (params.webhook !== void 0) form.append("webhook", JSON.stringify(params.webhook));
|
|
746
744
|
appendPerFileConfigs(form, filenames, params.configs);
|
|
747
|
-
const body = await this.requestJson("POST", "/v1/extract", {
|
|
745
|
+
const body = await this.requestJson("POST", "/v1/extract", {
|
|
746
|
+
body: form,
|
|
747
|
+
...params.projectId !== void 0 ? { headers: { [projectHeader]: params.projectId } } : {}
|
|
748
|
+
});
|
|
748
749
|
const jobIds = body.job_ids ?? [];
|
|
749
750
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
750
751
|
return parts.map(({ blob, filename }, index) => {
|
|
@@ -762,9 +763,9 @@ var XbergClient = class {
|
|
|
762
763
|
};
|
|
763
764
|
});
|
|
764
765
|
}
|
|
765
|
-
/** Fetch
|
|
766
|
-
async getJob(jobId) {
|
|
767
|
-
return await this.requestJson("GET", `${JOBS_PATH}/${encodePathSegment(jobId)}
|
|
766
|
+
/** Fetch a tagged extraction or crawl job record (`GET /v1/jobs/{id}`). */
|
|
767
|
+
async getJob(jobId, scope = {}) {
|
|
768
|
+
return parseJobResponse(await this.requestJson("GET", `${JOBS_PATH}/${encodePathSegment(jobId)}`, { params: await this.jobScope(scope) }));
|
|
768
769
|
}
|
|
769
770
|
/**
|
|
770
771
|
* Fetch a job's stored result document (`GET /v1/jobs/{id}/result`).
|
|
@@ -773,12 +774,13 @@ var XbergClient = class {
|
|
|
773
774
|
* extracted documents live in `results`. Distinct from {@link getJob}, which
|
|
774
775
|
* returns the job's metadata record.
|
|
775
776
|
*/
|
|
776
|
-
async getJobResult(jobId) {
|
|
777
|
-
return await this.requestJson("GET", `${JOBS_PATH}/${encodePathSegment(jobId)}/result
|
|
777
|
+
async getJobResult(jobId, scope = {}) {
|
|
778
|
+
return await this.requestJson("GET", `${JOBS_PATH}/${encodePathSegment(jobId)}/result`, { params: await this.jobScope(scope) });
|
|
778
779
|
}
|
|
779
780
|
/** List jobs (paginated) via `GET /v1/jobs`. */
|
|
780
781
|
async listJobs(params = {}) {
|
|
781
782
|
return await this.requestJson("GET", JOBS_PATH, { params: {
|
|
783
|
+
...await this.jobScope(params),
|
|
782
784
|
limit: params.limit,
|
|
783
785
|
offset: params.offset
|
|
784
786
|
} });
|
|
@@ -788,8 +790,8 @@ var XbergClient = class {
|
|
|
788
790
|
* cancellation and a job that already reached a terminal status answer
|
|
789
791
|
* `204`.
|
|
790
792
|
*/
|
|
791
|
-
async cancelJob(jobId) {
|
|
792
|
-
await this.requestJson("DELETE", `${JOBS_PATH}/${encodePathSegment(jobId)}
|
|
793
|
+
async cancelJob(jobId, scope = {}) {
|
|
794
|
+
await this.requestJson("DELETE", `${JOBS_PATH}/${encodePathSegment(jobId)}`, { params: await this.jobScope(scope) });
|
|
793
795
|
}
|
|
794
796
|
/**
|
|
795
797
|
* Poll {@link getJob} until the job reaches a terminal status. Throws
|
|
@@ -803,7 +805,11 @@ var XbergClient = class {
|
|
|
803
805
|
const start = Date.now();
|
|
804
806
|
let interval = initialInterval;
|
|
805
807
|
for (;;) {
|
|
806
|
-
const job = await this.getJob(jobId);
|
|
808
|
+
const job = await this.getJob(jobId, options);
|
|
809
|
+
if (job.job_type !== "extraction") throw new ValidationError("expected extraction job, received crawl job", {
|
|
810
|
+
status: 200,
|
|
811
|
+
body: job
|
|
812
|
+
});
|
|
807
813
|
if (TERMINAL_JOB_STATUSES.includes(job.status)) {
|
|
808
814
|
if (SUCCESS_JOB_STATUSES.includes(job.status)) return job;
|
|
809
815
|
throw new XbergError(`Job ${jobId} ended with status ${job.status}`, {
|
|
@@ -818,7 +824,7 @@ var XbergClient = class {
|
|
|
818
824
|
});
|
|
819
825
|
const delay = Math.min(interval, remaining);
|
|
820
826
|
await this.sleep(delay);
|
|
821
|
-
if (backoff === "exponential") interval = Math.min(interval *
|
|
827
|
+
if (backoff === "exponential") interval = Math.min(interval * 2, DEFAULT_RETRY_BACKOFF_CAP_MS);
|
|
822
828
|
}
|
|
823
829
|
}
|
|
824
830
|
/** Wait for many jobs in parallel. */
|
|
@@ -829,6 +835,7 @@ var XbergClient = class {
|
|
|
829
835
|
async extractAndWait(params) {
|
|
830
836
|
const extractParams = {
|
|
831
837
|
file: params.file,
|
|
838
|
+
...params.projectId !== void 0 ? { projectId: params.projectId } : {},
|
|
832
839
|
...params.documentId !== void 0 ? { documentId: params.documentId } : {},
|
|
833
840
|
...params.options !== void 0 ? { options: params.options } : {},
|
|
834
841
|
...params.webhook !== void 0 ? { webhook: params.webhook } : {},
|
|
@@ -836,6 +843,7 @@ var XbergClient = class {
|
|
|
836
843
|
};
|
|
837
844
|
const job = await this.extract(extractParams);
|
|
838
845
|
const waitOptions = {
|
|
846
|
+
...params.projectId !== void 0 ? { projectId: params.projectId } : {},
|
|
839
847
|
...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
|
|
840
848
|
...params.pollIntervalMs !== void 0 ? { pollIntervalMs: params.pollIntervalMs } : {},
|
|
841
849
|
...params.backoff !== void 0 ? { backoff: params.backoff } : {}
|
|
@@ -1042,17 +1050,20 @@ var XbergClient = class {
|
|
|
1042
1050
|
* (`GET /v1/jobs/{job_id}/pages/{page_number}`). The response is a PNG image —
|
|
1043
1051
|
* raw bytes, not JSON. `pageNumber` is 1-indexed.
|
|
1044
1052
|
*/
|
|
1045
|
-
async getJobPage(jobId, pageNumber) {
|
|
1053
|
+
async getJobPage(jobId, pageNumber, scope = {}) {
|
|
1046
1054
|
const path = `${JOBS_PATH}/${encodePathSegment(jobId)}/pages/${encodePathSegment(String(pageNumber))}`;
|
|
1047
|
-
return await this.requestBytes("GET", path);
|
|
1055
|
+
return await this.requestBytes("GET", path, { params: await this.jobScope(scope) });
|
|
1048
1056
|
}
|
|
1049
1057
|
/** Submit text for enrichment (`POST /v1/enrich`). */
|
|
1050
|
-
async submitEnrich(body) {
|
|
1051
|
-
return await this.requestJson("POST", ENRICH_PATH, {
|
|
1058
|
+
async submitEnrich(body, scope = {}) {
|
|
1059
|
+
return await this.requestJson("POST", ENRICH_PATH, {
|
|
1060
|
+
json: body,
|
|
1061
|
+
params: await this.jobScope(scope)
|
|
1062
|
+
});
|
|
1052
1063
|
}
|
|
1053
1064
|
/** Fetch an enrichment job's status (`GET /v1/enrich/{job_id}`). */
|
|
1054
|
-
async getEnrichStatus(jobId) {
|
|
1055
|
-
return await this.requestJson("GET", `${ENRICH_PATH}/${encodePathSegment(jobId)}
|
|
1065
|
+
async getEnrichStatus(jobId, scope = {}) {
|
|
1066
|
+
return await this.requestJson("GET", `${ENRICH_PATH}/${encodePathSegment(jobId)}`, { params: await this.jobScope(scope) });
|
|
1056
1067
|
}
|
|
1057
1068
|
/**
|
|
1058
1069
|
* Stream a crawl job's events (`GET /v1/crawl-jobs/{crawl_job_id}/events`,
|
|
@@ -1078,9 +1089,17 @@ var XbergClient = class {
|
|
|
1078
1089
|
*/
|
|
1079
1090
|
streamCrawlEvents(crawlJobId, options = {}) {
|
|
1080
1091
|
const path = `${CRAWL_JOBS_PATH}/${encodePathSegment(crawlJobId)}/events`;
|
|
1081
|
-
const open = () => this.openEventStream(path
|
|
1092
|
+
const open = async () => this.openEventStream(`${path}${buildQueryString(await this.jobScope(options))}`, options.signal);
|
|
1082
1093
|
return { [Symbol.asyncIterator]: () => iterateCrawlEvents(open) };
|
|
1083
1094
|
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Pro liveness and coarse license/capability state (`GET /healthz`).
|
|
1097
|
+
* Use readiness checks to assess dependencies. ~keep
|
|
1098
|
+
*/
|
|
1099
|
+
async proHealthz() {
|
|
1100
|
+
await this.requireTier("pro", "proHealthz");
|
|
1101
|
+
return this.requestJson("GET", "/healthz");
|
|
1102
|
+
}
|
|
1084
1103
|
/** Pro only: fetch the instance's accepted auth methods (`GET /auth/config`). */
|
|
1085
1104
|
async authConfig() {
|
|
1086
1105
|
await this.requireTier("pro", "authConfig");
|
|
@@ -1177,6 +1196,16 @@ var XbergClient = class {
|
|
|
1177
1196
|
await this.requireTier("pro", "getIntegration");
|
|
1178
1197
|
return this.requestJson("GET", this.integrationPath(projectId, integrationId));
|
|
1179
1198
|
}
|
|
1199
|
+
/** Pro only: read durable synchronization state for an integration. */
|
|
1200
|
+
async getIntegrationSync(projectId, integrationId) {
|
|
1201
|
+
await this.requireTier("pro", "getIntegrationSync");
|
|
1202
|
+
return this.requestJson("GET", `${this.integrationPath(projectId, integrationId)}/sync`);
|
|
1203
|
+
}
|
|
1204
|
+
/** Pro only: enable or disable durable synchronization for an integration. */
|
|
1205
|
+
async setIntegrationSync(projectId, integrationId, body) {
|
|
1206
|
+
await this.requireTier("pro", "setIntegrationSync");
|
|
1207
|
+
await this.requestJson("PUT", `${this.integrationPath(projectId, integrationId)}/sync`, { json: body });
|
|
1208
|
+
}
|
|
1180
1209
|
/** Pro only: delete an integration (`DELETE .../integrations/{integrationId}`). */
|
|
1181
1210
|
async deleteIntegration(projectId, integrationId) {
|
|
1182
1211
|
await this.requireTier("pro", "deleteIntegration");
|
|
@@ -1300,6 +1329,10 @@ var XbergClient = class {
|
|
|
1300
1329
|
const path = `${this.webhookDeliveriesPath(webhookId)}/${encodePathSegment(deliveryId)}`;
|
|
1301
1330
|
return this.requestJson("GET", path);
|
|
1302
1331
|
}
|
|
1332
|
+
async jobScope(scope) {
|
|
1333
|
+
if (scope.projectId !== void 0) await this.requireTier("pro", "projectId");
|
|
1334
|
+
return { project_id: scope.projectId };
|
|
1335
|
+
}
|
|
1303
1336
|
/** Build `/v1/projects/{projectId}` with the id percent-encoded. */
|
|
1304
1337
|
projectPath(projectId) {
|
|
1305
1338
|
return `/v1/projects/${encodePathSegment(projectId)}`;
|
|
@@ -1527,24 +1560,26 @@ var XbergClient = class {
|
|
|
1527
1560
|
interval = nextBackoffInterval(interval, this.retryBackoff);
|
|
1528
1561
|
continue;
|
|
1529
1562
|
}
|
|
1530
|
-
|
|
1531
|
-
await raiseForStatus(response);
|
|
1532
|
-
} catch (error) {
|
|
1533
|
-
if (error instanceof RateLimitError && error.retryAfter !== void 0 && attempt < this.retries) {
|
|
1534
|
-
attempt += 1;
|
|
1535
|
-
await this.sleep(error.retryAfter * 1e3);
|
|
1536
|
-
interval = nextBackoffInterval(interval, this.retryBackoff);
|
|
1537
|
-
continue;
|
|
1538
|
-
}
|
|
1539
|
-
throw error;
|
|
1540
|
-
}
|
|
1541
|
-
throw new XbergError("Unreachable", {
|
|
1542
|
-
status: response.status,
|
|
1543
|
-
body: null
|
|
1544
|
-
});
|
|
1563
|
+
throw await raiseForStatus(response);
|
|
1545
1564
|
}
|
|
1546
1565
|
}
|
|
1547
1566
|
};
|
|
1567
|
+
function parseJobResponse(payload) {
|
|
1568
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) throw new ValidationError("unexpected job response shape", {
|
|
1569
|
+
status: 200,
|
|
1570
|
+
body: payload
|
|
1571
|
+
});
|
|
1572
|
+
const record = payload;
|
|
1573
|
+
if (!("job_type" in record) && typeof record["filename"] === "string") return {
|
|
1574
|
+
...record,
|
|
1575
|
+
job_type: "extraction"
|
|
1576
|
+
};
|
|
1577
|
+
if (record["job_type"] === "extraction" || record["job_type"] === "crawl") return record;
|
|
1578
|
+
throw new ValidationError("unexpected job_type", {
|
|
1579
|
+
status: 200,
|
|
1580
|
+
body: payload
|
|
1581
|
+
});
|
|
1582
|
+
}
|
|
1548
1583
|
/**
|
|
1549
1584
|
* Pair a diff response's status code with its body to build the
|
|
1550
1585
|
* {@link DiffResult} union `diff`/`getDiffJob` return. `202` is the only
|
|
@@ -1707,7 +1742,7 @@ function prepareDocumentLineage(filenames, ids) {
|
|
|
1707
1742
|
}
|
|
1708
1743
|
return fields;
|
|
1709
1744
|
}
|
|
1710
|
-
|
|
1711
1745
|
//#endregion
|
|
1712
1746
|
export { AuthError, NotFoundError, RateLimitError, SUCCESS_JOB_STATUSES, ServerError, TERMINAL_JOB_STATUSES, TimeoutError, ValidationError, XbergClient, XbergError, createClient };
|
|
1747
|
+
|
|
1713
1748
|
//# sourceMappingURL=index.js.map
|