@geolonia/geonicdb-sdk 0.19.2 → 0.20.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geolonia/geonicdb-sdk",
3
- "version": "0.19.2",
3
+ "version": "0.20.0",
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,70 @@ 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 is
478
+ * `buildWebPushSubscriptionStorageKey(baseUrl, tenant, protocol)` — it does
479
+ * **not** include login principal. On a shared browser (kiosk, shared profile)
480
+ * or when registering multiple distinct filters from the same origin, pass a
481
+ * key that still includes that default scope plus app-managed suffixes
482
+ * (e.g. `` `${buildWebPushSubscriptionStorageKey(...)}:${userId}:${filterId}` ``).
483
+ * See docs/SDK.md → Web Push (#3097).
484
+ */
485
+ storageKey?: string;
486
+ }
425
487
  declare class GeonicDB extends EventEmitter {
426
488
  private _auth;
427
489
  private _ws;
490
+ /**
491
+ * In-flight `registerWebPushSubscription()` calls keyed by storageKey (#3092).
492
+ * Concurrent callers with the same key share one Promise so a race cannot
493
+ * POST two subscriptions before the first id is persisted.
494
+ */
495
+ private _webPushRegisterInflight;
428
496
  /**
429
497
  * Legacy callback for token refresh events.
430
498
  * Prefer `db.on('tokenRefresh', callback)` for new code.
@@ -627,9 +695,13 @@ declare class GeonicDB extends EventEmitter {
627
695
  * — otherwise (e.g. after the deployment rotated its VAPID keys) it
628
696
  * unsubscribes the stale one and re-subscribes with the current key.
629
697
  * 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', ...)`.
698
+ * is given) and calls `PushManager.subscribe()`.
699
+ *
700
+ * This method only covers the browser Push API. To also create (or reuse)
701
+ * the GeonicDB subscription idempotently, prefer
702
+ * `registerWebPushSubscription()` (#3092). For a manual flow, convert the
703
+ * result with `toNgsiv2WebPushNotification()` / `toNgsiLdWebPushEndpoint()`
704
+ * and `request('POST', ...)`.
633
705
  *
634
706
  * Browser-only — throws in environments without Service Worker + Push API
635
707
  * support (e.g. Node.js).
@@ -637,19 +709,54 @@ declare class GeonicDB extends EventEmitter {
637
709
  * @example
638
710
  * ```ts
639
711
  * await navigator.serviceWorker.register('/sw.js');
640
- * const subscription = await db.subscribeWebPush();
641
- * await db.request('POST', '/v2/subscriptions', {
642
- * description: 'Web Push notification',
712
+ * const { subscriptionId, created } = await db.registerWebPushSubscription({
713
+ * protocol: 'ngsiv2',
643
714
  * subject: { entities: [{ type: 'Sensor' }], condition: { attrs: ['value'] } },
644
- * notification: { webpush: toNgsiv2WebPushNotification(subscription) },
645
715
  * });
646
716
  * ```
647
717
  */
648
718
  subscribeWebPush(options?: WebPushSubscribeOptions): Promise<PushSubscription>;
719
+ /**
720
+ * Idempotently register a GeonicDB subscription that delivers via Web Push (#3092).
721
+ *
722
+ * 1. Calls `subscribeWebPush()` for the browser PushSubscription.
723
+ * 2. Looks up a previously saved subscription id in `localStorage`.
724
+ * 3. If that id still exists on the broker (`GET` → 200), returns it without
725
+ * `POST`ing. When the push endpoint changed (e.g. VAPID key rotation),
726
+ * `PATCH`es only `notification.webpush` / `endpoint.webpush` so a new
727
+ * subscription is not created (avoids burning the subscription quota).
728
+ * 4. Otherwise `POST`s a new subscription and persists its id.
729
+ *
730
+ * `protocol` is required — there is no default. A 507 from the broker is
731
+ * thrown as `QuotaExceededError`.
732
+ *
733
+ * @example
734
+ * ```ts
735
+ * import { QuotaExceededError } from '@geolonia/geonicdb-sdk';
736
+ *
737
+ * try {
738
+ * const { subscriptionId, created } = await db.registerWebPushSubscription({
739
+ * protocol: 'ngsiv2',
740
+ * subject: { entities: [{ type: 'Sensor' }], condition: { attrs: ['value'] } },
741
+ * });
742
+ * } catch (err) {
743
+ * if (err instanceof QuotaExceededError) {
744
+ * // plan grace ceiling — delete unused subscriptions or change plan, then retry
745
+ * }
746
+ * throw err;
747
+ * }
748
+ * ```
749
+ */
750
+ registerWebPushSubscription(options: RegisterWebPushSubscriptionOptions): Promise<RegisterWebPushSubscriptionResult>;
751
+ private _registerWebPushSubscriptionBody;
649
752
  /**
650
753
  * Unsubscribe the current browser from Web Push notifications, if
651
754
  * subscribed. Returns `false` (no-op) when there is no active
652
755
  * subscription or the environment has no Service Worker support.
756
+ *
757
+ * Does not delete the GeonicDB subscription document — clear the
758
+ * persisted id (see `buildWebPushSubscriptionStorageKey`) and
759
+ * `DELETE` the subscription separately if needed.
653
760
  */
654
761
  unsubscribeWebPush(registration?: ServiceWorkerRegistration): Promise<boolean>;
655
762
  }