@haatlabs-core/notifications 0.0.0-stage → 0.22.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 +201 -0
- package/README.md +215 -2
- package/dist/browser/contracts.d.ts +88 -0
- package/dist/browser/contracts.js +93 -0
- package/dist/browser/contracts.js.map +1 -0
- package/dist/browser/index.d.ts +5 -0
- package/dist/browser/index.js +4 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/push.d.ts +18 -0
- package/dist/browser/push.js +114 -0
- package/dist/browser/push.js.map +1 -0
- package/dist/browser/service-worker.d.ts +5 -0
- package/dist/browser/service-worker.js +53 -0
- package/dist/browser/service-worker.js.map +1 -0
- package/dist/cjs/browser/contracts.d.ts +88 -0
- package/dist/cjs/browser/contracts.js +115 -0
- package/dist/cjs/browser/index.d.ts +5 -0
- package/dist/cjs/browser/index.js +57 -0
- package/dist/cjs/browser/push.d.ts +18 -0
- package/dist/cjs/browser/push.js +127 -0
- package/dist/cjs/browser/service-worker.d.ts +5 -0
- package/dist/cjs/browser/service-worker.js +74 -0
- package/dist/cjs/delivery.d.ts +34 -0
- package/dist/cjs/delivery.js +166 -0
- package/dist/cjs/email.d.ts +27 -0
- package/dist/cjs/email.js +86 -0
- package/dist/cjs/events.d.ts +35 -0
- package/dist/cjs/events.js +51 -0
- package/dist/cjs/index.d.ts +20 -0
- package/dist/cjs/index.js +69 -0
- package/dist/cjs/links.d.ts +3 -0
- package/dist/cjs/links.js +35 -0
- package/dist/cjs/live.d.ts +14 -0
- package/dist/cjs/live.js +34 -0
- package/dist/cjs/nest/index.d.ts +12 -0
- package/dist/cjs/nest/index.js +69 -0
- package/dist/cjs/nest/notifications.controller.d.ts +48 -0
- package/dist/cjs/nest/notifications.controller.js +264 -0
- package/dist/cjs/nest/options.d.ts +45 -0
- package/dist/cjs/nest/options.js +69 -0
- package/dist/cjs/nest/stream.controller.d.ts +24 -0
- package/dist/cjs/nest/stream.controller.js +161 -0
- package/dist/cjs/notifier.d.ts +28 -0
- package/dist/cjs/notifier.js +82 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/preferences.d.ts +9 -0
- package/dist/cjs/preferences.js +42 -0
- package/dist/cjs/prune.d.ts +23 -0
- package/dist/cjs/prune.js +61 -0
- package/dist/cjs/push/fcm.d.ts +9 -0
- package/dist/cjs/push/fcm.js +127 -0
- package/dist/cjs/push/transport.d.ts +21 -0
- package/dist/cjs/push/transport.js +15 -0
- package/dist/cjs/push/web-push.d.ts +6 -0
- package/dist/cjs/push/web-push.js +95 -0
- package/dist/cjs/repository.d.ts +88 -0
- package/dist/cjs/repository.js +241 -0
- package/dist/delivery.d.ts +34 -0
- package/dist/delivery.js +155 -0
- package/dist/delivery.js.map +1 -0
- package/dist/email.d.ts +27 -0
- package/dist/email.js +80 -0
- package/dist/email.js.map +1 -0
- package/dist/events.d.ts +35 -0
- package/dist/events.js +29 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/links.d.ts +3 -0
- package/dist/links.js +9 -0
- package/dist/links.js.map +1 -0
- package/dist/live.d.ts +14 -0
- package/dist/live.js +8 -0
- package/dist/live.js.map +1 -0
- package/dist/nest/index.d.ts +12 -0
- package/dist/nest/index.js +40 -0
- package/dist/nest/index.js.map +1 -0
- package/dist/nest/notifications.controller.d.ts +48 -0
- package/dist/nest/notifications.controller.js +251 -0
- package/dist/nest/notifications.controller.js.map +1 -0
- package/dist/nest/options.d.ts +45 -0
- package/dist/nest/options.js +35 -0
- package/dist/nest/options.js.map +1 -0
- package/dist/nest/stream.controller.d.ts +24 -0
- package/dist/nest/stream.controller.js +144 -0
- package/dist/nest/stream.controller.js.map +1 -0
- package/dist/notifier.d.ts +28 -0
- package/dist/notifier.js +60 -0
- package/dist/notifier.js.map +1 -0
- package/dist/preferences.d.ts +9 -0
- package/dist/preferences.js +17 -0
- package/dist/preferences.js.map +1 -0
- package/dist/prune.d.ts +23 -0
- package/dist/prune.js +46 -0
- package/dist/prune.js.map +1 -0
- package/dist/push/fcm.d.ts +9 -0
- package/dist/push/fcm.js +109 -0
- package/dist/push/fcm.js.map +1 -0
- package/dist/push/transport.d.ts +21 -0
- package/dist/push/transport.js +2 -0
- package/dist/push/transport.js.map +1 -0
- package/dist/push/web-push.d.ts +6 -0
- package/dist/push/web-push.js +67 -0
- package/dist/push/web-push.js.map +1 -0
- package/dist/repository.d.ts +88 -0
- package/dist/repository.js +283 -0
- package/dist/repository.js.map +1 -0
- package/package.json +107 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Haatlabs
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,216 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @haatlabs-core/notifications
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
In-app notifications for NestJS apps: an event registry, per-person and per-organization channel preferences, an inbox, a live stream, email and web push.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @haatlabs-core/notifications
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Use
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { defineNotificationEvents } from '@haatlabs-core/notifications';
|
|
15
|
+
|
|
16
|
+
export const events = defineNotificationEvents({
|
|
17
|
+
'order.created': {
|
|
18
|
+
label: 'New order',
|
|
19
|
+
group: 'Orders',
|
|
20
|
+
audiences: ['vendor'],
|
|
21
|
+
defaults: { email: true, push: true },
|
|
22
|
+
render: (data: { id: string }) => ({
|
|
23
|
+
title: 'New order',
|
|
24
|
+
body: `Order ${data.id} was placed.`,
|
|
25
|
+
link: `/orders/${data.id}`,
|
|
26
|
+
}),
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Entry points: `@haatlabs-core/notifications` (framework-free), `/nest` (NestJS module) and `/browser` (client helpers, no Node modules). The dashboard widgets (`NotificationBell`, `NotificationInbox`, `NotificationPreferences`) live in `@haatlabs-core/ui/client`.
|
|
32
|
+
|
|
33
|
+
`required: true` on an event means the person cannot turn its email off (push stays switchable). Links are app-relative paths (`/orders/1`); anything else is refused when `notify` is called. A title is at most 120 characters and a body 280.
|
|
34
|
+
|
|
35
|
+
## Notify in a transaction
|
|
36
|
+
|
|
37
|
+
`createNotifier` writes the inbox rows and the `notifications.deliver` outbox messages in the caller's transaction, so both commit or neither does. Calling it twice with the same `dedupeKey` (a webhook that fires twice) gives one row and one delivery.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { createOutboxWriter, withTenant } from '@haatlabs-core/db-core';
|
|
41
|
+
import { createNotifier } from '@haatlabs-core/notifications';
|
|
42
|
+
|
|
43
|
+
export const notifier = createNotifier({
|
|
44
|
+
tables: { notifications },
|
|
45
|
+
events,
|
|
46
|
+
outbox: createOutboxWriter({ outboxMessages }),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// On the runtime role, inside the organization's row-level security.
|
|
50
|
+
await withTenant(appDb.db, { organizationId }, async (tx) => {
|
|
51
|
+
const order = await orders.create(tx, input);
|
|
52
|
+
await notifier.notify(tx, {
|
|
53
|
+
organizationId,
|
|
54
|
+
event: 'order.created',
|
|
55
|
+
recipients: [{ type: 'vendor', id: vendorId }],
|
|
56
|
+
data: { id: order.id },
|
|
57
|
+
dedupeKey: `order:${order.id}`,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`@haatlabs-core/db-core` gives the column and index builders; the app builds the four tables (`notifications`, `notification_preferences`, `notification_defaults`, `push_subscriptions`) in its own schema, all under row-level security by organization:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { pgTable } from 'drizzle-orm/pg-core';
|
|
66
|
+
import {
|
|
67
|
+
notificationColumns,
|
|
68
|
+
notificationDefaultColumns,
|
|
69
|
+
notificationDefaultIndexes,
|
|
70
|
+
notificationIndexes,
|
|
71
|
+
notificationPreferenceColumns,
|
|
72
|
+
notificationPreferenceIndexes,
|
|
73
|
+
pushSubscriptionColumns,
|
|
74
|
+
pushSubscriptionIndexes,
|
|
75
|
+
} from '@haatlabs-core/db-core';
|
|
76
|
+
|
|
77
|
+
export const notifications = pgTable('notifications', notificationColumns({ organizations }), (t) =>
|
|
78
|
+
notificationIndexes(t),
|
|
79
|
+
);
|
|
80
|
+
export const notificationPreferences = pgTable(
|
|
81
|
+
'notification_preferences',
|
|
82
|
+
notificationPreferenceColumns({ organizations }),
|
|
83
|
+
(t) => notificationPreferenceIndexes(t),
|
|
84
|
+
);
|
|
85
|
+
export const notificationDefaults = pgTable(
|
|
86
|
+
'notification_defaults',
|
|
87
|
+
notificationDefaultColumns({ organizations }),
|
|
88
|
+
(t) => notificationDefaultIndexes(t),
|
|
89
|
+
);
|
|
90
|
+
export const pushSubscriptions = pgTable(
|
|
91
|
+
'push_subscriptions',
|
|
92
|
+
pushSubscriptionColumns({ organizations }),
|
|
93
|
+
(t) => pushSubscriptionIndexes(t),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
export const tables = {
|
|
97
|
+
notifications,
|
|
98
|
+
notificationPreferences,
|
|
99
|
+
notificationDefaults,
|
|
100
|
+
pushSubscriptions,
|
|
101
|
+
};
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## The worker queue
|
|
105
|
+
|
|
106
|
+
Consume the `notifications` queue (routing key `notifications.deliver`) with the delivery handler. For each message it sends the live event, the push to the person's devices and the email, as their preferences say. A retried job sends no second email (each channel has a sent-at mark), and a dead push subscription (Web Push 404/410, FCM `UNREGISTERED`) is deleted without failing the job.
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import {
|
|
110
|
+
SystemNotificationRepository,
|
|
111
|
+
createNotificationDeliveryHandler,
|
|
112
|
+
fcmTransport,
|
|
113
|
+
startNotificationPrune,
|
|
114
|
+
webPushTransport,
|
|
115
|
+
} from '@haatlabs-core/notifications';
|
|
116
|
+
import type { PushTransport } from '@haatlabs-core/notifications';
|
|
117
|
+
|
|
118
|
+
const repo = new SystemNotificationRepository(system.db, tables);
|
|
119
|
+
// Each kind is on only where its settings are (both groups are null when unset).
|
|
120
|
+
const push: PushTransport[] = [];
|
|
121
|
+
if (config.notifications.webPush) push.push(webPushTransport(config.notifications.webPush));
|
|
122
|
+
if (config.notifications.fcm) {
|
|
123
|
+
push.push(fcmTransport(config.notifications.fcm, { redis, keyPrefix }));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const handler = createNotificationDeliveryHandler({
|
|
127
|
+
repo,
|
|
128
|
+
events,
|
|
129
|
+
redis,
|
|
130
|
+
keyPrefix,
|
|
131
|
+
push,
|
|
132
|
+
mailer,
|
|
133
|
+
brand: async (orgId, recipient) => ({ appName, accent, baseUrl, poweredBy: false }),
|
|
134
|
+
emailOf: async (orgId, recipient) => emailAddressOf(recipient),
|
|
135
|
+
logger,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const stopPrune = startNotificationPrune({ repo, redis, keyPrefix, logger });
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`brand` gives the sender and host for each organization and recipient (`from`, `replyTo`, `appName`, `accent`, `logoUrl`, `baseUrl`, `poweredBy`): an owner and a vendor may sign in on different hosts, so links in their push and email go to their own. With no `from` the mailer's own is used. Every email carries a "Change which emails you get" link to `baseUrl + preferencesPath`; `preferencesPath` is one path or a function of the recipient (default `/settings/notifications`).
|
|
142
|
+
|
|
143
|
+
A push request (Web Push or FCM) that gets no answer in 10 s fails as transient, so the job retries push only. Web Push sends the bare app path beside the absolute URL; FCM sends the absolute URL.
|
|
144
|
+
|
|
145
|
+
Inbox rows are kept 90 days once read and 180 days in all. `startNotificationPrune` prunes daily, once across workers, under `<keyPrefix>:notifications:prune:<UTC date>`; call what it returns on shutdown.
|
|
146
|
+
|
|
147
|
+
## The routes
|
|
148
|
+
|
|
149
|
+
`NotificationsModule.forRoot` mounts these under `/api/v1/notifications`. It needs nest-auth's `AuthModule` and nest-common's `LOGGER`.
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
NotificationsModule.forRoot({
|
|
153
|
+
events,
|
|
154
|
+
repository: {
|
|
155
|
+
useFactory: (a: DatabaseHandle) => new NotificationRepository(a.db, tables),
|
|
156
|
+
inject: [APP_DB],
|
|
157
|
+
},
|
|
158
|
+
redis: { useFactory: (r: Redis) => r, inject: [REDIS] },
|
|
159
|
+
keyPrefix,
|
|
160
|
+
webPushPublicKey: config.notifications.webPush?.publicKey ?? null,
|
|
161
|
+
recipientOf, // optional: who a caller is as a recipient; null is refused (403)
|
|
162
|
+
audienceAdmins: ['OWNER'], // optional: default OWNER and ADMIN
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
| Route | What |
|
|
167
|
+
| -------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
|
|
168
|
+
| `GET /` | the inbox: `?unread=true&cursor=&limit=` (1-50, default 20) |
|
|
169
|
+
| `GET /unread-count` | the unread number |
|
|
170
|
+
| `POST /read` | `{ ids: uuid[1..100] }`; ids that are not the caller's change nothing |
|
|
171
|
+
| `POST /read-all` | mark everything read |
|
|
172
|
+
| `GET /stream` | server-sent events: `unread` and `notification`, `: ping` every 25 s, closed after 30 minutes |
|
|
173
|
+
| `GET /preferences`, `PUT /preferences` | the person's choices per event and channel |
|
|
174
|
+
| `GET /defaults/:audience`, `PUT /defaults/:audience` | the organization's defaults for an audience; `audienceAdmins` only |
|
|
175
|
+
| `GET /push/key` | the public VAPID key (or null) |
|
|
176
|
+
| `POST /push/subscriptions`, `DELETE /push/subscriptions` | register or remove a device (10 per person, newest kept) |
|
|
177
|
+
|
|
178
|
+
Defaults resolve in this order: the person's own choice, the organization's default for the audience, the event's `defaults`.
|
|
179
|
+
|
|
180
|
+
A Web Push subscription is accepted only on a browser push service, over https and never at an IP address: `*.googleapis.com`, `*.push.services.mozilla.com`, `*.notify.windows.com` and `*.push.apple.com`.
|
|
181
|
+
|
|
182
|
+
On shutdown the API ends every open stream (each browser reconnects, to another instance), so closing does not wait for them. A stream checks who the caller is when it opens, not after: one opened before sign-out (or before a role or membership changed) stays open until its 30 minutes are up.
|
|
183
|
+
|
|
184
|
+
## Web Push and FCM setup
|
|
185
|
+
|
|
186
|
+
Each group of settings is all or nothing: a half-set group fails config load naming the missing variables. A group left unset turns that transport off, and push of that kind is skipped without an error.
|
|
187
|
+
|
|
188
|
+
- Web Push: `VAPID_PUBLIC_KEY`, `VAPID_PRIVATE_KEY`, `VAPID_SUBJECT` (`mailto:` or `https:`). Make the keys with `npx web-push generate-vapid-keys`.
|
|
189
|
+
- FCM (for native apps): `FCM_PROJECT_ID`, `FCM_CLIENT_EMAIL`, `FCM_PRIVATE_KEY` from a Firebase service account with the "Firebase Cloud Messaging API Admin" role. No `firebase-admin` is needed.
|
|
190
|
+
|
|
191
|
+
## The service worker
|
|
192
|
+
|
|
193
|
+
Web Push needs a service worker. Serve `PUSH_SERVICE_WORKER_SCRIPT` from `@haatlabs-core/notifications/browser` as-is at `/notifications-sw.js` on the dashboard's origin, as JavaScript at the site root:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
import { PUSH_SERVICE_WORKER_SCRIPT } from '@haatlabs-core/notifications/browser';
|
|
197
|
+
// GET /notifications-sw.js -> Content-Type: text/javascript, body PUSH_SERVICE_WORKER_SCRIPT
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
It shows the push's bare path when there is one (the absolute URL may be on another of the app's hosts), else an absolute URL on its own origin; anything else opens `/`. The page side is `pushSupport()`, `subscribeToPush()`, `pushSubscribed()` and `unsubscribeFromPush()` from the same entry. Call `unsubscribeFromPush` on sign-out (ignoring a failure), so a shared device stops getting the person's notifications; the starter does.
|
|
201
|
+
|
|
202
|
+
## The embedded Shopify frame gets no push
|
|
203
|
+
|
|
204
|
+
Inside Shopify's admin iframe there is no service worker and no permission prompt, so the bell shows the inbox and live updates only, and the preferences page does not offer push. The stream is read with `fetch` rather than `EventSource`, so bearer auth works in the frame; after repeated stream failures it also polls the unread count every 60 s. Embedded sessions have no user, so the starter maps them to a `store` recipient and sets `audienceAdmins: ['OWNER']`. The embedded inbox therefore shows only events whose `audiences` include `store`: an app that wants one there defines such events and notifies the `store` recipient.
|
|
205
|
+
|
|
206
|
+
## Adopting it in an existing app
|
|
207
|
+
|
|
208
|
+
What the starter (`@haatlabs-core/templates`) does, and what catalogpix, catalogscout, marginledger, the console and VendorHaat copy:
|
|
209
|
+
|
|
210
|
+
1. Add the schema file with the four tables (as above) to the app's database package, run `drizzle-kit generate`, then append the GRANT and row-level security block from the starter's `0009_notifications.sql` to the generated migration (do not copy the file: the app's migration numbers and snapshot are its own).
|
|
211
|
+
2. Define the app's events in a contracts file loaded by the API and the worker, never by a page, with one shared `keyPrefix`.
|
|
212
|
+
3. Create the notifier with `createNotifier` and call `notify` in the transaction of each change worth telling of.
|
|
213
|
+
4. Add `NotificationsModule.forRoot` to the API.
|
|
214
|
+
5. Add the `notifications` queue and the delivery handler to the worker, plus `startNotificationPrune`.
|
|
215
|
+
6. Set the VAPID (and FCM) variables where push should work; serve `/notifications-sw.js`.
|
|
216
|
+
7. Give the web API client a `notifications: NotificationsClient` (the type is in `@haatlabs-core/ui/client`; the starter's `packages/web-ui/src/api-client.ts` implements it against `/api/v1/notifications`, with the stream read through `fetch`) and put `NotificationBell` in the app shell, with a page for `NotificationInbox` and one for `NotificationPreferences`, all from `@haatlabs-core/ui/client`.
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const ChannelSchema: z.ZodEnum<{
|
|
3
|
+
email: "email";
|
|
4
|
+
push: "push";
|
|
5
|
+
}>;
|
|
6
|
+
/** One inbox item. */
|
|
7
|
+
export declare const NotificationItemSchema: z.ZodObject<{
|
|
8
|
+
id: z.ZodUUID;
|
|
9
|
+
event: z.ZodString;
|
|
10
|
+
title: z.ZodString;
|
|
11
|
+
body: z.ZodString;
|
|
12
|
+
link: z.ZodNullable<z.ZodString>;
|
|
13
|
+
readAt: z.ZodNullable<z.ZodString>;
|
|
14
|
+
createdAt: z.ZodString;
|
|
15
|
+
}, z.core.$strip>;
|
|
16
|
+
export type NotificationItemBody = z.infer<typeof NotificationItemSchema>;
|
|
17
|
+
/** `GET /notifications`: newest first; `nextCursor` is null on the last page. */
|
|
18
|
+
export declare const NotificationListSchema: z.ZodObject<{
|
|
19
|
+
items: z.ZodArray<z.ZodObject<{
|
|
20
|
+
id: z.ZodUUID;
|
|
21
|
+
event: z.ZodString;
|
|
22
|
+
title: z.ZodString;
|
|
23
|
+
body: z.ZodString;
|
|
24
|
+
link: z.ZodNullable<z.ZodString>;
|
|
25
|
+
readAt: z.ZodNullable<z.ZodString>;
|
|
26
|
+
createdAt: z.ZodString;
|
|
27
|
+
}, z.core.$strip>>;
|
|
28
|
+
nextCursor: z.ZodNullable<z.ZodString>;
|
|
29
|
+
}, z.core.$strip>;
|
|
30
|
+
/** How many of the caller's items are unread. */
|
|
31
|
+
export declare const UnreadSchema: z.ZodObject<{
|
|
32
|
+
unread: z.ZodNumber;
|
|
33
|
+
}, z.core.$strip>;
|
|
34
|
+
/** `POST /notifications/read`. */
|
|
35
|
+
export declare const MarkReadBodySchema: z.ZodObject<{
|
|
36
|
+
ids: z.ZodArray<z.ZodUUID>;
|
|
37
|
+
}, z.core.$strip>;
|
|
38
|
+
/** `GET` and `PUT /notifications/preferences`, and the organization's defaults per audience. */
|
|
39
|
+
export declare const PreferencesSchema: z.ZodObject<{
|
|
40
|
+
events: z.ZodArray<z.ZodObject<{
|
|
41
|
+
event: z.ZodString;
|
|
42
|
+
label: z.ZodString;
|
|
43
|
+
group: z.ZodString;
|
|
44
|
+
required: z.ZodBoolean;
|
|
45
|
+
channels: z.ZodObject<{
|
|
46
|
+
email: z.ZodObject<{
|
|
47
|
+
on: z.ZodBoolean;
|
|
48
|
+
locked: z.ZodBoolean;
|
|
49
|
+
}, z.core.$strip>;
|
|
50
|
+
push: z.ZodObject<{
|
|
51
|
+
on: z.ZodBoolean;
|
|
52
|
+
locked: z.ZodBoolean;
|
|
53
|
+
}, z.core.$strip>;
|
|
54
|
+
}, z.core.$strip>;
|
|
55
|
+
}, z.core.$strip>>;
|
|
56
|
+
}, z.core.$strip>;
|
|
57
|
+
/** The body of `PUT /notifications/preferences` and `PUT /notifications/defaults/:audience`. */
|
|
58
|
+
export declare const PreferenceChangesSchema: z.ZodObject<{
|
|
59
|
+
changes: z.ZodArray<z.ZodObject<{
|
|
60
|
+
event: z.ZodString;
|
|
61
|
+
channel: z.ZodEnum<{
|
|
62
|
+
email: "email";
|
|
63
|
+
push: "push";
|
|
64
|
+
}>;
|
|
65
|
+
on: z.ZodBoolean;
|
|
66
|
+
}, z.core.$strip>>;
|
|
67
|
+
}, z.core.$strip>;
|
|
68
|
+
/** `GET /notifications/push/key`: the Web Push public key, or null where push is not set up. */
|
|
69
|
+
export declare const PushKeySchema: z.ZodObject<{
|
|
70
|
+
webPush: z.ZodNullable<z.ZodString>;
|
|
71
|
+
}, z.core.$strip>;
|
|
72
|
+
/** `POST /notifications/push/subscriptions`: a browser's Web Push subscription, or an FCM token. */
|
|
73
|
+
export declare const PushSubscriptionBodySchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
74
|
+
kind: z.ZodLiteral<"webpush">;
|
|
75
|
+
endpoint: z.ZodURL;
|
|
76
|
+
keys: z.ZodObject<{
|
|
77
|
+
p256dh: z.ZodString;
|
|
78
|
+
auth: z.ZodString;
|
|
79
|
+
}, z.core.$strip>;
|
|
80
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
81
|
+
kind: z.ZodLiteral<"fcm">;
|
|
82
|
+
token: z.ZodString;
|
|
83
|
+
}, z.core.$strip>], "kind">;
|
|
84
|
+
export type PushSubscriptionBody = z.infer<typeof PushSubscriptionBodySchema>;
|
|
85
|
+
/** `DELETE /notifications/push/subscriptions`: the Web Push endpoint, or the FCM token. */
|
|
86
|
+
export declare const PushUnsubscribeBodySchema: z.ZodObject<{
|
|
87
|
+
endpoint: z.ZodString;
|
|
88
|
+
}, z.core.$strip>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
// What the notification routes and the app's pages agree on. Pages load this too, so nothing
|
|
3
|
+
// here loads a Node built-in (scripts/check-browser.mjs).
|
|
4
|
+
const MAX_IDS = 100;
|
|
5
|
+
const MAX_CHANGES = 100;
|
|
6
|
+
const MAX_KEY = 1024;
|
|
7
|
+
const MAX_ENDPOINT = 2048;
|
|
8
|
+
const MAX_TOKEN = 4096;
|
|
9
|
+
export const ChannelSchema = z.enum(['email', 'push']);
|
|
10
|
+
/** One inbox item. */
|
|
11
|
+
export const NotificationItemSchema = z.object({
|
|
12
|
+
id: z.uuid(),
|
|
13
|
+
event: z.string(),
|
|
14
|
+
title: z.string(),
|
|
15
|
+
body: z.string(),
|
|
16
|
+
link: z.string().nullable(),
|
|
17
|
+
readAt: z.string().nullable(),
|
|
18
|
+
createdAt: z.string(),
|
|
19
|
+
});
|
|
20
|
+
/** `GET /notifications`: newest first; `nextCursor` is null on the last page. */
|
|
21
|
+
export const NotificationListSchema = z.object({
|
|
22
|
+
items: z.array(NotificationItemSchema),
|
|
23
|
+
nextCursor: z.string().nullable(),
|
|
24
|
+
});
|
|
25
|
+
/** How many of the caller's items are unread. */
|
|
26
|
+
export const UnreadSchema = z.object({ unread: z.number().int().min(0) });
|
|
27
|
+
/** `POST /notifications/read`. */
|
|
28
|
+
export const MarkReadBodySchema = z.object({ ids: z.array(z.uuid()).min(1).max(MAX_IDS) });
|
|
29
|
+
const ChannelStateSchema = z.object({ on: z.boolean(), locked: z.boolean() });
|
|
30
|
+
/** `GET` and `PUT /notifications/preferences`, and the organization's defaults per audience. */
|
|
31
|
+
export const PreferencesSchema = z.object({
|
|
32
|
+
events: z.array(z.object({
|
|
33
|
+
event: z.string(),
|
|
34
|
+
label: z.string(),
|
|
35
|
+
group: z.string(),
|
|
36
|
+
required: z.boolean(),
|
|
37
|
+
channels: z.object({ email: ChannelStateSchema, push: ChannelStateSchema }),
|
|
38
|
+
})),
|
|
39
|
+
});
|
|
40
|
+
/** The body of `PUT /notifications/preferences` and `PUT /notifications/defaults/:audience`. */
|
|
41
|
+
export const PreferenceChangesSchema = z.object({
|
|
42
|
+
changes: z
|
|
43
|
+
.array(z.object({ event: z.string().min(1).max(64), channel: ChannelSchema, on: z.boolean() }))
|
|
44
|
+
.min(1)
|
|
45
|
+
.max(MAX_CHANGES),
|
|
46
|
+
});
|
|
47
|
+
/** `GET /notifications/push/key`: the Web Push public key, or null where push is not set up. */
|
|
48
|
+
export const PushKeySchema = z.object({ webPush: z.string().nullable() });
|
|
49
|
+
/** The browsers' push services; a subscription anywhere else is refused (no server-side fetch to
|
|
50
|
+
* an address the caller picked). A new browser's service would need adding here. */
|
|
51
|
+
const PUSH_SERVICE_HOSTS = [
|
|
52
|
+
/^[a-z0-9-]+(\.[a-z0-9-]+)*\.googleapis\.com$/,
|
|
53
|
+
/^[a-z0-9-]+(\.[a-z0-9-]+)*\.push\.services\.mozilla\.com$/,
|
|
54
|
+
/^[a-z0-9-]+(\.[a-z0-9-]+)*\.notify\.windows\.com$/,
|
|
55
|
+
/^[a-z0-9-]+(\.[a-z0-9-]+)*\.push\.apple\.com$/,
|
|
56
|
+
];
|
|
57
|
+
const IP_LITERAL = /^(\[.*\]|\d{1,3}(\.\d{1,3}){3})$/;
|
|
58
|
+
/** An https URL on a known push service's host, never an IP literal. */
|
|
59
|
+
function isPushServiceEndpoint(endpoint) {
|
|
60
|
+
let url;
|
|
61
|
+
try {
|
|
62
|
+
url = new URL(endpoint);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
if (url.protocol !== 'https:' || url.username !== '' || url.password !== '')
|
|
68
|
+
return false;
|
|
69
|
+
const host = url.hostname.toLowerCase();
|
|
70
|
+
if (IP_LITERAL.test(host))
|
|
71
|
+
return false;
|
|
72
|
+
return PUSH_SERVICE_HOSTS.some((pattern) => pattern.test(host));
|
|
73
|
+
}
|
|
74
|
+
/** `POST /notifications/push/subscriptions`: a browser's Web Push subscription, or an FCM token. */
|
|
75
|
+
export const PushSubscriptionBodySchema = z.discriminatedUnion('kind', [
|
|
76
|
+
z.object({
|
|
77
|
+
kind: z.literal('webpush'),
|
|
78
|
+
endpoint: z
|
|
79
|
+
.url({ protocol: /^https$/ })
|
|
80
|
+
.max(MAX_ENDPOINT)
|
|
81
|
+
.refine(isPushServiceEndpoint, 'Not a known push service endpoint'),
|
|
82
|
+
keys: z.object({
|
|
83
|
+
p256dh: z.string().min(1).max(MAX_KEY),
|
|
84
|
+
auth: z.string().min(1).max(MAX_KEY),
|
|
85
|
+
}),
|
|
86
|
+
}),
|
|
87
|
+
z.object({ kind: z.literal('fcm'), token: z.string().min(1).max(MAX_TOKEN) }),
|
|
88
|
+
]);
|
|
89
|
+
/** `DELETE /notifications/push/subscriptions`: the Web Push endpoint, or the FCM token. */
|
|
90
|
+
export const PushUnsubscribeBodySchema = z.object({
|
|
91
|
+
endpoint: z.string().min(1).max(MAX_TOKEN),
|
|
92
|
+
});
|
|
93
|
+
//# sourceMappingURL=contracts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contracts.js","sourceRoot":"","sources":["../../src/browser/contracts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,6FAA6F;AAC7F,0DAA0D;AAE1D,MAAM,OA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AAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC;YACtC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC;SACrC,CAAC;KACH,CAAC;IACF,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;CAC9E,CAAC,CAAC;AAGH,2FAA2F;AAC3F,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,CAAC,MAAM,CAAC;IAChD,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC;CAC3C,CAAC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ChannelSchema, MarkReadBodySchema, NotificationItemSchema, NotificationListSchema, PreferenceChangesSchema, PreferencesSchema, PushKeySchema, PushSubscriptionBodySchema, PushUnsubscribeBodySchema, UnreadSchema, } from './contracts.js';
|
|
2
|
+
export type { NotificationItemBody, PushSubscriptionBody } from './contracts.js';
|
|
3
|
+
export { pushSubscribed, pushSupport, subscribeToPush, unsubscribeFromPush } from './push.js';
|
|
4
|
+
export type { PushSupport, SubscribeResult } from './push.js';
|
|
5
|
+
export { PUSH_SERVICE_WORKER_SCRIPT } from './service-worker.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ChannelSchema, MarkReadBodySchema, NotificationItemSchema, NotificationListSchema, PreferenceChangesSchema, PreferencesSchema, PushKeySchema, PushSubscriptionBodySchema, PushUnsubscribeBodySchema, UnreadSchema, } from './contracts.js';
|
|
2
|
+
export { pushSubscribed, pushSupport, subscribeToPush, unsubscribeFromPush } from './push.js';
|
|
3
|
+
export { PUSH_SERVICE_WORKER_SCRIPT } from './service-worker.js';
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/browser/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,sBAAsB,EACtB,sBAAsB,EACtB,uBAAuB,EACvB,iBAAiB,EACjB,aAAa,EACb,0BAA0B,EAC1B,yBAAyB,EACzB,YAAY,GACb,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAE9F,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PushSubscriptionBody } from './contracts.js';
|
|
2
|
+
export type PushSupport = 'supported' | 'embedded' | 'unsupported' | 'needs-install';
|
|
3
|
+
export type SubscribeResult = 'subscribed' | 'denied' | 'embedded' | 'unsupported' | 'needs-install';
|
|
4
|
+
/** Whether this page can ask for push permission, and if not, why. */
|
|
5
|
+
export declare function pushSupport(): PushSupport;
|
|
6
|
+
/**
|
|
7
|
+
* Registers the service worker, asks for permission and hands the subscription to `save`.
|
|
8
|
+
* Call it from a click: browsers refuse a permission prompt that no one asked for.
|
|
9
|
+
*/
|
|
10
|
+
export declare function subscribeToPush(opts: {
|
|
11
|
+
vapidPublicKey: string;
|
|
12
|
+
serviceWorkerUrl?: string;
|
|
13
|
+
save(body: PushSubscriptionBody): Promise<void>;
|
|
14
|
+
}): Promise<SubscribeResult>;
|
|
15
|
+
/** Whether this browser already gets push here (the preferences offer to turn it off). */
|
|
16
|
+
export declare function pushSubscribed(): Promise<boolean>;
|
|
17
|
+
/** Drops this browser's subscription and tells the server which endpoint to forget. */
|
|
18
|
+
export declare function unsubscribeFromPush(remove: (endpoint: string) => Promise<void>): Promise<void>;
|