@geolonia/geonicdb-sdk 0.19.2 → 0.19.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geolonia/geonicdb-sdk",
3
- "version": "0.19.2",
3
+ "version": "0.19.3",
4
4
  "description": "GeonicDB JavaScript SDK — NGSI-LD Context Broker client for browser and Node.js",
5
5
  "main": "geonicdb.cjs",
6
6
  "module": "geonicdb.mjs",
package/react/index.d.ts CHANGED
@@ -408,6 +408,13 @@ declare class EventEmitter {
408
408
  protected emit<K extends keyof GeonicDBEventMap>(event: K, data?: GeonicDBEventMap[K]): void;
409
409
  protected emit(event: string, data?: unknown): void;
410
410
  }
411
+ /** RFC 8030 delivery options, shared by both NGSI protocol shapes. */
412
+ export interface WebPushDeliveryOptions {
413
+ /** Push message TTL in seconds. */
414
+ ttl?: number;
415
+ urgency?: "very-low" | "low" | "normal" | "high";
416
+ topic?: string;
417
+ }
411
418
  /** Options for `GeonicDB.subscribeWebPush()`. */
412
419
  export interface WebPushSubscribeOptions {
413
420
  /**
@@ -422,9 +429,65 @@ export interface WebPushSubscribeOptions {
422
429
  */
423
430
  serviceWorkerRegistration?: ServiceWorkerRegistration;
424
431
  }
