@centia-io/sdk 0.2.14 → 0.2.16

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.
@@ -706,6 +706,52 @@ var CentiaHttpClient = class {
706
706
  getHeader: (name) => response.headers.get(name)
707
707
  };
708
708
  }
709
+ /**
710
+ * Execute a request and return the raw Response without consuming the body.
711
+ * For binary endpoints (file downloads): follows redirects (a presigned
712
+ * storage URL — the fetch spec strips Authorization on cross-origin
713
+ * redirects), sends no Content-Type, and supports extra request headers
714
+ * such as Range. Throws CentiaApiError on a status not in expectedStatus.
715
+ */
716
+ async requestRaw(opts) {
717
+ var _this3 = this;
718
+ var _opts$accept, _opts$expectedStatus2;
719
+ const url = _this3.buildUrl(opts.path, opts.query);
720
+ const headers = { "Accept": (_opts$accept = opts.accept) !== null && _opts$accept !== void 0 ? _opts$accept : "*/*" };
721
+ if (_this3.auth.getAccessToken) {
722
+ const token = await _this3.auth.getAccessToken();
723
+ if (token) headers["Authorization"] = `Bearer ${token}`;
724
+ }
725
+ if (_this3.auth.getHeaders) Object.assign(headers, await _this3.auth.getHeaders());
726
+ if (_this3.userAgent && typeof navigator === "undefined") headers["User-Agent"] = _this3.userAgent;
727
+ Object.assign(headers, opts.headers);
728
+ const response = await _this3.fetchFn(url, {
729
+ method: opts.method,
730
+ headers,
731
+ redirect: "follow"
732
+ });
733
+ if (!((_opts$expectedStatus2 = opts.expectedStatus) !== null && _opts$expectedStatus2 !== void 0 ? _opts$expectedStatus2 : [200]).includes(response.status)) {
734
+ var _ref, _parsed$message, _response$headers$get;
735
+ let bodyText = "";
736
+ try {
737
+ bodyText = await response.text();
738
+ } catch (_unused) {}
739
+ let parsed = null;
740
+ if (bodyText) try {
741
+ parsed = JSON.parse(bodyText);
742
+ } catch (_unused2) {}
743
+ throw new CentiaApiError({
744
+ message: ((_ref = (_parsed$message = parsed === null || parsed === void 0 ? void 0 : parsed.message) !== null && _parsed$message !== void 0 ? _parsed$message : parsed === null || parsed === void 0 ? void 0 : parsed.error) !== null && _ref !== void 0 ? _ref : bodyText) || `Unexpected status ${response.status}`,
745
+ status: response.status,
746
+ code: parsed === null || parsed === void 0 ? void 0 : parsed.code,
747
+ details: parsed,
748
+ requestId: (_response$headers$get = response.headers.get("x-request-id")) !== null && _response$headers$get !== void 0 ? _response$headers$get : void 0,
749
+ method: opts.method,
750
+ url
751
+ });
752
+ }
753
+ return response;
754
+ }
709
755
  buildUrl(path, query) {
710
756
  const cleanPath = path.replace(/^\/+/, "");
711
757
  let url = `${this.baseUrl}/${cleanPath}`;
@@ -716,19 +762,19 @@ var CentiaHttpClient = class {
716
762
  return url;
717
763
  }
718
764
  async buildHeaders(opts) {
719
- var _this3 = this;
720
- var _opts$accept;
721
- const headers = { "Accept": (_opts$accept = opts.accept) !== null && _opts$accept !== void 0 ? _opts$accept : "application/json" };
722
- if (_this3.auth.getAccessToken) {
723
- const token = await _this3.auth.getAccessToken();
765
+ var _this4 = this;
766
+ var _opts$accept2;
767
+ const headers = { "Accept": (_opts$accept2 = opts.accept) !== null && _opts$accept2 !== void 0 ? _opts$accept2 : "application/json" };
768
+ if (_this4.auth.getAccessToken) {
769
+ const token = await _this4.auth.getAccessToken();
724
770
  if (token) headers["Authorization"] = `Bearer ${token}`;
725
771
  }
726
- if (_this3.auth.getHeaders) {
727
- const authHeaders = await _this3.auth.getHeaders();
772
+ if (_this4.auth.getHeaders) {
773
+ const authHeaders = await _this4.auth.getHeaders();
728
774
  Object.assign(headers, authHeaders);
729
775
  }
730
- if (_this3.userAgent && typeof navigator === "undefined") headers["User-Agent"] = _this3.userAgent;
731
- const ct = _this3.resolveContentType(opts.contentType);
776
+ if (_this4.userAgent && typeof navigator === "undefined") headers["User-Agent"] = _this4.userAgent;
777
+ const ct = _this4.resolveContentType(opts.contentType);
732
778
  if (ct) headers["Content-Type"] = ct;
733
779
  return headers;
734
780
  }
@@ -737,24 +783,24 @@ var CentiaHttpClient = class {
737
783
  return contentType !== null && contentType !== void 0 ? contentType : "application/json";
738
784
  }
739
785
  async handleResponse(response, opts, url) {
740
- var _opts$expectedStatus2, _parsed;
741
- const expectedStatus = (_opts$expectedStatus2 = opts.expectedStatus) !== null && _opts$expectedStatus2 !== void 0 ? _opts$expectedStatus2 : 200;
786
+ var _opts$expectedStatus3, _parsed;
787
+ const expectedStatus = (_opts$expectedStatus3 = opts.expectedStatus) !== null && _opts$expectedStatus3 !== void 0 ? _opts$expectedStatus3 : 200;
742
788
  let bodyText = "";
743
789
  try {
744
790
  bodyText = await response.text();
745
- } catch (_unused) {}
791
+ } catch (_unused3) {}
746
792
  let parsed = null;
747
793
  if (bodyText) try {
748
794
  parsed = JSON.parse(bodyText);
749
- } catch (_unused2) {}
795
+ } catch (_unused4) {}
750
796
  if (response.status !== expectedStatus) {
751
- var _ref, _parsed$message, _response$headers$get;
797
+ var _ref2, _parsed$message2, _response$headers$get2;
752
798
  throw new CentiaApiError({
753
- message: ((_ref = (_parsed$message = parsed === null || parsed === void 0 ? void 0 : parsed.message) !== null && _parsed$message !== void 0 ? _parsed$message : parsed === null || parsed === void 0 ? void 0 : parsed.error) !== null && _ref !== void 0 ? _ref : bodyText) || `Unexpected status ${response.status}`,
799
+ message: ((_ref2 = (_parsed$message2 = parsed === null || parsed === void 0 ? void 0 : parsed.message) !== null && _parsed$message2 !== void 0 ? _parsed$message2 : parsed === null || parsed === void 0 ? void 0 : parsed.error) !== null && _ref2 !== void 0 ? _ref2 : bodyText) || `Unexpected status ${response.status}`,
754
800
  status: response.status,
755
801
  code: parsed === null || parsed === void 0 ? void 0 : parsed.code,
756
802
  details: parsed,
757
- requestId: (_response$headers$get = response.headers.get("x-request-id")) !== null && _response$headers$get !== void 0 ? _response$headers$get : void 0,
803
+ requestId: (_response$headers$get2 = response.headers.get("x-request-id")) !== null && _response$headers$get2 !== void 0 ? _response$headers$get2 : void 0,
758
804
  method: opts.method,
759
805
  url
760
806
  });
@@ -3385,6 +3431,265 @@ var Features = class {
3385
3431
  }
3386
3432
  };
3387
3433
 
3434
+ //#endregion
3435
+ //#region src/snapshots/Snapshots.ts
3436
+ /**
3437
+ * Client for the snapshot API: GeoParquet exports of a relation.
3438
+ *
3439
+ * The job API (`/api/v4/snapshots`) queues and inspects exports and is
3440
+ * super-user only; exports run asynchronously — poll with `getSnapshot` or
3441
+ * use `waitForSnapshot`. The read API
3442
+ * (`/api/v4/schemas/{schema}/relations/{relation}/snapshots`) serves the
3443
+ * published snapshots to any user with read access to the relation, with
3444
+ * HEAD + byte-range support so Parquet readers can fetch footers and row
3445
+ * groups selectively.
3446
+ *
3447
+ * ```ts
3448
+ * const snapshots = new Snapshots(createCentiaClient({ baseUrl, auth }));
3449
+ * const { id } = await snapshots.postSnapshot({ schema: 'geodanmark', relation: 'bygning' });
3450
+ * const done = await snapshots.waitForSnapshot(id);
3451
+ * ```
3452
+ */
3453
+ var Snapshots = class {
3454
+ constructor(client) {
3455
+ this.client = client;
3456
+ }
3457
+ relationPath(schema, relation, rest = "") {
3458
+ return `api/v4/schemas/${encodeURIComponent(schema)}/relations/${encodeURIComponent(relation)}/snapshots${rest}`;
3459
+ }
3460
+ async postSnapshot(body) {
3461
+ return this.client.request({
3462
+ path: "api/v4/snapshots",
3463
+ method: "POST",
3464
+ body,
3465
+ expectedStatus: 202
3466
+ });
3467
+ }
3468
+ async getSnapshot(id) {
3469
+ var _this2 = this;
3470
+ const keys = Array.isArray(id) ? id : [id];
3471
+ return _this2.client.request({
3472
+ path: `api/v4/snapshots/${keys.map((k) => encodeURIComponent(k)).join(",")}`,
3473
+ method: "GET"
3474
+ });
3475
+ }
3476
+ /** List the newest snapshot jobs (50), optionally filtered by schema/relation. Super-user only. */
3477
+ async getSnapshots(options) {
3478
+ var _this3 = this;
3479
+ const query = {};
3480
+ if ((options === null || options === void 0 ? void 0 : options.schema) !== void 0) query.schema = options.schema;
3481
+ if ((options === null || options === void 0 ? void 0 : options.relation) !== void 0) query.relation = options.relation;
3482
+ return _this3.client.request({
3483
+ path: "api/v4/snapshots",
3484
+ method: "GET",
3485
+ query: Object.keys(query).length > 0 ? query : void 0
3486
+ });
3487
+ }
3488
+ /**
3489
+ * Poll a snapshot job until it reaches a terminal status (succeeded, failed
3490
+ * or superseded) and return it — check `status`/`error` on the result.
3491
+ * Throws an `Error` when `timeoutMs` elapses first.
3492
+ */
3493
+ async waitForSnapshot(id, options) {
3494
+ var _this4 = this;
3495
+ var _options$intervalMs, _options$timeoutMs;
3496
+ const intervalMs = (_options$intervalMs = options === null || options === void 0 ? void 0 : options.intervalMs) !== null && _options$intervalMs !== void 0 ? _options$intervalMs : 2e3;
3497
+ const timeoutMs = (_options$timeoutMs = options === null || options === void 0 ? void 0 : options.timeoutMs) !== null && _options$timeoutMs !== void 0 ? _options$timeoutMs : 3e5;
3498
+ const deadline = Date.now() + timeoutMs;
3499
+ for (;;) {
3500
+ const job = await _this4.getSnapshot(id);
3501
+ if (job.status === "succeeded" || job.status === "failed" || job.status === "superseded") return job;
3502
+ if (Date.now() + intervalMs > deadline) throw new Error(`Timed out after ${timeoutMs} ms waiting for snapshot ${id} (status: ${job.status})`);
3503
+ await new Promise((resolve) => setTimeout(resolve, intervalMs));
3504
+ }
3505
+ }
3506
+ /** List the published snapshots of a relation, newest first. */
3507
+ async getRelationSnapshots(schema, relation) {
3508
+ var _this5 = this;
3509
+ return _this5.client.request({
3510
+ path: _this5.relationPath(schema, relation),
3511
+ method: "GET"
3512
+ });
3513
+ }
3514
+ /** Get one published snapshot (metadata) by date (`YYYY-MM-DD`). */
3515
+ async getRelationSnapshot(schema, relation, date) {
3516
+ var _this6 = this;
3517
+ return _this6.client.request({
3518
+ path: _this6.relationPath(schema, relation, `/${encodeURIComponent(date)}`),
3519
+ method: "GET"
3520
+ });
3521
+ }
3522
+ /**
3523
+ * Absolute URL of the snapshot's Parquet file, without fetching it — for
3524
+ * DuckDB/GDAL or a plain fetch. Authorization travels in the request
3525
+ * header, so protected relations need the caller to attach the token.
3526
+ */
3527
+ getRelationSnapshotDataUrl(schema, relation, date) {
3528
+ return `${this.client.baseUrl}/${this.relationPath(schema, relation, `/${encodeURIComponent(date)}/data`)}`;
3529
+ }
3530
+ /**
3531
+ * Fetch the snapshot's Parquet file and return the raw `Response` (stream
3532
+ * or buffer it yourself). Follows the 302 redirect the server answers in
3533
+ * redirect mode. Pass `range` for a single byte range (206). Throws
3534
+ * `CentiaApiError`: 403, 404, 409 (several files; use
3535
+ * getRelationSnapshotFile), 416, 502.
3536
+ */
3537
+ async getRelationSnapshotData(schema, relation, date, options) {
3538
+ var _this7 = this;
3539
+ return _this7.rawGet(_this7.relationPath(schema, relation, `/${encodeURIComponent(date)}/data`), "GET", options);
3540
+ }
3541
+ /** HEAD request for the snapshot's Parquet file — Content-Length, Accept-Ranges and ETag without the body. */
3542
+ async headRelationSnapshotData(schema, relation, date) {
3543
+ var _this8 = this;
3544
+ return _this8.rawGet(_this8.relationPath(schema, relation, `/${encodeURIComponent(date)}/data`), "HEAD");
3545
+ }
3546
+ /**
3547
+ * Fetch one file of the snapshot by catalog name (`data-<id>.parquet`,
3548
+ * `metadata-<id>.json`). Same redirect/range semantics as
3549
+ * getRelationSnapshotData.
3550
+ */
3551
+ async getRelationSnapshotFile(schema, relation, date, file, options) {
3552
+ var _this9 = this;
3553
+ return _this9.rawGet(_this9.relationPath(schema, relation, `/${encodeURIComponent(date)}/files/${encodeURIComponent(file)}`), "GET", options);
3554
+ }
3555
+ /** HEAD request for one file of the snapshot. */
3556
+ async headRelationSnapshotFile(schema, relation, date, file) {
3557
+ var _this10 = this;
3558
+ return _this10.rawGet(_this10.relationPath(schema, relation, `/${encodeURIComponent(date)}/files/${encodeURIComponent(file)}`), "HEAD");
3559
+ }
3560
+ async rawGet(path, method, options) {
3561
+ var _this11 = this;
3562
+ const headers = {};
3563
+ if (options === null || options === void 0 ? void 0 : options.range) headers["Range"] = `bytes=${options.range[0]}-${options.range[1]}`;
3564
+ return _this11.client.requestRaw({
3565
+ path,
3566
+ method,
3567
+ headers: Object.keys(headers).length > 0 ? headers : void 0,
3568
+ expectedStatus: [200, 206]
3569
+ });
3570
+ }
3571
+ };
3572
+
3573
+ //#endregion
3574
+ //#region src/scheduler/Scheduler.ts
3575
+ function idsPath(id) {
3576
+ return (Array.isArray(id) ? id : [id]).map((k) => encodeURIComponent(String(k))).join(",");
3577
+ }
3578
+ /**
3579
+ * Client for the scheduler API (`/api/v4/scheduler`). Super-user only.
3580
+ *
3581
+ * Jobs describe recurring data imports (cron-scheduled); runs are their
3582
+ * executions. Starting a run is asynchronous — poll `getSchedulerRuns`
3583
+ * (the `_links.runs` of the 202) until the run finishes.
3584
+ *
3585
+ * ```ts
3586
+ * const scheduler = new Scheduler(createCentiaClient({ baseUrl, auth }));
3587
+ * const { ids } = await scheduler.postSchedulerJob({ name, schema, url, schedule: '0 3 * * *' });
3588
+ * await scheduler.postSchedulerRun({ job: ids[0] });
3589
+ * ```
3590
+ */
3591
+ var Scheduler = class {
3592
+ constructor(client) {
3593
+ this.client = client;
3594
+ }
3595
+ /** List all scheduler jobs. */
3596
+ async getSchedulerJobs() {
3597
+ return this.client.request({
3598
+ path: "api/v4/scheduler/jobs",
3599
+ method: "GET"
3600
+ });
3601
+ }
3602
+ async getSchedulerJob(id) {
3603
+ return this.client.request({
3604
+ path: `api/v4/scheduler/jobs/${idsPath(id)}`,
3605
+ method: "GET"
3606
+ });
3607
+ }
3608
+ /**
3609
+ * Create one or more jobs (201; all-or-nothing for arrays). Returns the
3610
+ * Location header and the new ids parsed from it, in request order.
3611
+ */
3612
+ async postSchedulerJob(body) {
3613
+ var _this3 = this;
3614
+ var _res$getHeader, _location$split$pop;
3615
+ const location = (_res$getHeader = (await _this3.client.requestFull({
3616
+ path: "api/v4/scheduler/jobs",
3617
+ method: "POST",
3618
+ body,
3619
+ expectedStatus: 201
3620
+ })).getHeader("Location")) !== null && _res$getHeader !== void 0 ? _res$getHeader : "";
3621
+ return {
3622
+ location,
3623
+ ids: ((_location$split$pop = location.split("/").pop()) !== null && _location$split$pop !== void 0 ? _location$split$pop : "").split(",").map((s) => Number(s)).filter((n) => Number.isFinite(n))
3624
+ };
3625
+ }
3626
+ /** Partially update a job (303 + Location). */
3627
+ async patchSchedulerJob(id, body) {
3628
+ var _this4 = this;
3629
+ var _res$getHeader2;
3630
+ return { location: (_res$getHeader2 = (await _this4.client.requestFull({
3631
+ path: `api/v4/scheduler/jobs/${idsPath(id)}`,
3632
+ method: "PATCH",
3633
+ body,
3634
+ expectedStatus: 303
3635
+ })).getHeader("Location")) !== null && _res$getHeader2 !== void 0 ? _res$getHeader2 : "" };
3636
+ }
3637
+ /**
3638
+ * Delete one or more jobs (204). Nothing is deleted if any id fails:
3639
+ * throws 404 (unknown id) or 409 (a run is in progress).
3640
+ */
3641
+ async deleteSchedulerJob(id) {
3642
+ await this.client.request({
3643
+ path: `api/v4/scheduler/jobs/${idsPath(id)}`,
3644
+ method: "DELETE",
3645
+ expectedStatus: 204
3646
+ });
3647
+ }
3648
+ /** List runs (running plus the newest 50 finished), optionally filtered by job and status. */
3649
+ async getSchedulerRuns(options) {
3650
+ var _this6 = this;
3651
+ const query = {};
3652
+ if ((options === null || options === void 0 ? void 0 : options.job) !== void 0) query.job = String(options.job);
3653
+ if ((options === null || options === void 0 ? void 0 : options.status) !== void 0) query.status = options.status;
3654
+ return _this6.client.request({
3655
+ path: "api/v4/scheduler/runs",
3656
+ method: "GET",
3657
+ query: Object.keys(query).length > 0 ? query : void 0
3658
+ });
3659
+ }
3660
+ /** Get one run by uuid. */
3661
+ async getSchedulerRun(uuid) {
3662
+ return this.client.request({
3663
+ path: `api/v4/scheduler/runs/${encodeURIComponent(uuid)}`,
3664
+ method: "GET"
3665
+ });
3666
+ }
3667
+ /**
3668
+ * Start a run of a job (202). Throws 404 (unknown job) or 409 (a run of
3669
+ * the job is already running).
3670
+ */
3671
+ async postSchedulerRun(body) {
3672
+ return this.client.request({
3673
+ path: "api/v4/scheduler/runs",
3674
+ method: "POST",
3675
+ body,
3676
+ expectedStatus: 202
3677
+ });
3678
+ }
3679
+ /**
3680
+ * Stop a running run: sends SIGINT, which the server escalates to SIGKILL
3681
+ * after 30 s — the request itself can take up to ~30 s, so do not use a
3682
+ * short timeout. Throws 404 (no running run with that uuid) or 409 (the
3683
+ * run is on another host).
3684
+ */
3685
+ async deleteSchedulerRun(uuid) {
3686
+ return this.client.request({
3687
+ path: `api/v4/scheduler/runs/${encodeURIComponent(uuid)}`,
3688
+ method: "DELETE"
3689
+ });
3690
+ }
3691
+ };
3692
+
3388
3693
  //#endregion
3389
3694
  //#region src/auth/errors.ts
3390
3695
  /**
@@ -3507,5 +3812,5 @@ function isExpired(jwt, skewSeconds) {
3507
3812
  */
3508
3813
 
3509
3814
  //#endregion
3510
- export { CentiaApiError, Claims, CodeFlow, Features, Gql, GuestFlow, Keyvalue, Mapcache, Meta, NotLoggedInError, OGC_CRS84, Ogc, Ows, PasswordFlow, Rpc, SessionExpiredError, SignUp, Sql, SqlNoToken, Stats, Status, Tables, Users, Wfs, Ws, createApi, createCentiaAdminClient, createCentiaClient, createSqlBuilder, createTokenProvider, isCentiaApiError, ogcEpsgCrs };
3815
+ export { CentiaApiError, Claims, CodeFlow, Features, Gql, GuestFlow, Keyvalue, Mapcache, Meta, NotLoggedInError, OGC_CRS84, Ogc, Ows, PasswordFlow, Rpc, Scheduler, SessionExpiredError, SignUp, Snapshots, Sql, SqlNoToken, Stats, Status, Tables, Users, Wfs, Ws, createApi, createCentiaAdminClient, createCentiaClient, createSqlBuilder, createTokenProvider, isCentiaApiError, ogcEpsgCrs };
3511
3816
  //# sourceMappingURL=centia-io-sdk.js.map