@sparkvault/sdk-js 8.9.1 → 8.10.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/dist/index.d.ts +186 -2
- package/dist/notify/index.d.ts +77 -0
- package/dist/notify/types.d.ts +105 -0
- package/dist/sparkvault.cjs.js +201 -0
- package/dist/sparkvault.cjs.js.map +1 -1
- package/dist/sparkvault.esm.js +201 -1
- package/dist/sparkvault.esm.js.map +1 -1
- package/dist/sparkvault.js +1 -1
- package/dist/sparkvault.js.map +1 -1
- package/package.json +1 -1
package/dist/sparkvault.esm.js
CHANGED
|
@@ -13520,6 +13520,205 @@ class EntropyModule {
|
|
|
13520
13520
|
}
|
|
13521
13521
|
}
|
|
13522
13522
|
|
|
13523
|
+
/**
|
|
13524
|
+
* Notify Module
|
|
13525
|
+
*
|
|
13526
|
+
* The signed-in user's first-party Notify surface: their own metadata-only
|
|
13527
|
+
* inbox. The recipient is ALWAYS derived from the session on the server — no
|
|
13528
|
+
* method takes a `recipient_id`, and any account/recipient a caller might try
|
|
13529
|
+
* to supply is ignored. This mirrors `@sparkvault/sdk-mobile`'s notify client;
|
|
13530
|
+
* device push registration is intentionally omitted (Expo-only on mobile).
|
|
13531
|
+
*/
|
|
13532
|
+
const MARK_STATES = ['seen', 'read', 'archived'];
|
|
13533
|
+
/**
|
|
13534
|
+
* Decode a base64url VAPID key into the Uint8Array `pushManager.subscribe`
|
|
13535
|
+
* requires for `applicationServerKey`. Standalone (no atob dependency on a
|
|
13536
|
+
* data: URL) so it works in every browser context.
|
|
13537
|
+
*/
|
|
13538
|
+
function urlBase64ToUint8Array(base64url) {
|
|
13539
|
+
const padding = '='.repeat((4 - (base64url.length % 4)) % 4);
|
|
13540
|
+
const base64 = (base64url + padding).replace(/-/g, '+').replace(/_/g, '/');
|
|
13541
|
+
const raw = atob(base64);
|
|
13542
|
+
const output = new Uint8Array(new ArrayBuffer(raw.length));
|
|
13543
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
13544
|
+
output[i] = raw.charCodeAt(i);
|
|
13545
|
+
}
|
|
13546
|
+
return output;
|
|
13547
|
+
}
|
|
13548
|
+
/** Normalize a PushSubscription (or its JSON) to the server's wire shape. */
|
|
13549
|
+
function toSubscriptionInput(subscription) {
|
|
13550
|
+
const json = typeof subscription.toJSON === 'function'
|
|
13551
|
+
? subscription.toJSON()
|
|
13552
|
+
: subscription;
|
|
13553
|
+
const endpoint = json.endpoint;
|
|
13554
|
+
const p256dh = json.keys?.p256dh;
|
|
13555
|
+
const auth = json.keys?.auth;
|
|
13556
|
+
if (!endpoint || !p256dh || !auth) {
|
|
13557
|
+
throw new ValidationError('A web push subscription requires endpoint and keys.p256dh/auth');
|
|
13558
|
+
}
|
|
13559
|
+
return { endpoint, keys: { p256dh, auth } };
|
|
13560
|
+
}
|
|
13561
|
+
class NotifyModule {
|
|
13562
|
+
constructor(http) {
|
|
13563
|
+
this.http = http;
|
|
13564
|
+
}
|
|
13565
|
+
/**
|
|
13566
|
+
* The caller's own inbox feed (newest first). Follow `cursor` to page — a
|
|
13567
|
+
* page can come back short of `limit` yet still carry a non-null cursor.
|
|
13568
|
+
*
|
|
13569
|
+
* @example
|
|
13570
|
+
* const { notifications, cursor } = await sv.notify.getInbox({ state: 'unread' });
|
|
13571
|
+
*/
|
|
13572
|
+
async getInbox(options = {}) {
|
|
13573
|
+
const params = new URLSearchParams();
|
|
13574
|
+
if (options.state)
|
|
13575
|
+
params.set('state', options.state);
|
|
13576
|
+
if (options.limit)
|
|
13577
|
+
params.set('limit', String(options.limit));
|
|
13578
|
+
if (options.cursor)
|
|
13579
|
+
params.set('cursor', options.cursor);
|
|
13580
|
+
const query = params.toString();
|
|
13581
|
+
const response = await this.http.get(query ? `/v1/me/notifications?${query}` : '/v1/me/notifications');
|
|
13582
|
+
return response.data;
|
|
13583
|
+
}
|
|
13584
|
+
/**
|
|
13585
|
+
* Bounded unseen badge count for the caller's own inbox. `capped` true means
|
|
13586
|
+
* the true count is at/beyond the ceiling — render "N+".
|
|
13587
|
+
*
|
|
13588
|
+
* @example
|
|
13589
|
+
* const { unread_count, capped } = await sv.notify.getUnreadCount();
|
|
13590
|
+
*/
|
|
13591
|
+
async getUnreadCount() {
|
|
13592
|
+
const response = await this.http.get('/v1/me/notifications/unread-count');
|
|
13593
|
+
return response.data;
|
|
13594
|
+
}
|
|
13595
|
+
/**
|
|
13596
|
+
* Apply a terminal display state to one of the caller's own rows (idempotent).
|
|
13597
|
+
* `createdAt` MUST be the row's own `created_at` (round-tripped from the list
|
|
13598
|
+
* row) — the id alone cannot address the row.
|
|
13599
|
+
*
|
|
13600
|
+
* @example
|
|
13601
|
+
* await sv.notify.markState('ntf_abc', { createdAt: row.created_at, state: 'read' });
|
|
13602
|
+
*/
|
|
13603
|
+
async markState(notificationId, options) {
|
|
13604
|
+
if (!notificationId || typeof notificationId !== 'string') {
|
|
13605
|
+
throw new ValidationError('notificationId is required', { field: 'notificationId' });
|
|
13606
|
+
}
|
|
13607
|
+
if (!Number.isFinite(options?.createdAt)) {
|
|
13608
|
+
throw new ValidationError('createdAt (the row\'s created_at) is required', {
|
|
13609
|
+
field: 'createdAt',
|
|
13610
|
+
received: options?.createdAt,
|
|
13611
|
+
});
|
|
13612
|
+
}
|
|
13613
|
+
if (!MARK_STATES.includes(options.state)) {
|
|
13614
|
+
throw new ValidationError(`state must be one of ${MARK_STATES.join(', ')}`, {
|
|
13615
|
+
field: 'state',
|
|
13616
|
+
received: options.state,
|
|
13617
|
+
});
|
|
13618
|
+
}
|
|
13619
|
+
const response = await this.http.post(`/v1/me/notifications/${encodeURIComponent(notificationId)}/state`, { created_at: options.createdAt, state: options.state });
|
|
13620
|
+
return response.data;
|
|
13621
|
+
}
|
|
13622
|
+
// ── Browser Web Push ──────────────────────────────────────────────────────
|
|
13623
|
+
/**
|
|
13624
|
+
* Whether this environment can do Web Push (service worker + Push API +
|
|
13625
|
+
* Notifications). Feature-detect before showing an enable-notifications UI.
|
|
13626
|
+
*/
|
|
13627
|
+
isWebPushSupported() {
|
|
13628
|
+
return (typeof window !== 'undefined' &&
|
|
13629
|
+
'serviceWorker' in navigator &&
|
|
13630
|
+
'PushManager' in window &&
|
|
13631
|
+
'Notification' in window);
|
|
13632
|
+
}
|
|
13633
|
+
/**
|
|
13634
|
+
* The platform VAPID public key for `pushManager.subscribe()`. `configured`
|
|
13635
|
+
* is false until the server-side VAPID keypair is provisioned — hide the
|
|
13636
|
+
* enable-notifications affordance while false.
|
|
13637
|
+
*/
|
|
13638
|
+
async getWebPushKey() {
|
|
13639
|
+
const response = await this.http.get('/v1/me/notifications/web-push/key');
|
|
13640
|
+
return response.data;
|
|
13641
|
+
}
|
|
13642
|
+
/**
|
|
13643
|
+
* Register (or refresh) a browser push subscription for the signed-in user.
|
|
13644
|
+
* Accepts a live PushSubscription or its JSON shape.
|
|
13645
|
+
*/
|
|
13646
|
+
async registerWebPushSubscription(subscription) {
|
|
13647
|
+
const input = toSubscriptionInput(subscription);
|
|
13648
|
+
const response = await this.http.post('/v1/me/notifications/web-push', input);
|
|
13649
|
+
return response.data;
|
|
13650
|
+
}
|
|
13651
|
+
/** Remove a browser push subscription for the signed-in user, by endpoint. */
|
|
13652
|
+
async unsubscribeWebPushEndpoint(endpoint) {
|
|
13653
|
+
if (!endpoint || typeof endpoint !== 'string') {
|
|
13654
|
+
throw new ValidationError('endpoint is required');
|
|
13655
|
+
}
|
|
13656
|
+
const response = await this.http.post('/v1/me/notifications/web-push/unsubscribe', { endpoint });
|
|
13657
|
+
return response.data;
|
|
13658
|
+
}
|
|
13659
|
+
/**
|
|
13660
|
+
* End-to-end enable ceremony: feature-detect, fetch the VAPID key, ensure
|
|
13661
|
+
* Notification permission, subscribe via the caller's service worker, and
|
|
13662
|
+
* register the subscription with the server. Returns a typed status instead
|
|
13663
|
+
* of throwing on the expected declines (unsupported / not configured /
|
|
13664
|
+
* permission denied) so a host can branch on the outcome. Call it from a user
|
|
13665
|
+
* gesture (permission prompts require one).
|
|
13666
|
+
*
|
|
13667
|
+
* @example
|
|
13668
|
+
* const reg = await navigator.serviceWorker.register('/sw.js');
|
|
13669
|
+
* const { status } = await sv.notify.subscribeWebPush({ serviceWorkerRegistration: reg });
|
|
13670
|
+
*/
|
|
13671
|
+
async subscribeWebPush(options) {
|
|
13672
|
+
if (!this.isWebPushSupported()) {
|
|
13673
|
+
return { status: 'not_supported' };
|
|
13674
|
+
}
|
|
13675
|
+
const registration = options?.serviceWorkerRegistration;
|
|
13676
|
+
if (!registration || typeof registration.pushManager?.subscribe !== 'function') {
|
|
13677
|
+
throw new ValidationError('serviceWorkerRegistration with a pushManager is required');
|
|
13678
|
+
}
|
|
13679
|
+
const key = await this.getWebPushKey();
|
|
13680
|
+
if (!key.configured || !key.vapid_public_key) {
|
|
13681
|
+
return { status: 'not_configured' };
|
|
13682
|
+
}
|
|
13683
|
+
if (Notification.permission === 'denied') {
|
|
13684
|
+
return { status: 'permission_denied' };
|
|
13685
|
+
}
|
|
13686
|
+
if (Notification.permission === 'default') {
|
|
13687
|
+
const permission = await Notification.requestPermission();
|
|
13688
|
+
if (permission !== 'granted') {
|
|
13689
|
+
return { status: 'permission_denied' };
|
|
13690
|
+
}
|
|
13691
|
+
}
|
|
13692
|
+
// Reuse an existing subscription if present; browsers return the same one,
|
|
13693
|
+
// but registering again refreshes the server TTL.
|
|
13694
|
+
const existing = await registration.pushManager.getSubscription();
|
|
13695
|
+
const subscription = existing ??
|
|
13696
|
+
(await registration.pushManager.subscribe({
|
|
13697
|
+
userVisibleOnly: true,
|
|
13698
|
+
applicationServerKey: urlBase64ToUint8Array(key.vapid_public_key),
|
|
13699
|
+
}));
|
|
13700
|
+
await this.registerWebPushSubscription(subscription);
|
|
13701
|
+
return { status: existing ? 'already_subscribed' : 'subscribed', subscription };
|
|
13702
|
+
}
|
|
13703
|
+
/**
|
|
13704
|
+
* Disable Web Push on this device: unsubscribe the browser subscription and
|
|
13705
|
+
* remove it server-side. Safe to call when not subscribed (returns false).
|
|
13706
|
+
*/
|
|
13707
|
+
async unsubscribeWebPush(options) {
|
|
13708
|
+
const registration = options?.serviceWorkerRegistration;
|
|
13709
|
+
if (!registration || typeof registration.pushManager?.getSubscription !== 'function') {
|
|
13710
|
+
return false;
|
|
13711
|
+
}
|
|
13712
|
+
const subscription = await registration.pushManager.getSubscription();
|
|
13713
|
+
if (!subscription) {
|
|
13714
|
+
return false;
|
|
13715
|
+
}
|
|
13716
|
+
// Remove server-side first (still authenticated), then drop it locally.
|
|
13717
|
+
await this.unsubscribeWebPushEndpoint(subscription.endpoint).catch(() => undefined);
|
|
13718
|
+
return subscription.unsubscribe();
|
|
13719
|
+
}
|
|
13720
|
+
}
|
|
13721
|
+
|
|
13523
13722
|
/**
|
|
13524
13723
|
* SparkVault Debug Logger
|
|
13525
13724
|
*
|
|
@@ -13708,6 +13907,7 @@ class SparkVault {
|
|
|
13708
13907
|
this.folders = new FoldersModule(http);
|
|
13709
13908
|
this.sparks = new SparksModule(http);
|
|
13710
13909
|
this.entropy = new EntropyModule(http);
|
|
13910
|
+
this.notify = new NotifyModule(http);
|
|
13711
13911
|
this.health = new HealthModule(this.config);
|
|
13712
13912
|
}
|
|
13713
13913
|
/**
|
|
@@ -13747,5 +13947,5 @@ if (typeof window !== 'undefined') {
|
|
|
13747
13947
|
window.SparkVault = instance ?? SparkVault;
|
|
13748
13948
|
}
|
|
13749
13949
|
|
|
13750
|
-
export { AuthenticationError, AuthorizationError, EntropyModule, FoldersModule, IdentityModule, IngotsModule, NetworkError, PlanRequiredError, PopupBlockedError, ProductsModule, QuotaExceededError, SparkVault, SparkVaultError, SparksModule, TimeoutError, UserCancelledError, ValidationError, VaultsModule, SparkVault as default, logger, setDebugMode };
|
|
13950
|
+
export { AuthenticationError, AuthorizationError, EntropyModule, FoldersModule, IdentityModule, IngotsModule, NetworkError, NotifyModule, PlanRequiredError, PopupBlockedError, ProductsModule, QuotaExceededError, SparkVault, SparkVaultError, SparksModule, TimeoutError, UserCancelledError, ValidationError, VaultsModule, SparkVault as default, logger, setDebugMode };
|
|
13751
13951
|
//# sourceMappingURL=sparkvault.esm.js.map
|