@molecule/app-push 1.0.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/LICENSE ADDED
@@ -0,0 +1,115 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work.
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship.
43
+
44
+ "Contribution" shall mean any work of authorship, including the
45
+ original version of the Work and any modifications or additions
46
+ to that Work, that is intentionally submitted to the Licensor for
47
+ inclusion in the Work by the copyright owner or by an individual or
48
+ Legal Entity authorized to submit on behalf of the copyright owner.
49
+
50
+ "Contributor" shall mean Licensor and any individual or Legal Entity
51
+ on behalf of whom a Contribution has been received by the Licensor and
52
+ subsequently incorporated within the Work.
53
+
54
+ 2. Grant of Copyright License. Subject to the terms and conditions of
55
+ this License, each Contributor hereby grants to You a perpetual,
56
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
57
+ copyright license to reproduce, prepare Derivative Works of,
58
+ publicly display, publicly perform, sublicense, and distribute the
59
+ Work and such Derivative Works in Source or Object form.
60
+
61
+ 3. Grant of Patent License. Subject to the terms and conditions of
62
+ this License, each Contributor hereby grants to You a perpetual,
63
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
64
+ patent license to make, have made, use, offer to sell, sell, import,
65
+ and otherwise transfer the Work.
66
+
67
+ 4. Redistribution. You may reproduce and distribute copies of the
68
+ Work or Derivative Works thereof in any medium, with or without
69
+ modifications, and in Source or Object form, provided that You
70
+ meet the following conditions:
71
+
72
+ (a) You must give any other recipients of the Work or
73
+ Derivative Works a copy of this License; and
74
+
75
+ (b) You must cause any modified files to carry prominent notices
76
+ stating that You changed the files; and
77
+
78
+ (c) You must retain, in the Source form of any Derivative Works
79
+ that You distribute, all copyright, patent, trademark, and
80
+ attribution notices from the Source form of the Work,
81
+ excluding those notices that do not pertain to any part of
82
+ the Derivative Works; and
83
+
84
+ (d) If the Work includes a "NOTICE" text file as part of its
85
+ distribution, then any Derivative Works that You distribute must
86
+ include a readable copy of the attribution notices contained
87
+ within such NOTICE file.
88
+
89
+ 5. Submission of Contributions.
90
+
91
+ 6. Trademarks. This License does not grant permission to use the trade
92
+ names, trademarks, service marks, or product names of the Licensor.
93
+
94
+ 7. Disclaimer of Warranty. Unless required by applicable law or
95
+ agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
96
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
97
+
98
+ 8. Limitation of Liability. In no event and under no legal theory shall
99
+ any Contributor be liable to You for damages.
100
+
101
+ 9. Accepting Warranty or Additional Liability.
102
+
103
+ Copyright 2026 Molecule Dev, Inc.
104
+
105
+ Licensed under the Apache License, Version 2.0 (the "License");
106
+ you may not use this file except in compliance with the License.
107
+ You may obtain a copy of the License at
108
+
109
+ http://www.apache.org/licenses/LICENSE-2.0
110
+
111
+ Unless required by applicable law or agreed to in writing, software
112
+ distributed under the License is distributed on an "AS IS" BASIS,
113
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
+ See the License for the specific language governing permissions and
115
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,612 @@
1
+ <!--
2
+ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
+ Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
+ Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
+ To change this document, edit the module-level JSDoc in src/index.ts.
6
+ Generated: 2026-08-04T00:40:50.304Z
7
+ -->
8
+
9
+ # @molecule/app-push
10
+
11
+ > **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
12
+ > It is written to be read by coding agents as much as by people, and is generated from this
13
+ > package's source — edit `src/index.ts` JSDoc, not this file.
14
+
15
+ Client-side push notifications interface for molecule.dev.
16
+
17
+ Provides a unified API for push notifications across platforms (web,
18
+ native containers, etc.): permission flow (`checkPermission`,
19
+ `requestPermission`), registration (`register`, `getToken`), incoming
20
+ events (`onNotificationReceived`, `onNotificationAction`), local
21
+ notifications (`scheduleLocal`), and badges (`setBadge`, `clearBadge`).
22
+ The device token is what your API's `@molecule/api-push-notifications`
23
+ bond sends to.
24
+
25
+ ## Quick Start
26
+
27
+ ```typescript
28
+ import { getToken, onNotificationAction, register, requestPermission } from '@molecule/app-push'
29
+
30
+ async function enablePush(vapidPublicKey: string): Promise<string | null> {
31
+ const permission = await requestPermission() // from a user gesture
32
+ if (permission !== 'granted') return null
33
+ const token = await register({ vapidPublicKey })
34
+ // POST token.value to your API so api-push-notifications can target it
35
+ return token.value
36
+ }
37
+
38
+ function handleTaps(open: (data: unknown) => void): () => void {
39
+ return onNotificationAction((event) => open(event.notification.data))
40
+ }
41
+ ```
42
+
43
+ ## Type
44
+
45
+ `native`
46
+
47
+ ## Installation
48
+
49
+ ```bash
50
+ npm install @molecule/app-push @molecule/app-bond @molecule/app-i18n
51
+ ```
52
+
53
+ ## API
54
+
55
+ ### Interfaces
56
+
57
+ #### `LocalNotificationOptions`
58
+
59
+ Options for scheduling a local notification.
60
+
61
+ ```typescript
62
+ interface LocalNotificationOptions {
63
+ /**
64
+ * Notification ID.
65
+ */
66
+ id?: string
67
+
68
+ /**
69
+ * Notification title.
70
+ */
71
+ title: string
72
+
73
+ /**
74
+ * Notification body.
75
+ */
76
+ body?: string
77
+
78
+ /**
79
+ * Schedule at a specific time.
80
+ */
81
+ at?: Date
82
+
83
+ /**
84
+ * Repeat interval.
85
+ */
86
+ repeat?: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'
87
+
88
+ /**
89
+ * Extra data.
90
+ */
91
+ extra?: Record<string, unknown>
92
+
93
+ /**
94
+ * Sound to play.
95
+ */
96
+ sound?: string
97
+
98
+ /**
99
+ * Badge count.
100
+ */
101
+ badge?: number
102
+
103
+ /**
104
+ * Notification channel (Android).
105
+ */
106
+ channelId?: string
107
+
108
+ /**
109
+ * Actions/buttons.
110
+ */
111
+ actions?: PushNotificationAction[]
112
+ }
113
+ ```
114
+
115
+ #### `NotificationActionEvent`
116
+
117
+ Notification action event.
118
+
119
+ ```typescript
120
+ interface NotificationActionEvent {
121
+ /**
122
+ * The notification.
123
+ */
124
+ notification: PushNotification
125
+
126
+ /**
127
+ * Action that was triggered.
128
+ */
129
+ actionId?: string
130
+ }
131
+ ```
132
+
133
+ #### `NotificationReceivedEvent`
134
+
135
+ Notification received event.
136
+
137
+ ```typescript
138
+ interface NotificationReceivedEvent {
139
+ /**
140
+ * The notification.
141
+ */
142
+ notification: PushNotification
143
+
144
+ /**
145
+ * Whether the app was in foreground.
146
+ */
147
+ foreground: boolean
148
+ }
149
+ ```
150
+
151
+ #### `PushNotification`
152
+
153
+ Push notification payload.
154
+
155
+ ```typescript
156
+ interface PushNotification {
157
+ /**
158
+ * Notification ID.
159
+ */
160
+ id: string
161
+
162
+ /**
163
+ * Notification title.
164
+ */
165
+ title: string
166
+
167
+ /**
168
+ * Notification body/message.
169
+ */
170
+ body?: string
171
+
172
+ /**
173
+ * Notification data payload.
174
+ */
175
+ data?: Record<string, unknown>
176
+
177
+ /**
178
+ * Badge count.
179
+ */
180
+ badge?: number
181
+
182
+ /**
183
+ * Sound to play.
184
+ */
185
+ sound?: string
186
+
187
+ /**
188
+ * Icon URL.
189
+ */
190
+ icon?: string
191
+
192
+ /**
193
+ * Image URL.
194
+ */
195
+ image?: string
196
+
197
+ /**
198
+ * Click action/URL.
199
+ */
200
+ clickAction?: string
201
+
202
+ /**
203
+ * Notification tag (for grouping).
204
+ */
205
+ tag?: string
206
+
207
+ /**
208
+ * Whether the notification requires interaction.
209
+ */
210
+ requireInteraction?: boolean
211
+
212
+ /**
213
+ * Notification timestamp.
214
+ */
215
+ timestamp?: number
216
+
217
+ /**
218
+ * Notification actions (buttons).
219
+ */
220
+ actions?: PushNotificationAction[]
221
+ }
222
+ ```
223
+
224
+ #### `PushNotificationAction`
225
+
226
+ Notification action button.
227
+
228
+ ```typescript
229
+ interface PushNotificationAction {
230
+ /**
231
+ * Action ID.
232
+ */
233
+ id: string
234
+
235
+ /**
236
+ * Action title.
237
+ */
238
+ title: string
239
+
240
+ /**
241
+ * Action icon.
242
+ */
243
+ icon?: string
244
+ }
245
+ ```
246
+
247
+ #### `PushProvider`
248
+
249
+ Push notifications provider interface.
250
+
251
+ All push providers must implement this interface.
252
+
253
+ ```typescript
254
+ interface PushProvider {
255
+ /**
256
+ * Checks the current permission status.
257
+ */
258
+ checkPermission(): Promise<PermissionStatus>
259
+
260
+ /**
261
+ * Requests notification permission.
262
+ */
263
+ requestPermission(): Promise<PermissionStatus>
264
+
265
+ /**
266
+ * Registers for push notifications and gets a token.
267
+ *
268
+ * @param options - Optional registration options (e.g. a runtime VAPID
269
+ * public key for web subscriptions).
270
+ */
271
+ register(options?: PushRegisterOptions): Promise<PushToken>
272
+
273
+ /**
274
+ * Unregisters from push notifications.
275
+ */
276
+ unregister(): Promise<void>
277
+
278
+ /**
279
+ * Gets the current push token.
280
+ * @returns The current token, or `null` if not registered.
281
+ */
282
+ getToken(): Promise<PushToken | null>
283
+
284
+ /**
285
+ * Subscribes to notification received events.
286
+ */
287
+ onNotificationReceived(listener: NotificationReceivedListener): () => void
288
+
289
+ /**
290
+ * Subscribes to notification action events (taps, button clicks).
291
+ */
292
+ onNotificationAction(listener: NotificationActionListener): () => void
293
+
294
+ /**
295
+ * Subscribes to token changes.
296
+ */
297
+ onTokenChange(listener: TokenChangeListener): () => void
298
+
299
+ /**
300
+ * Schedules a local notification.
301
+ */
302
+ scheduleLocal(options: LocalNotificationOptions): Promise<string>
303
+
304
+ /**
305
+ * Cancels a local notification.
306
+ */
307
+ cancelLocal(id: string): Promise<void>
308
+
309
+ /**
310
+ * Cancels all local notifications.
311
+ */
312
+ cancelAllLocal(): Promise<void>
313
+
314
+ /**
315
+ * Gets pending local notifications.
316
+ */
317
+ getPendingLocal(): Promise<LocalNotificationOptions[]>
318
+
319
+ /**
320
+ * Gets delivered notifications.
321
+ */
322
+ getDelivered(): Promise<PushNotification[]>
323
+
324
+ /**
325
+ * Removes delivered notifications.
326
+ */
327
+ removeDelivered(ids: string[]): Promise<void>
328
+
329
+ /**
330
+ * Removes all delivered notifications.
331
+ */
332
+ removeAllDelivered(): Promise<void>
333
+
334
+ /**
335
+ * Sets the badge count.
336
+ */
337
+ setBadge(count: number): Promise<void>
338
+
339
+ /**
340
+ * Gets the badge count.
341
+ */
342
+ getBadge(): Promise<number>
343
+
344
+ /**
345
+ * Clears the badge.
346
+ */
347
+ clearBadge(): Promise<void>
348
+
349
+ /**
350
+ * Destroys the provider.
351
+ */
352
+ destroy(): void
353
+ }
354
+ ```
355
+
356
+ #### `PushRegisterOptions`
357
+
358
+ Options for {@link PushProvider.register}.
359
+
360
+ ```typescript
361
+ interface PushRegisterOptions {
362
+ /**
363
+ * VAPID public key (URL-safe base64) used as the `applicationServerKey`
364
+ * when subscribing on the web. Chromium rejects keyless subscriptions, so
365
+ * web apps should deliver the server's key at runtime (e.g. from
366
+ * `GET /api/devices/push/public-key`) and pass it here. Takes precedence
367
+ * over any key the provider was constructed with. Ignored by providers
368
+ * whose platform does not use VAPID (e.g. FCM/APNs native providers).
369
+ */
370
+ vapidPublicKey?: string
371
+ }
372
+ ```
373
+
374
+ #### `PushToken`
375
+
376
+ Push token info.
377
+
378
+ ```typescript
379
+ interface PushToken {
380
+ /**
381
+ * Token value.
382
+ */
383
+ value: string
384
+
385
+ /**
386
+ * Platform the token is for.
387
+ */
388
+ platform: 'web' | 'ios' | 'android'
389
+
390
+ /**
391
+ * When the token was obtained.
392
+ */
393
+ timestamp: number
394
+ }
395
+ ```
396
+
397
+ ### Types
398
+
399
+ #### `NotificationActionListener`
400
+
401
+ Notification Action Listener type.
402
+
403
+ ```typescript
404
+ type NotificationActionListener = (event: NotificationActionEvent) => void
405
+ ```
406
+
407
+ #### `NotificationReceivedListener`
408
+
409
+ Listener invoked when a push notification is received.
410
+
411
+ ```typescript
412
+ type NotificationReceivedListener = (event: NotificationReceivedEvent) => void
413
+ ```
414
+
415
+ #### `PermissionStatus`
416
+
417
+ Push notification permission status.
418
+
419
+ ```typescript
420
+ type PermissionStatus = 'granted' | 'denied' | 'default' | 'prompt'
421
+ ```
422
+
423
+ #### `TokenChangeListener`
424
+
425
+ Token Change Listener type.
426
+
427
+ ```typescript
428
+ type TokenChangeListener = (token: PushToken) => void
429
+ ```
430
+
431
+ ### Functions
432
+
433
+ #### `checkPermission()`
434
+
435
+ Checks the current notification permission status.
436
+
437
+ ```typescript
438
+ function checkPermission(): Promise<PermissionStatus>
439
+ ```
440
+
441
+ **Returns:** The current permission status (granted, denied, default, or prompt).
442
+
443
+ #### `clearBadge()`
444
+
445
+ Clears the app icon badge count.
446
+
447
+ ```typescript
448
+ function clearBadge(): Promise<void>
449
+ ```
450
+
451
+ **Returns:** A promise that resolves when the badge count is cleared.
452
+
453
+ #### `createWebPushProvider(vapidPublicKey)`
454
+
455
+ Creates a web push provider using the browser Push API and Notification API.
456
+
457
+ ```typescript
458
+ function createWebPushProvider(vapidPublicKey?: string): PushProvider
459
+ ```
460
+
461
+ - `vapidPublicKey` — Optional VAPID public key for server-authenticated subscriptions.
462
+
463
+ **Returns:** A {@link PushProvider} backed by the Web Push API.
464
+
465
+ #### `getProvider()`
466
+
467
+ Gets the current push provider, creating a default web provider if none is bonded.
468
+
469
+ ```typescript
470
+ function getProvider(): PushProvider
471
+ ```
472
+
473
+ **Returns:** The active push provider instance.
474
+
475
+ #### `getToken()`
476
+
477
+ Gets the current push token.
478
+
479
+ ```typescript
480
+ function getToken(): Promise<PushToken | null>
481
+ ```
482
+
483
+ **Returns:** The current token, or `null` if not registered.
484
+
485
+ #### `hasProvider()`
486
+
487
+ Checks if a push provider has been bonded.
488
+
489
+ ```typescript
490
+ function hasProvider(): boolean
491
+ ```
492
+
493
+ **Returns:** Whether a push provider is currently registered.
494
+
495
+ #### `onNotificationAction(listener)`
496
+
497
+ Subscribes to notification action events (taps, button clicks).
498
+
499
+ ```typescript
500
+ function onNotificationAction(listener: NotificationActionListener): () => void
501
+ ```
502
+
503
+ - `listener` — Callback invoked when a notification action is triggered.
504
+
505
+ **Returns:** An unsubscribe function to remove the listener.
506
+
507
+ #### `onNotificationReceived(listener)`
508
+
509
+ Subscribes to notification received events.
510
+
511
+ ```typescript
512
+ function onNotificationReceived(listener: NotificationReceivedListener): () => void
513
+ ```
514
+
515
+ - `listener` — Callback invoked when a notification is received.
516
+
517
+ **Returns:** An unsubscribe function to remove the listener.
518
+
519
+ #### `register(options)`
520
+
521
+ Registers for push notifications and obtains a device token.
522
+
523
+ ```typescript
524
+ function register(options?: PushRegisterOptions): Promise<PushToken>
525
+ ```
526
+
527
+ - `options` — Optional registration options (e.g. a runtime VAPID public key).
528
+
529
+ **Returns:** The push token for this device.
530
+
531
+ #### `requestPermission()`
532
+
533
+ Requests notification permission from the user.
534
+
535
+ ```typescript
536
+ function requestPermission(): Promise<PermissionStatus>
537
+ ```
538
+
539
+ **Returns:** The resulting permission status after the user responds.
540
+
541
+ #### `scheduleLocal(options)`
542
+
543
+ Schedules a local notification.
544
+
545
+ ```typescript
546
+ function scheduleLocal(options: LocalNotificationOptions): Promise<string>
547
+ ```
548
+
549
+ - `options` — Configuration for the notification (title, body, schedule time, etc.).
550
+
551
+ **Returns:** The notification ID that can be used to cancel it later.
552
+
553
+ #### `setBadge(count)`
554
+
555
+ Sets the app icon badge count.
556
+
557
+ ```typescript
558
+ function setBadge(count: number): Promise<void>
559
+ ```
560
+
561
+ - `count` — The badge number to display on the app icon.
562
+
563
+ **Returns:** A promise that resolves when the badge count is set.
564
+
565
+ #### `setProvider(provider)`
566
+
567
+ Sets the push provider implementation.
568
+
569
+ ```typescript
570
+ function setProvider(provider: PushProvider): void
571
+ ```
572
+
573
+ - `provider` — The push provider to bond as the active implementation.
574
+
575
+ ## Injection Notes
576
+
577
+ ### Requirements
578
+
579
+ Peer dependencies:
580
+
581
+ - `@molecule/app-bond` ^1.0.0
582
+ - `@molecule/app-i18n` ^1.0.0
583
+
584
+ ### Runtime Dependencies
585
+
586
+ - `@molecule/app-bond`
587
+ - `@molecule/app-i18n`
588
+
589
+ - **No wiring is needed on web**: the first accessor call silently bonds
590
+ the built-in Web Push provider. In a native app wire
591
+ `@molecule/app-push-react-native` (the one prebuilt bond) via
592
+ `setProvider()` BEFORE any push call — otherwise the web fallback gets
593
+ bonded and native registration never happens.
594
+ - **Web `register()` has three hard prerequisites**: (1) HTTPS (secure
595
+ context), (2) a registered SERVICE WORKER — dev builds typically don't
596
+ ship one, so register() throws "requires the production app build"
597
+ (it fails fast instead of hanging), and (3) a VAPID public key
598
+ (pass via `register({ vapidPublicKey })`) matching your API's
599
+ `VAPID_PRIVATE_KEY` — without it Chrome may register but delivery
600
+ fails.
601
+ - **Request permission from a user gesture at the point of value**, never
602
+ on load — a denied browser prompt is remembered forever (no re-prompt).
603
+ - Web `scheduleLocal` at a future time uses an in-page timer: it does NOT
604
+ survive reload/close. Web `cancelLocal`/`cancelAllLocal` are no-ops,
605
+ and `getPendingLocal()` is always empty — for reliable scheduled
606
+ delivery send a real push from the server.
607
+ - `token.value` on web is a JSON-serialized PushSubscription (not an FCM
608
+ token) — store it opaquely; platform is in `token.platform`.
609
+
610
+ ## Translations
611
+
612
+ Translation strings are provided by `@molecule/app-locales-push`.