432
+ /** Which NGSI protocol `registerWebPushSubscription()` should talk to (#3092). */
433
+ export type WebPushRegisterProtocol = "ngsiv2" | "ngsi-ld";
434
+ /** Entity selector shared by NGSIv2 `subject.entities` and NGSI-LD `entities`. */
435
+ export interface WebPushRegisterEntitySelector {
436
+ id?: string;
437
+ idPattern?: string;
438
+ type?: string;
439
+ }
440
+ /** NGSIv2 subscription `subject` accepted by `registerWebPushSubscription()`. */
441
+ export interface WebPushRegisterNgsiv2Subject {
442
+ entities: WebPushRegisterEntitySelector[];
443
+ condition?: {
444
+ attrs?: string[];
445
+ expression?: string;
446
+ };
447
+ }
448
+ /**
449
+ * Result of `GeonicDB.registerWebPushSubscription()` (#3092).
450
+ * `created` is `true` only when a new GeonicDB subscription was POSTed;
451
+ * reuse (and VAPID-rotation PATCH) returns `created: false`.
452
+ */
453
+ export interface RegisterWebPushSubscriptionResult {
454
+ subscriptionId: string;
455
+ created: boolean;
456
+ }
457
+ /**
458
+ * Options for `GeonicDB.registerWebPushSubscription()` (#3092).
459
+ *
460
+ * `protocol` is required with no default — the SDK already exposes separate
461
+ * NGSIv2 / NGSI-LD converters, and an implicit default would silently register
462
+ * against the wrong API.
463
+ */
464
+ export interface RegisterWebPushSubscriptionOptions extends WebPushSubscribeOptions, WebPushDeliveryOptions {
465
+ protocol: WebPushRegisterProtocol;
466
+ /** Subscription description. Defaults to `'Web Push notification'`. */
467
+ description?: string;
468
+ /** Required when `protocol` is `'ngsiv2'`. */
469
+ subject?: WebPushRegisterNgsiv2Subject;
470
+ /** Required when `protocol` is `'ngsi-ld'`. */
471
+ entities?: WebPushRegisterEntitySelector[];
472
+ /** NGSI-LD `watchedAttributes` (ignored for NGSIv2 — use `subject.condition.attrs`). */
473
+ watchedAttributes?: string[];
474
+ /** NGSI-LD `q` filter (ignored for NGSIv2). */
475
+ q?: string;
476
+ /**
477
+ * Override the localStorage key used for idempotency. Default includes
478
+ * baseUrl + tenant + protocol.
479
+ */
480
+ storageKey?: string;
481
+ }
425
482
  declare class GeonicDB extends EventEmitter {
426
483
  private _auth;
427
484
  private _ws;
485
+ /**
486
+ * In-flight `registerWebPushSubscription()` calls keyed by storageKey (#3092).
487
+ * Concurrent callers with the same key share one Promise so a race cannot
488
+ * POST two subscriptions before the first id is persisted.
489
+ */
490
+ private _webPushRegisterInflight;
428
491
  /**
429
492
  * Legacy callback for token refresh events.
430
493
  * Prefer `db.on('tokenRefresh', callback)` for new code.
@@ -627,9 +690,13 @@ declare class GeonicDB extends EventEmitter {
627
690
  * — otherwise (e.g. after the deployment rotated its VAPID keys) it
628
691
  * unsubscribes the stale one and re-subscribes with the current key.
629
692
  * Fetches the deployment's VAPID public key (unless `applicationServerKey`
630
- * is given) and calls `PushManager.subscribe()`. Convert the result with
631
- * `toNgsiv2WebPushNotification()` / `toNgsiLdWebPushEndpoint()` and create
632
- * the NGSI subscription with `request('POST', ...)`.
693
+ * is given) and calls `PushManager.subscribe()`.
694
+ *
695
+ * This method only covers the browser Push API. To also create (or reuse)
696
+ * the GeonicDB subscription idempotently, prefer
697
+ * `registerWebPushSubscription()` (#3092). For a manual flow, convert the
698
+ * result with `toNgsiv2WebPushNotification()` / `toNgsiLdWebPushEndpoint()`
699
+ * and `request('POST', ...)`.
633
700
  *
634
701
  * Browser-only — throws in environments without Service Worker + Push API
635
702
  * support (e.g. Node.js).
@@ -637,19 +704,54 @@ declare class GeonicDB extends EventEmitter {
637
704
  * @example
638
705
  * ```ts
639
706
  * await navigator.serviceWorker.register('/sw.js');
640
- * const subscription = await db.subscribeWebPush();
641
- * await db.request('POST', '/v2/subscriptions', {
642
- * description: 'Web Push notification',
707
+ * const { subscriptionId, created } = await db.registerWebPushSubscription({
708
+ * protocol: 'ngsiv2',
643
709
  * subject: { entities: [{ type: 'Sensor' }], condition: { attrs: ['value'] } },
644
- * notification: { webpush: toNgsiv2WebPushNotification(subscription) },
645
710
  * });
646
711
  * ```
647
712
  */
648
713
  subscribeWebPush(options?: WebPushSubscribeOptions): Promise<PushSubscription>;
714
+ /**
715
+ * Idempotently register a GeonicDB subscription that delivers via Web Push (#3092).
716
+ *
717
+ * 1. Calls `subscribeWebPush()` for the browser PushSubscription.
718
+ * 2. Looks up a previously saved subscription id in `localStorage`.
719
+ * 3. If that id still exists on the broker (`GET` → 200), returns it without
720
+ * `POST`ing. When the push endpoint changed (e.g. VAPID key rotation),
721
+ * `PATCH`es only `notification.webpush` / `endpoint.webpush` so a new
722
+ * subscription is not created (avoids burning the subscription quota).
723
+ * 4. Otherwise `POST`s a new subscription and persists its id.
724
+ *
725
+ * `protocol` is required — there is no default. A 507 from the broker is
726
+ * thrown as `QuotaExceededError`.
727
+ *
728
+ * @example
729
+ * ```ts
730
+ * import { QuotaExceededError } from '@geolonia/geonicdb-sdk';
731
+ *
732
+ * try {
733
+ * const { subscriptionId, created } = await db.registerWebPushSubscription({
734
+ * protocol: 'ngsiv2',
735
+ * subject: { entities: [{ type: 'Sensor' }], condition: { attrs: ['value'] } },
736
+ * });
737
+ * } catch (err) {
738
+ * if (err instanceof QuotaExceededError) {
739
+ * // plan grace ceiling — delete unused subscriptions or change plan, then retry
740
+ * }
741
+ * throw err;
742
+ * }
743
+ * ```
744
+ */
745
+ registerWebPushSubscription(options: RegisterWebPushSubscriptionOptions): Promise<RegisterWebPushSubscriptionResult>;
746
+ private _registerWebPushSubscriptionBody;
649
747
  /**
650
748
  * Unsubscribe the current browser from Web Push notifications, if
651
749
  * subscribed. Returns `false` (no-op) when there is no active
652
750
  * subscription or the environment has no Service Worker support.
751
+ *
752
+ * Does not delete the GeonicDB subscription document — clear the
753
+ * persisted id (see `buildWebPushSubscriptionStorageKey`) and
754
+ * `DELETE` the subscription separately if needed.
653
755
  */
654
756
  unsubscribeWebPush(registration?: ServiceWorkerRegistration): Promise<boolean>;
655
757
  }