@xberg-io/sdk 0.6.0 → 0.8.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 +5 -6
- package/dist/index.d.ts +3432 -1272
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +47 -34
- package/dist/index.js.map +1 -1
- package/package.json +13 -10
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
|
/**
|
|
@@ -120,9 +118,7 @@ function parseRetryAfter(value) {
|
|
|
120
118
|
* {@link FileLike} and {@link BackoffStrategy} are re-exported, via `client.ts`,
|
|
121
119
|
* because they appear in public client signatures.
|
|
122
120
|
*/
|
|
123
|
-
const DEFAULT_ENTERPRISE_BASE_URL = "https://api.xberg.io";
|
|
124
121
|
const DEFAULT_RETRY_BACKOFF_CAP_MS = 3e4;
|
|
125
|
-
const DEFAULT_BACKOFF_FACTOR = 2;
|
|
126
122
|
/**
|
|
127
123
|
* Caps an event-stream frame. The frames this endpoint sends are a few hundred
|
|
128
124
|
* bytes; the cap exists so a server that never closes a frame cannot grow this
|
|
@@ -135,15 +131,15 @@ const DEFAULT_BACKOFF_FACTOR = 2;
|
|
|
135
131
|
*/
|
|
136
132
|
const MAX_EVENT_STREAM_FRAME_BYTES = 1 << 20;
|
|
137
133
|
/**
|
|
138
|
-
* Resolve the
|
|
134
|
+
* Resolve the deployer's base URL; neither self-hosted product has a default.
|
|
139
135
|
*/
|
|
140
136
|
function resolveBaseUrl(baseUrl, target) {
|
|
141
|
-
|
|
142
|
-
if (
|
|
137
|
+
const resolved = baseUrl?.replace(/\/+$/, "");
|
|
138
|
+
if (resolved !== void 0 && resolved.length > 0) return resolved;
|
|
139
|
+
throw new XbergError(`${target === "pro" ? "Xberg Pro" : target === "enterprise" ? "Xberg Enterprise" : "Xberg"} has no default base URL; pass baseUrl pointing at your deployment.`, {
|
|
143
140
|
status: 0,
|
|
144
141
|
body: null
|
|
145
142
|
});
|
|
146
|
-
return DEFAULT_ENTERPRISE_BASE_URL;
|
|
147
143
|
}
|
|
148
144
|
/**
|
|
149
145
|
* Resolve the base URL of the Enterprise control plane, defaulting to the data plane's.
|
|
@@ -173,7 +169,7 @@ function buildQueryString(params) {
|
|
|
173
169
|
}
|
|
174
170
|
function nextBackoffInterval(current, strategy) {
|
|
175
171
|
if (strategy === "constant") return current;
|
|
176
|
-
return Math.min(current *
|
|
172
|
+
return Math.min(current * 2, DEFAULT_RETRY_BACKOFF_CAP_MS);
|
|
177
173
|
}
|
|
178
174
|
function parseRetryAfterHeader(value) {
|
|
179
175
|
if (value === null) return;
|
|
@@ -257,7 +253,7 @@ var EventStreamDecoder = class {
|
|
|
257
253
|
for (;;) {
|
|
258
254
|
const boundary = this.nextLineBoundary();
|
|
259
255
|
if (boundary === void 0) {
|
|
260
|
-
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`, {
|
|
261
257
|
status: 0,
|
|
262
258
|
body: null
|
|
263
259
|
});
|
|
@@ -301,7 +297,7 @@ var EventStreamDecoder = class {
|
|
|
301
297
|
if (value.startsWith(" ")) value = value.slice(1);
|
|
302
298
|
if (field === "data") {
|
|
303
299
|
this.size += value.length + 1;
|
|
304
|
-
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`, {
|
|
305
301
|
status: 0,
|
|
306
302
|
body: null
|
|
307
303
|
});
|
|
@@ -325,7 +321,6 @@ function encodePathSegment(value) {
|
|
|
325
321
|
});
|
|
326
322
|
return encodeURIComponent(value);
|
|
327
323
|
}
|
|
328
|
-
|
|
329
324
|
//#endregion
|
|
330
325
|
//#region src/types.ts
|
|
331
326
|
/**
|
|
@@ -343,15 +338,6 @@ const TERMINAL_JOB_STATUSES = [
|
|
|
343
338
|
* "completed but check warnings" result from a hard failure.
|
|
344
339
|
*/
|
|
345
340
|
const SUCCESS_JOB_STATUSES = ["completed", "partial_success"];
|
|
346
|
-
|
|
347
|
-
//#endregion
|
|
348
|
-
//#region src/version.ts
|
|
349
|
-
/**
|
|
350
|
-
* Package version, synced from the repo-root `VERSION` file by
|
|
351
|
-
* `scripts/sync-versions.py`. Used to build the `User-Agent` header. ~keep
|
|
352
|
-
*/
|
|
353
|
-
const VERSION = "0.6.0";
|
|
354
|
-
|
|
355
341
|
//#endregion
|
|
356
342
|
//#region src/client.ts
|
|
357
343
|
/**
|
|
@@ -365,8 +351,7 @@ const VERSION = "0.6.0";
|
|
|
365
351
|
* invoked against the wrong tier.
|
|
366
352
|
*
|
|
367
353
|
* Both products authenticate identically: `Authorization: Bearer {apiKey}`.
|
|
368
|
-
*
|
|
369
|
-
* (its spec ships no servers block) and requires an explicit one.
|
|
354
|
+
* Both products are self-hosted and require the deployment's `baseUrl`.
|
|
370
355
|
*
|
|
371
356
|
* Enterprise splits into two binaries — the data plane `baseUrl` addresses and a
|
|
372
357
|
* control plane (projects, API keys, integrations) on its own origin — while Pro
|
|
@@ -374,10 +359,10 @@ const VERSION = "0.6.0";
|
|
|
374
359
|
* and defaults to `baseUrl`. The backend methods use that origin and an optional
|
|
375
360
|
* separate `controlPlaneToken`; existing Pro methods retain their data-plane routing.
|
|
376
361
|
*/
|
|
377
|
-
const USER_AGENT = `xberg-io-sdk-typescript
|
|
362
|
+
const USER_AGENT = `xberg-io-sdk-typescript/0.8.0`;
|
|
378
363
|
const DEFAULT_TIMEOUT_MS = 3e4;
|
|
379
364
|
const DEFAULT_POLL_INTERVAL_MS = 1e3;
|
|
380
|
-
const DEFAULT_WAIT_TIMEOUT_MS =
|
|
365
|
+
const DEFAULT_WAIT_TIMEOUT_MS = 3e5;
|
|
381
366
|
const DEFAULT_RETRY_STATUSES = [
|
|
382
367
|
429,
|
|
383
368
|
502,
|
|
@@ -403,14 +388,14 @@ const OCTET_STREAM_MEDIA_TYPE = "application/octet-stream";
|
|
|
403
388
|
/** Media type of the crawl-event stream, sent as `Accept` and served as `Content-Type`. */
|
|
404
389
|
const EVENT_STREAM_MEDIA_TYPE = "text/event-stream";
|
|
405
390
|
/** The `kind` values `CrawlEventV1` declares. A frame carrying anything else is rejected. */
|
|
406
|
-
const CRAWL_EVENT_KINDS = new Set([
|
|
391
|
+
const CRAWL_EVENT_KINDS = /* @__PURE__ */ new Set([
|
|
407
392
|
"page",
|
|
408
393
|
"discovered",
|
|
409
394
|
"complete",
|
|
410
395
|
"error"
|
|
411
396
|
]);
|
|
412
397
|
/**
|
|
413
|
-
* High-level dual-target client. Construct with `new XbergClient({ apiKey })`.
|
|
398
|
+
* High-level dual-target client. Construct with `new XbergClient({ baseUrl, apiKey })`.
|
|
414
399
|
*/
|
|
415
400
|
var XbergClient = class {
|
|
416
401
|
baseUrl;
|
|
@@ -432,7 +417,7 @@ var XbergClient = class {
|
|
|
432
417
|
tierProbe;
|
|
433
418
|
/** Underlying `openapi-fetch` client — exposed for advanced use. */
|
|
434
419
|
raw;
|
|
435
|
-
constructor(options
|
|
420
|
+
constructor(options) {
|
|
436
421
|
if (options.target !== void 0) this.target = options.target;
|
|
437
422
|
this.baseUrl = resolveBaseUrl(options.baseUrl, options.target);
|
|
438
423
|
this.controlPlaneBaseUrl = resolveControlPlaneBaseUrl(options.controlPlaneBaseUrl, this.baseUrl);
|
|
@@ -462,6 +447,14 @@ var XbergClient = class {
|
|
|
462
447
|
async getAuthConfig() {
|
|
463
448
|
return await this.backendRequest("GET", `/auth/config`, { backendPublic: true });
|
|
464
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
|
+
}
|
|
465
458
|
/** Enterprise backend `POST /auth/login`. */
|
|
466
459
|
async backendLogin(body) {
|
|
467
460
|
return await this.backendRequest("POST", `/auth/login`, {
|
|
@@ -764,9 +757,9 @@ var XbergClient = class {
|
|
|
764
757
|
};
|
|
765
758
|
});
|
|
766
759
|
}
|
|
767
|
-
/** Fetch
|
|
760
|
+
/** Fetch a tagged extraction or crawl job record (`GET /v1/jobs/{id}`). */
|
|
768
761
|
async getJob(jobId) {
|
|
769
|
-
return await this.requestJson("GET", `${JOBS_PATH}/${encodePathSegment(jobId)}`);
|
|
762
|
+
return parseJobResponse(await this.requestJson("GET", `${JOBS_PATH}/${encodePathSegment(jobId)}`));
|
|
770
763
|
}
|
|
771
764
|
/**
|
|
772
765
|
* Fetch a job's stored result document (`GET /v1/jobs/{id}/result`).
|
|
@@ -806,6 +799,10 @@ var XbergClient = class {
|
|
|
806
799
|
let interval = initialInterval;
|
|
807
800
|
for (;;) {
|
|
808
801
|
const job = await this.getJob(jobId);
|
|
802
|
+
if (job.job_type !== "extraction") throw new ValidationError("expected extraction job, received crawl job", {
|
|
803
|
+
status: 200,
|
|
804
|
+
body: job
|
|
805
|
+
});
|
|
809
806
|
if (TERMINAL_JOB_STATUSES.includes(job.status)) {
|
|
810
807
|
if (SUCCESS_JOB_STATUSES.includes(job.status)) return job;
|
|
811
808
|
throw new XbergError(`Job ${jobId} ended with status ${job.status}`, {
|
|
@@ -820,7 +817,7 @@ var XbergClient = class {
|
|
|
820
817
|
});
|
|
821
818
|
const delay = Math.min(interval, remaining);
|
|
822
819
|
await this.sleep(delay);
|
|
823
|
-
if (backoff === "exponential") interval = Math.min(interval *
|
|
820
|
+
if (backoff === "exponential") interval = Math.min(interval * 2, DEFAULT_RETRY_BACKOFF_CAP_MS);
|
|
824
821
|
}
|
|
825
822
|
}
|
|
826
823
|
/** Wait for many jobs in parallel. */
|
|
@@ -1547,6 +1544,22 @@ var XbergClient = class {
|
|
|
1547
1544
|
}
|
|
1548
1545
|
}
|
|
1549
1546
|
};
|
|
1547
|
+
function parseJobResponse(payload) {
|
|
1548
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) throw new ValidationError("unexpected job response shape", {
|
|
1549
|
+
status: 200,
|
|
1550
|
+
body: payload
|
|
1551
|
+
});
|
|
1552
|
+
const record = payload;
|
|
1553
|
+
if (!("job_type" in record) && typeof record["filename"] === "string") return {
|
|
1554
|
+
...record,
|
|
1555
|
+
job_type: "extraction"
|
|
1556
|
+
};
|
|
1557
|
+
if (record["job_type"] === "extraction" || record["job_type"] === "crawl") return record;
|
|
1558
|
+
throw new ValidationError("unexpected job_type", {
|
|
1559
|
+
status: 200,
|
|
1560
|
+
body: payload
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1550
1563
|
/**
|
|
1551
1564
|
* Pair a diff response's status code with its body to build the
|
|
1552
1565
|
* {@link DiffResult} union `diff`/`getDiffJob` return. `202` is the only
|
|
@@ -1653,14 +1666,14 @@ async function* iterateCrawlEvents(open) {
|
|
|
1653
1666
|
await reader.cancel().catch(() => void 0);
|
|
1654
1667
|
}
|
|
1655
1668
|
}
|
|
1656
|
-
function createClient(options
|
|
1669
|
+
function createClient(options) {
|
|
1657
1670
|
const headers = {
|
|
1658
1671
|
"User-Agent": USER_AGENT,
|
|
1659
1672
|
...options.headers
|
|
1660
1673
|
};
|
|
1661
1674
|
if (options.apiKey !== void 0) headers["Authorization"] = `Bearer ${options.apiKey}`;
|
|
1662
1675
|
return createOpenApiClient({
|
|
1663
|
-
baseUrl: options.baseUrl
|
|
1676
|
+
baseUrl: resolveBaseUrl(options.baseUrl, void 0),
|
|
1664
1677
|
headers,
|
|
1665
1678
|
...options.fetch !== void 0 ? { fetch: options.fetch } : {}
|
|
1666
1679
|
});
|
|
@@ -1709,7 +1722,7 @@ function prepareDocumentLineage(filenames, ids) {
|
|
|
1709
1722
|
}
|
|
1710
1723
|
return fields;
|
|
1711
1724
|
}
|
|
1712
|
-
|
|
1713
1725
|
//#endregion
|
|
1714
1726
|
export { AuthError, NotFoundError, RateLimitError, SUCCESS_JOB_STATUSES, ServerError, TERMINAL_JOB_STATUSES, TimeoutError, ValidationError, XbergClient, XbergError, createClient };
|
|
1727
|
+
|
|
1715
1728
|
//# sourceMappingURL=index.js.map
|