@molecule/app-push 1.0.3 → 1.2.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 +152 -5
- package/dist/device-toggle.d.ts +114 -0
- package/dist/device-toggle.d.ts.map +1 -0
- package/dist/device-toggle.js +174 -0
- package/dist/device-toggle.js.map +1 -0
- package/dist/index.d.ts +14 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -3
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-11-23T15:46:24.534Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/app-push
|
|
@@ -47,13 +47,47 @@ function handleTaps(open: (data: unknown) => void): () => void {
|
|
|
47
47
|
## Installation
|
|
48
48
|
|
|
49
49
|
```bash
|
|
50
|
-
npm install @molecule/app-push @molecule/app-bond @molecule/app-i18n
|
|
50
|
+
npm install @molecule/app-push @molecule/app-bond @molecule/app-i18n @molecule/app-pwa-default
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
## API
|
|
54
54
|
|
|
55
55
|
### Interfaces
|
|
56
56
|
|
|
57
|
+
#### `PushToggleHttp`
|
|
58
|
+
|
|
59
|
+
Minimal structural slice of `@molecule/app-http`'s HttpClient used here.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
interface PushToggleHttp {
|
|
63
|
+
get<T = unknown>(url: string): Promise<{ data: T }>
|
|
64
|
+
patch<T = unknown>(url: string, data?: unknown): Promise<{ data: T }>
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### `PushToggleDevice`
|
|
69
|
+
|
|
70
|
+
Device row slice returned by `GET /api/devices`.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
interface PushToggleDevice {
|
|
74
|
+
id: string
|
|
75
|
+
isCurrent?: boolean
|
|
76
|
+
hasPushSubscription?: boolean
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### `PushToggleToken`
|
|
81
|
+
|
|
82
|
+
Push token slice returned by `@molecule/app-push` `register()`.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
interface PushToggleToken {
|
|
86
|
+
value: string
|
|
87
|
+
platform: 'web' | 'ios' | 'android'
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
57
91
|
#### `PushNotification`
|
|
58
92
|
|
|
59
93
|
Push notification payload.
|
|
@@ -396,6 +430,24 @@ interface PushProvider {
|
|
|
396
430
|
|
|
397
431
|
### Types
|
|
398
432
|
|
|
433
|
+
#### `PushToggleFailureReason`
|
|
434
|
+
|
|
435
|
+
Why an enable/disable attempt failed (mapped to i18n by the component).
|
|
436
|
+
|
|
437
|
+
```typescript
|
|
438
|
+
type PushToggleFailureReason =
|
|
439
|
+
'permission-denied' | 'server-unconfigured' | 'register-failed' | 'persist-failed'
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
#### `PushToggleResult`
|
|
443
|
+
|
|
444
|
+
Result of an enable/disable attempt.
|
|
445
|
+
|
|
446
|
+
```typescript
|
|
447
|
+
type PushToggleResult =
|
|
448
|
+
{ ok: true } | { ok: false; reason: PushToggleFailureReason; message?: string }
|
|
449
|
+
```
|
|
450
|
+
|
|
399
451
|
#### `PermissionStatus`
|
|
400
452
|
|
|
401
453
|
Push notification permission status.
|
|
@@ -432,6 +484,89 @@ type TokenChangeListener = (token: PushToken) => void
|
|
|
432
484
|
|
|
433
485
|
### Functions
|
|
434
486
|
|
|
487
|
+
#### `findCurrentDevice(devices)`
|
|
488
|
+
|
|
489
|
+
Picks the caller's own device row: the api flags the session device
|
|
490
|
+
`isCurrent`; fall back to the first row for sessions predating the flag.
|
|
491
|
+
|
|
492
|
+
```typescript
|
|
493
|
+
function findCurrentDevice(devices: PushToggleDevice[] | undefined): PushToggleDevice | undefined
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
- `devices` — Rows from `GET /api/devices`.
|
|
497
|
+
|
|
498
|
+
**Returns:** The current device row, or `undefined` when the user has none.
|
|
499
|
+
|
|
500
|
+
#### `subscriptionFromToken(token)`
|
|
501
|
+
|
|
502
|
+
Converts an `@molecule/app-push` token into the device resource's
|
|
503
|
+
`pushSubscription` shape (web PushSubscription JSON, FCM registration for
|
|
504
|
+
Android, APNs registration for iOS).
|
|
505
|
+
|
|
506
|
+
```typescript
|
|
507
|
+
function subscriptionFromToken(token: PushToggleToken): unknown
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
- `token` — The push token returned by `register()`.
|
|
511
|
+
|
|
512
|
+
**Returns:** The `pushSubscription` value to PATCH onto the device row.
|
|
513
|
+
|
|
514
|
+
#### `enablePushOnCurrentDevice(deps)`
|
|
515
|
+
|
|
516
|
+
Runs the full enable chain: browser permission → runtime VAPID public key
|
|
517
|
+
(`GET /api/devices/push/public-key`) → `register({ vapidPublicKey })` →
|
|
518
|
+
persist the subscription on the current device row.
|
|
519
|
+
|
|
520
|
+
Every failure is returned as a typed reason (never thrown) so the UI can
|
|
521
|
+
show an honest, specific message instead of hanging or silently reverting.
|
|
522
|
+
|
|
523
|
+
```typescript
|
|
524
|
+
function enablePushOnCurrentDevice(deps: {
|
|
525
|
+
http: PushToggleHttp
|
|
526
|
+
requestPermission: () => Promise<string>
|
|
527
|
+
register: (options?: { vapidPublicKey?: string }) => Promise<PushToggleToken>
|
|
528
|
+
}): Promise<PushToggleResult>
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
- `deps` — The http client + push actions from `usePush()`.
|
|
532
|
+
- `deps.http` — Authenticated http client (`useHttpClient()`).
|
|
533
|
+
- `deps.requestPermission` — `usePush().requestPermission`.
|
|
534
|
+
- `deps.register` — `usePush().register`.
|
|
535
|
+
|
|
536
|
+
**Returns:** `{ ok: true }` or a typed failure.
|
|
537
|
+
|
|
538
|
+
#### `disablePushOnCurrentDevice(deps)`
|
|
539
|
+
|
|
540
|
+
Disables push: unsubscribes the browser (best-effort — a dev build without
|
|
541
|
+
a service worker has nothing to unsubscribe) and ALWAYS clears the server
|
|
542
|
+
state so no further pushes target this device.
|
|
543
|
+
|
|
544
|
+
```typescript
|
|
545
|
+
function disablePushOnCurrentDevice(deps: {
|
|
546
|
+
http: PushToggleHttp
|
|
547
|
+
unregister: () => Promise<void>
|
|
548
|
+
}): Promise<PushToggleResult>
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
- `deps` — The http client + push action from `usePush()`.
|
|
552
|
+
- `deps.http` — Authenticated http client (`useHttpClient()`).
|
|
553
|
+
- `deps.unregister` — `usePush().unregister`.
|
|
554
|
+
|
|
555
|
+
**Returns:** `{ ok: true }` or a typed failure (server state not cleared).
|
|
556
|
+
|
|
557
|
+
#### `readCurrentDevicePushEnabled(http)`
|
|
558
|
+
|
|
559
|
+
Reads whether push is currently enabled for THIS device (server truth:
|
|
560
|
+
the current device row's `hasPushSubscription`).
|
|
561
|
+
|
|
562
|
+
```typescript
|
|
563
|
+
function readCurrentDevicePushEnabled(http: PushToggleHttp): Promise<boolean>
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
- `http` — Authenticated http client (`useHttpClient()`).
|
|
567
|
+
|
|
568
|
+
**Returns:** `true` when the current device has a stored subscription.
|
|
569
|
+
|
|
435
570
|
#### `setProvider(provider)`
|
|
436
571
|
|
|
437
572
|
Sets the push provider implementation.
|
|
@@ -588,11 +723,13 @@ Peer dependencies:
|
|
|
588
723
|
|
|
589
724
|
- `@molecule/app-bond` ^1.0.1
|
|
590
725
|
- `@molecule/app-i18n` ^1.0.1
|
|
726
|
+
- `@molecule/app-pwa-default` ^1.1.0
|
|
591
727
|
|
|
592
728
|
### Runtime Dependencies
|
|
593
729
|
|
|
594
730
|
- `@molecule/app-bond`
|
|
595
731
|
- `@molecule/app-i18n`
|
|
732
|
+
- `@molecule/app-pwa-default`
|
|
596
733
|
|
|
597
734
|
- **No wiring is needed on web**: the first accessor call silently bonds
|
|
598
735
|
the built-in Web Push provider. In a native app wire
|
|
@@ -600,9 +737,11 @@ Peer dependencies:
|
|
|
600
737
|
`setProvider()` BEFORE any push call — otherwise the web fallback gets
|
|
601
738
|
bonded and native registration never happens.
|
|
602
739
|
- **Web `register()` has three hard prerequisites**: (1) HTTPS (secure
|
|
603
|
-
context), (2) a registered SERVICE WORKER —
|
|
604
|
-
|
|
605
|
-
|
|
740
|
+
context), (2) a registered SERVICE WORKER — the one
|
|
741
|
+
`@molecule/app-pwa-default` (a peer of this package) registers in the
|
|
742
|
+
production build, which also shows the delivered notifications; dev
|
|
743
|
+
builds don't ship one, so register() throws "requires the production app
|
|
744
|
+
build" (it fails fast instead of hanging), and (3) a VAPID public key
|
|
606
745
|
(pass via `register({ vapidPublicKey })`) matching your API's
|
|
607
746
|
`VAPID_PRIVATE_KEY` — without it Chrome may register but delivery
|
|
608
747
|
fails.
|
|
@@ -614,6 +753,14 @@ Peer dependencies:
|
|
|
614
753
|
delivery send a real push from the server.
|
|
615
754
|
- `token.value` on web is a JSON-serialized PushSubscription (not an FCM
|
|
616
755
|
token) — store it opaquely; platform is in `token.platform`.
|
|
756
|
+
- **A settings toggle for the current device** needs no code of its own:
|
|
757
|
+
`enablePushOnCurrentDevice({ http, requestPermission, register })`,
|
|
758
|
+
`disablePushOnCurrentDevice({ http, unregister })` and
|
|
759
|
+
`readCurrentDevicePushEnabled(http)` run the whole chain against the
|
|
760
|
+
molecule device API (`GET /api/devices/push/public-key`,
|
|
761
|
+
`GET /api/devices`, `PATCH /api/devices/:id`) and return a typed result
|
|
762
|
+
instead of throwing. Every framework's settings panel package re-exports
|
|
763
|
+
them.
|
|
617
764
|
|
|
618
765
|
## Translations
|
|
619
766
|
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The push-notifications toggle for the signed-in user's current device: the
|
|
3
|
+
* full enable/disable chain — permission → runtime VAPID key fetch →
|
|
4
|
+
* subscribe → persist on the device row — as plain functions over an HTTP
|
|
5
|
+
* client and this package's `requestPermission` / `register` /
|
|
6
|
+
* `unregister`. Every framework's settings panel
|
|
7
|
+
* (`@molecule/app-settings-panel-react`, `-vue`, `-solid`, `-svelte`,
|
|
8
|
+
* `-angular`) re-exports these, so the chain is the same everywhere.
|
|
9
|
+
*
|
|
10
|
+
* The server contract (shared by every molecule api):
|
|
11
|
+
*
|
|
12
|
+
* - `GET /api/devices/push/public-key` → `{ publicKey }` — the VAPID public
|
|
13
|
+
* key the browser must pass as `applicationServerKey` (Chromium rejects
|
|
14
|
+
* keyless subscriptions). 404/503 when push is not configured server-side.
|
|
15
|
+
* - `GET /api/devices` → the caller's device rows; the session's own device
|
|
16
|
+
* is flagged `isCurrent: true`.
|
|
17
|
+
* - `PATCH /api/devices/:id { pushSubscription, hasPushSubscription }` —
|
|
18
|
+
* persists the browser subscription where the api-side push fan-outs read
|
|
19
|
+
* it. Without this PATCH the server never knows the subscription exists
|
|
20
|
+
* and no push can ever reach the user.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
/** Minimal structural slice of `@molecule/app-http`'s HttpClient used here. */
|
|
25
|
+
export interface PushToggleHttp {
|
|
26
|
+
get<T = unknown>(url: string): Promise<{
|
|
27
|
+
data: T;
|
|
28
|
+
}>;
|
|
29
|
+
patch<T = unknown>(url: string, data?: unknown): Promise<{
|
|
30
|
+
data: T;
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
33
|
+
/** Device row slice returned by `GET /api/devices`. */
|
|
34
|
+
export interface PushToggleDevice {
|
|
35
|
+
id: string;
|
|
36
|
+
isCurrent?: boolean;
|
|
37
|
+
hasPushSubscription?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** Push token slice returned by `@molecule/app-push` `register()`. */
|
|
40
|
+
export interface PushToggleToken {
|
|
41
|
+
value: string;
|
|
42
|
+
platform: 'web' | 'ios' | 'android';
|
|
43
|
+
}
|
|
44
|
+
/** Why an enable/disable attempt failed (mapped to i18n by the component). */
|
|
45
|
+
export type PushToggleFailureReason = 'permission-denied' | 'server-unconfigured' | 'register-failed' | 'persist-failed';
|
|
46
|
+
/** Result of an enable/disable attempt. */
|
|
47
|
+
export type PushToggleResult = {
|
|
48
|
+
ok: true;
|
|
49
|
+
} | {
|
|
50
|
+
ok: false;
|
|
51
|
+
reason: PushToggleFailureReason;
|
|
52
|
+
message?: string;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Picks the caller's own device row: the api flags the session device
|
|
56
|
+
* `isCurrent`; fall back to the first row for sessions predating the flag.
|
|
57
|
+
*
|
|
58
|
+
* @param devices - Rows from `GET /api/devices`.
|
|
59
|
+
* @returns The current device row, or `undefined` when the user has none.
|
|
60
|
+
*/
|
|
61
|
+
export declare const findCurrentDevice: (devices: PushToggleDevice[] | undefined) => PushToggleDevice | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Converts an `@molecule/app-push` token into the device resource's
|
|
64
|
+
* `pushSubscription` shape (web PushSubscription JSON, FCM registration for
|
|
65
|
+
* Android, APNs registration for iOS).
|
|
66
|
+
*
|
|
67
|
+
* @param token - The push token returned by `register()`.
|
|
68
|
+
* @returns The `pushSubscription` value to PATCH onto the device row.
|
|
69
|
+
*/
|
|
70
|
+
export declare const subscriptionFromToken: (token: PushToggleToken) => unknown;
|
|
71
|
+
/**
|
|
72
|
+
* Runs the full enable chain: browser permission → runtime VAPID public key
|
|
73
|
+
* (`GET /api/devices/push/public-key`) → `register({ vapidPublicKey })` →
|
|
74
|
+
* persist the subscription on the current device row.
|
|
75
|
+
*
|
|
76
|
+
* Every failure is returned as a typed reason (never thrown) so the UI can
|
|
77
|
+
* show an honest, specific message instead of hanging or silently reverting.
|
|
78
|
+
*
|
|
79
|
+
* @param deps - The http client + push actions from `usePush()`.
|
|
80
|
+
* @param deps.http - Authenticated http client (`useHttpClient()`).
|
|
81
|
+
* @param deps.requestPermission - `usePush().requestPermission`.
|
|
82
|
+
* @param deps.register - `usePush().register`.
|
|
83
|
+
* @returns `{ ok: true }` or a typed failure.
|
|
84
|
+
*/
|
|
85
|
+
export declare const enablePushOnCurrentDevice: (deps: {
|
|
86
|
+
http: PushToggleHttp;
|
|
87
|
+
requestPermission: () => Promise<string>;
|
|
88
|
+
register: (options?: {
|
|
89
|
+
vapidPublicKey?: string;
|
|
90
|
+
}) => Promise<PushToggleToken>;
|
|
91
|
+
}) => Promise<PushToggleResult>;
|
|
92
|
+
/**
|
|
93
|
+
* Disables push: unsubscribes the browser (best-effort — a dev build without
|
|
94
|
+
* a service worker has nothing to unsubscribe) and ALWAYS clears the server
|
|
95
|
+
* state so no further pushes target this device.
|
|
96
|
+
*
|
|
97
|
+
* @param deps - The http client + push action from `usePush()`.
|
|
98
|
+
* @param deps.http - Authenticated http client (`useHttpClient()`).
|
|
99
|
+
* @param deps.unregister - `usePush().unregister`.
|
|
100
|
+
* @returns `{ ok: true }` or a typed failure (server state not cleared).
|
|
101
|
+
*/
|
|
102
|
+
export declare const disablePushOnCurrentDevice: (deps: {
|
|
103
|
+
http: PushToggleHttp;
|
|
104
|
+
unregister: () => Promise<void>;
|
|
105
|
+
}) => Promise<PushToggleResult>;
|
|
106
|
+
/**
|
|
107
|
+
* Reads whether push is currently enabled for THIS device (server truth:
|
|
108
|
+
* the current device row's `hasPushSubscription`).
|
|
109
|
+
*
|
|
110
|
+
* @param http - Authenticated http client (`useHttpClient()`).
|
|
111
|
+
* @returns `true` when the current device has a stored subscription.
|
|
112
|
+
*/
|
|
113
|
+
export declare const readCurrentDevicePushEnabled: (http: PushToggleHttp) => Promise<boolean>;
|
|
114
|
+
//# sourceMappingURL=device-toggle.d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AAI,OAAO,eAAe,KAAG,OAS9D,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,yBAAyB,GAAU,MAAM;IACpD,IAAI,EAAE,cAAc,CAAA;IACpB,iBAAiB,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAA;IACxC,QAAQ,EAAE,CAAC,OAAO,CAAC,EAAE;QAAE,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;CAC9E,KAAG,OAAO,CAAC,gBAAgB,CAmD3B,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,0BAA0B,GAAU,MAAM;IACrD,IAAI,EAAE,cAAc,CAAA;IACpB,UAAU,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAChC,KAAG,OAAO,CAAC,gBAAgB,CA0B3B,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,GAAU,MAAM,cAAc,KAAG,OAAO,CAAC,OAAO,CASxF,CAAA"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The push-notifications toggle for the signed-in user's current device: the
|
|
3
|
+
* full enable/disable chain — permission → runtime VAPID key fetch →
|
|
4
|
+
* subscribe → persist on the device row — as plain functions over an HTTP
|
|
5
|
+
* client and this package's `requestPermission` / `register` /
|
|
6
|
+
* `unregister`. Every framework's settings panel
|
|
7
|
+
* (`@molecule/app-settings-panel-react`, `-vue`, `-solid`, `-svelte`,
|
|
8
|
+
* `-angular`) re-exports these, so the chain is the same everywhere.
|
|
9
|
+
*
|
|
10
|
+
* The server contract (shared by every molecule api):
|
|
11
|
+
*
|
|
12
|
+
* - `GET /api/devices/push/public-key` → `{ publicKey }` — the VAPID public
|
|
13
|
+
* key the browser must pass as `applicationServerKey` (Chromium rejects
|
|
14
|
+
* keyless subscriptions). 404/503 when push is not configured server-side.
|
|
15
|
+
* - `GET /api/devices` → the caller's device rows; the session's own device
|
|
16
|
+
* is flagged `isCurrent: true`.
|
|
17
|
+
* - `PATCH /api/devices/:id { pushSubscription, hasPushSubscription }` —
|
|
18
|
+
* persists the browser subscription where the api-side push fan-outs read
|
|
19
|
+
* it. Without this PATCH the server never knows the subscription exists
|
|
20
|
+
* and no push can ever reach the user.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Picks the caller's own device row: the api flags the session device
|
|
26
|
+
* `isCurrent`; fall back to the first row for sessions predating the flag.
|
|
27
|
+
*
|
|
28
|
+
* @param devices - Rows from `GET /api/devices`.
|
|
29
|
+
* @returns The current device row, or `undefined` when the user has none.
|
|
30
|
+
*/
|
|
31
|
+
export const findCurrentDevice = (devices) => {
|
|
32
|
+
if (!Array.isArray(devices) || devices.length === 0)
|
|
33
|
+
return undefined;
|
|
34
|
+
return devices.find((device) => device.isCurrent) ?? devices[0];
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Converts an `@molecule/app-push` token into the device resource's
|
|
38
|
+
* `pushSubscription` shape (web PushSubscription JSON, FCM registration for
|
|
39
|
+
* Android, APNs registration for iOS).
|
|
40
|
+
*
|
|
41
|
+
* @param token - The push token returned by `register()`.
|
|
42
|
+
* @returns The `pushSubscription` value to PATCH onto the device row.
|
|
43
|
+
*/
|
|
44
|
+
export const subscriptionFromToken = (token) => {
|
|
45
|
+
if (token.platform === 'android') {
|
|
46
|
+
return { registrationId: token.value, registrationType: 'FCM' };
|
|
47
|
+
}
|
|
48
|
+
if (token.platform === 'ios') {
|
|
49
|
+
return { registrationId: token.value };
|
|
50
|
+
}
|
|
51
|
+
// Web: register() serializes `subscription.toJSON()` — endpoint + keys.
|
|
52
|
+
return JSON.parse(token.value);
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Runs the full enable chain: browser permission → runtime VAPID public key
|
|
56
|
+
* (`GET /api/devices/push/public-key`) → `register({ vapidPublicKey })` →
|
|
57
|
+
* persist the subscription on the current device row.
|
|
58
|
+
*
|
|
59
|
+
* Every failure is returned as a typed reason (never thrown) so the UI can
|
|
60
|
+
* show an honest, specific message instead of hanging or silently reverting.
|
|
61
|
+
*
|
|
62
|
+
* @param deps - The http client + push actions from `usePush()`.
|
|
63
|
+
* @param deps.http - Authenticated http client (`useHttpClient()`).
|
|
64
|
+
* @param deps.requestPermission - `usePush().requestPermission`.
|
|
65
|
+
* @param deps.register - `usePush().register`.
|
|
66
|
+
* @returns `{ ok: true }` or a typed failure.
|
|
67
|
+
*/
|
|
68
|
+
export const enablePushOnCurrentDevice = async (deps) => {
|
|
69
|
+
const permission = await deps.requestPermission();
|
|
70
|
+
if (permission !== 'granted') {
|
|
71
|
+
return { ok: false, reason: 'permission-denied' };
|
|
72
|
+
}
|
|
73
|
+
let publicKey;
|
|
74
|
+
try {
|
|
75
|
+
const response = await deps.http.get('/api/devices/push/public-key');
|
|
76
|
+
publicKey = response.data?.publicKey;
|
|
77
|
+
}
|
|
78
|
+
catch (_error) {
|
|
79
|
+
// 404/503 from the bond-gated route — push is not configured server-side.
|
|
80
|
+
// Reported to the user as 'server-unconfigured'; nothing else to log here.
|
|
81
|
+
publicKey = undefined;
|
|
82
|
+
}
|
|
83
|
+
if (!publicKey) {
|
|
84
|
+
return { ok: false, reason: 'server-unconfigured' };
|
|
85
|
+
}
|
|
86
|
+
let token;
|
|
87
|
+
try {
|
|
88
|
+
token = await deps.register({ vapidPublicKey: publicKey });
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
// The provider throws already-localized messages (not supported / no
|
|
92
|
+
// service worker in dev builds / push-service rejection) — surface them.
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
reason: 'register-failed',
|
|
96
|
+
message: error instanceof Error ? error.message : String(error),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const devices = await deps.http.get('/api/devices');
|
|
101
|
+
const device = findCurrentDevice(devices.data);
|
|
102
|
+
if (!device) {
|
|
103
|
+
return { ok: false, reason: 'persist-failed' };
|
|
104
|
+
}
|
|
105
|
+
await deps.http.patch(`/api/devices/${device.id}`, {
|
|
106
|
+
pushSubscription: subscriptionFromToken(token),
|
|
107
|
+
hasPushSubscription: true,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
return {
|
|
112
|
+
ok: false,
|
|
113
|
+
reason: 'persist-failed',
|
|
114
|
+
message: error instanceof Error ? error.message : String(error),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return { ok: true };
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Disables push: unsubscribes the browser (best-effort — a dev build without
|
|
121
|
+
* a service worker has nothing to unsubscribe) and ALWAYS clears the server
|
|
122
|
+
* state so no further pushes target this device.
|
|
123
|
+
*
|
|
124
|
+
* @param deps - The http client + push action from `usePush()`.
|
|
125
|
+
* @param deps.http - Authenticated http client (`useHttpClient()`).
|
|
126
|
+
* @param deps.unregister - `usePush().unregister`.
|
|
127
|
+
* @returns `{ ok: true }` or a typed failure (server state not cleared).
|
|
128
|
+
*/
|
|
129
|
+
export const disablePushOnCurrentDevice = async (deps) => {
|
|
130
|
+
try {
|
|
131
|
+
await deps.unregister();
|
|
132
|
+
}
|
|
133
|
+
catch (_error) {
|
|
134
|
+
// Best-effort: browser-side unsubscribe can fail (no service worker in a
|
|
135
|
+
// dev build); the server-side clear below is what stops future sends.
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
const devices = await deps.http.get('/api/devices');
|
|
139
|
+
const device = findCurrentDevice(devices.data);
|
|
140
|
+
if (device) {
|
|
141
|
+
await deps.http.patch(`/api/devices/${device.id}`, {
|
|
142
|
+
pushSubscription: null,
|
|
143
|
+
hasPushSubscription: false,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
return {
|
|
149
|
+
ok: false,
|
|
150
|
+
reason: 'persist-failed',
|
|
151
|
+
message: error instanceof Error ? error.message : String(error),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return { ok: true };
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Reads whether push is currently enabled for THIS device (server truth:
|
|
158
|
+
* the current device row's `hasPushSubscription`).
|
|
159
|
+
*
|
|
160
|
+
* @param http - Authenticated http client (`useHttpClient()`).
|
|
161
|
+
* @returns `true` when the current device has a stored subscription.
|
|
162
|
+
*/
|
|
163
|
+
export const readCurrentDevicePushEnabled = async (http) => {
|
|
164
|
+
try {
|
|
165
|
+
const devices = await http.get('/api/devices');
|
|
166
|
+
return findCurrentDevice(devices.data)?.hasPushSubscription === true;
|
|
167
|
+
}
|
|
168
|
+
catch (_error) {
|
|
169
|
+
// Devices endpoint unavailable (e.g. unauthenticated preview) — render
|
|
170
|
+
// the toggle off rather than failing the whole settings panel.
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
//# sourceMappingURL=device-toggle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device-toggle.js","sourceRoot":"","sources":["../src/device-toggle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AA6BH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,OAAuC,EACT,EAAE;IAChC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,SAAS,CAAA;IACrE,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAA;AACjE,CAAC,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,KAAsB,EAAW,EAAE;IACvE,IAAI,KAAK,CAAC,QAAQ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,CAAC;IAED,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAqB,cAAc,CAAC,CAAA;QACvE,MAAM,MAAM,GAAG,iBAAiB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC9C,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,gBAAgB,MAAM,CAAC,EAAE,EAAE,EAAE;gBACjD,gBAAgB,EAAE,IAAI;gBACtB,mBAAmB,EAAE,KAAK;aAC3B,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO;YACL,EAAE,EAAE,KAAK;YACT,MAAM,EAAE,gBAAgB;YACxB,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;SAChE,CAAA;IACH,CAAC;IAED,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,CAAA;AACrB,CAAC,CAAA;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,KAAK,EAAE,IAAoB,EAAoB,EAAE;IAC3F,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,GAAG,CAAqB,cAAc,CAAC,CAAA;QAClE,OAAO,iBAAiB,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,mBAAmB,KAAK,IAAI,CAAA;IACtE,CAAC;IAAC,OAAO,MAAM,EAAE,CAAC;QAChB,uEAAuE;QACvE,+DAA+D;QAC/D,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -38,9 +38,11 @@
|
|
|
38
38
|
* `setProvider()` BEFORE any push call — otherwise the web fallback gets
|
|
39
39
|
* bonded and native registration never happens.
|
|
40
40
|
* - **Web `register()` has three hard prerequisites**: (1) HTTPS (secure
|
|
41
|
-
* context), (2) a registered SERVICE WORKER —
|
|
42
|
-
*
|
|
43
|
-
*
|
|
41
|
+
* context), (2) a registered SERVICE WORKER — the one
|
|
42
|
+
* `@molecule/app-pwa-default` (a peer of this package) registers in the
|
|
43
|
+
* production build, which also shows the delivered notifications; dev
|
|
44
|
+
* builds don't ship one, so register() throws "requires the production app
|
|
45
|
+
* build" (it fails fast instead of hanging), and (3) a VAPID public key
|
|
44
46
|
* (pass via `register({ vapidPublicKey })`) matching your API's
|
|
45
47
|
* `VAPID_PRIVATE_KEY` — without it Chrome may register but delivery
|
|
46
48
|
* fails.
|
|
@@ -52,9 +54,18 @@
|
|
|
52
54
|
* delivery send a real push from the server.
|
|
53
55
|
* - `token.value` on web is a JSON-serialized PushSubscription (not an FCM
|
|
54
56
|
* token) — store it opaquely; platform is in `token.platform`.
|
|
57
|
+
* - **A settings toggle for the current device** needs no code of its own:
|
|
58
|
+
* `enablePushOnCurrentDevice({ http, requestPermission, register })`,
|
|
59
|
+
* `disablePushOnCurrentDevice({ http, unregister })` and
|
|
60
|
+
* `readCurrentDevicePushEnabled(http)` run the whole chain against the
|
|
61
|
+
* molecule device API (`GET /api/devices/push/public-key`,
|
|
62
|
+
* `GET /api/devices`, `PATCH /api/devices/:id`) and return a typed result
|
|
63
|
+
* instead of throwing. Every framework's settings panel package re-exports
|
|
64
|
+
* them.
|
|
55
65
|
*
|
|
56
66
|
* @module
|
|
57
67
|
*/
|
|
68
|
+
export * from './device-toggle.js';
|
|
58
69
|
export * from './provider.js';
|
|
59
70
|
export * from './types.js';
|
|
60
71
|
export * from './web-provider.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkEG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -38,9 +38,11 @@
|
|
|
38
38
|
* `setProvider()` BEFORE any push call — otherwise the web fallback gets
|
|
39
39
|
* bonded and native registration never happens.
|
|
40
40
|
* - **Web `register()` has three hard prerequisites**: (1) HTTPS (secure
|
|
41
|
-
* context), (2) a registered SERVICE WORKER —
|
|
42
|
-
*
|
|
43
|
-
*
|
|
41
|
+
* context), (2) a registered SERVICE WORKER — the one
|
|
42
|
+
* `@molecule/app-pwa-default` (a peer of this package) registers in the
|
|
43
|
+
* production build, which also shows the delivered notifications; dev
|
|
44
|
+
* builds don't ship one, so register() throws "requires the production app
|
|
45
|
+
* build" (it fails fast instead of hanging), and (3) a VAPID public key
|
|
44
46
|
* (pass via `register({ vapidPublicKey })`) matching your API's
|
|
45
47
|
* `VAPID_PRIVATE_KEY` — without it Chrome may register but delivery
|
|
46
48
|
* fails.
|
|
@@ -52,9 +54,18 @@
|
|
|
52
54
|
* delivery send a real push from the server.
|
|
53
55
|
* - `token.value` on web is a JSON-serialized PushSubscription (not an FCM
|
|
54
56
|
* token) — store it opaquely; platform is in `token.platform`.
|
|
57
|
+
* - **A settings toggle for the current device** needs no code of its own:
|
|
58
|
+
* `enablePushOnCurrentDevice({ http, requestPermission, register })`,
|
|
59
|
+
* `disablePushOnCurrentDevice({ http, unregister })` and
|
|
60
|
+
* `readCurrentDevicePushEnabled(http)` run the whole chain against the
|
|
61
|
+
* molecule device API (`GET /api/devices/push/public-key`,
|
|
62
|
+
* `GET /api/devices`, `PATCH /api/devices/:id`) and return a typed result
|
|
63
|
+
* instead of throwing. Every framework's settings panel package re-exports
|
|
64
|
+
* them.
|
|
55
65
|
*
|
|
56
66
|
* @module
|
|
57
67
|
*/
|
|
68
|
+
export * from './device-toggle.js';
|
|
58
69
|
export * from './provider.js';
|
|
59
70
|
export * from './types.js';
|
|
60
71
|
export * from './web-provider.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkEG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/app-push",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Client-side push notifications interface for molecule.dev",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -29,11 +29,12 @@
|
|
|
29
29
|
"license": "Apache-2.0",
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@molecule/app-bond": "^1.0.1",
|
|
32
|
-
"@molecule/app-i18n": "^1.0.1"
|
|
32
|
+
"@molecule/app-i18n": "^1.0.1",
|
|
33
|
+
"@molecule/app-pwa-default": "^1.1.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@molecule/app-bond": "1.0.
|
|
36
|
-
"@molecule/app-i18n": "1.
|
|
36
|
+
"@molecule/app-bond": "1.0.5",
|
|
37
|
+
"@molecule/app-i18n": "1.2.2",
|
|
37
38
|
"@types/node": "26.6.5",
|
|
38
39
|
"typescript": "6.0.3",
|
|
39
40
|
"vitest": "4.1.12"
|