@haatlabs-core/notifications 0.27.2 → 0.29.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 +1 -1
- package/dist/cjs/delivery.d.ts +3 -4
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/nest/context.d.ts +2 -0
- package/dist/cjs/nest/context.js +28 -0
- package/dist/cjs/nest/index.d.ts +4 -3
- package/dist/cjs/nest/index.js +2 -1
- package/dist/cjs/nest/notifications.controller.d.ts +15 -13
- package/dist/cjs/nest/notifications.controller.js +70 -55
- package/dist/cjs/nest/options.d.ts +35 -5
- package/dist/cjs/nest/options.js +21 -5
- package/dist/cjs/nest/stream.controller.d.ts +5 -3
- package/dist/cjs/nest/stream.controller.js +15 -10
- package/dist/cjs/nest-auth/index.d.ts +8 -0
- package/dist/cjs/nest-auth/index.js +38 -0
- package/dist/cjs/ports.d.ts +21 -0
- package/dist/cjs/ports.js +15 -0
- package/dist/delivery.d.ts +3 -4
- package/dist/delivery.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js.map +1 -1
- package/dist/nest/context.d.ts +2 -0
- package/dist/nest/context.js +4 -0
- package/dist/nest/context.js.map +1 -0
- package/dist/nest/index.d.ts +4 -3
- package/dist/nest/index.js +5 -3
- package/dist/nest/index.js.map +1 -1
- package/dist/nest/notifications.controller.d.ts +15 -13
- package/dist/nest/notifications.controller.js +44 -29
- package/dist/nest/notifications.controller.js.map +1 -1
- package/dist/nest/options.d.ts +35 -5
- package/dist/nest/options.js +15 -3
- package/dist/nest/options.js.map +1 -1
- package/dist/nest/stream.controller.d.ts +5 -3
- package/dist/nest/stream.controller.js +13 -8
- package/dist/nest/stream.controller.js.map +1 -1
- package/dist/nest-auth/index.d.ts +8 -0
- package/dist/nest-auth/index.js +21 -0
- package/dist/nest-auth/index.js.map +1 -0
- package/dist/ports.d.ts +21 -0
- package/dist/ports.js +4 -0
- package/dist/ports.js.map +1 -0
- package/package.json +28 -12
package/README.md
CHANGED
|
@@ -205,7 +205,7 @@ Inside Shopify's admin iframe there is no service worker and no permission promp
|
|
|
205
205
|
|
|
206
206
|
## Adopting it in an existing app
|
|
207
207
|
|
|
208
|
-
What the starter (`@haatlabs-core/templates`) does, and what
|
|
208
|
+
What the starter (`@haatlabs-core/templates`) does, and what each app built from it copies:
|
|
209
209
|
|
|
210
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
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`.
|
package/dist/cjs/delivery.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Logger } from '@haatlabs-core/logger';
|
|
2
|
-
import type {
|
|
3
|
-
import type { ConsumedMessage } from '@haatlabs-core/queue';
|
|
2
|
+
import type { DeliveryMessage, MailSender } from './ports.js';
|
|
4
3
|
import type { Redis } from 'ioredis';
|
|
5
4
|
import type { EmailBrand } from './email.js';
|
|
6
5
|
import type { NotificationEventDef, NotificationEvents, Recipient } from './events.js';
|
|
@@ -13,7 +12,7 @@ export interface NotificationDeliveryDeps<E extends Record<string, NotificationE
|
|
|
13
12
|
keyPrefix: string;
|
|
14
13
|
/** The configured transports; a device of a kind with none is skipped. */
|
|
15
14
|
push: PushTransport[];
|
|
16
|
-
mailer:
|
|
15
|
+
mailer: MailSender;
|
|
17
16
|
/** The sender and host for this recipient: an owner and a vendor may use different hosts. */
|
|
18
17
|
brand(orgId: string, r: Recipient): Promise<EmailBrand>;
|
|
19
18
|
/** The recipient's email address; null: no email. */
|
|
@@ -31,4 +30,4 @@ export interface NotificationDeliveryDeps<E extends Record<string, NotificationE
|
|
|
31
30
|
* failure fails the job after every device and the email were tried, so the retry sends
|
|
32
31
|
* push only.
|
|
33
32
|
*/
|
|
34
|
-
export declare function createNotificationDeliveryHandler<E extends Record<string, NotificationEventDef<any>>>(deps: NotificationDeliveryDeps<E>): (message:
|
|
33
|
+
export declare function createNotificationDeliveryHandler<E extends Record<string, NotificationEventDef<any>>>(deps: NotificationDeliveryDeps<E>): (message: DeliveryMessage) => Promise<void>;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export { webPushTransport } from './push/web-push.js';
|
|
|
15
15
|
export { fcmTransport } from './push/fcm.js';
|
|
16
16
|
export type { PushMessage, PushResult, PushTransport } from './push/transport.js';
|
|
17
17
|
export { createNotificationDeliveryHandler } from './delivery.js';
|
|
18
|
+
export type { DeliveryMessage, MailMessage, MailSender } from './ports.js';
|
|
18
19
|
export type { NotificationDeliveryDeps } from './delivery.js';
|
|
19
20
|
export { liveChannel, publishLive } from './live.js';
|
|
20
21
|
export type { LiveEvent } from './live.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var context_exports = {};
|
|
19
|
+
__export(context_exports, {
|
|
20
|
+
CurrentContext: () => CurrentContext
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(context_exports);
|
|
23
|
+
var import_common = require("@nestjs/common");
|
|
24
|
+
const CurrentContext = (0, import_common.createParamDecorator)((_data, context) => context);
|
|
25
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
26
|
+
0 && (module.exports = {
|
|
27
|
+
CurrentContext
|
|
28
|
+
});
|
package/dist/cjs/nest/index.d.ts
CHANGED
|
@@ -2,11 +2,12 @@ import type { DynamicModule } from '@nestjs/common';
|
|
|
2
2
|
import type { AnyEvents, NotificationsModuleOptions } from './options.js';
|
|
3
3
|
/**
|
|
4
4
|
* The notification routes under `api/v1/notifications`: the inbox, preferences, the
|
|
5
|
-
* organization's defaults, push devices and the live stream.
|
|
6
|
-
* for
|
|
5
|
+
* organization's defaults, push devices and the live stream. Who is asking comes from the
|
|
6
|
+
* `auth` adapter (`@haatlabs-core/notifications/nest-auth` for the platform's sign-in); nest-common's
|
|
7
|
+
* `LOGGER` is needed.
|
|
7
8
|
*/
|
|
8
9
|
export declare class NotificationsModule {
|
|
9
10
|
static forRoot<E extends AnyEvents>(options: NotificationsModuleOptions<E>): DynamicModule;
|
|
10
11
|
}
|
|
11
12
|
export { DEFAULT_STREAM_LIFETIME_MS, DEFAULT_STREAM_PING_MS, NOTIFICATIONS_OPTIONS, NOTIFICATIONS_REDIS, NOTIFICATION_REPOSITORY, } from './options.js';
|
|
12
|
-
export type { NotificationsModuleOptions, ResolvedNotificationsOptions } from './options.js';
|
|
13
|
+
export type { NotificationCaller, NotificationsAuth, NotificationsModuleOptions, Provide, ResolvedNotificationsOptions, } from './options.js';
|
package/dist/cjs/nest/index.js
CHANGED
|
@@ -49,7 +49,8 @@ let NotificationsModule = NotificationsModule_1 = class NotificationsModule2 {
|
|
|
49
49
|
providers: [
|
|
50
50
|
{ provide: import_options.NOTIFICATIONS_OPTIONS, useValue: (0, import_options.resolveOptions)(options) },
|
|
51
51
|
provide(import_options.NOTIFICATION_REPOSITORY, options.repository),
|
|
52
|
-
provide(import_options.NOTIFICATIONS_REDIS, options.redis)
|
|
52
|
+
provide(import_options.NOTIFICATIONS_REDIS, options.redis),
|
|
53
|
+
provide(import_options.NOTIFICATIONS_AUTH, options.auth)
|
|
53
54
|
],
|
|
54
55
|
controllers: [import_notifications_controller.NotificationsController, import_stream_controller.NotificationStreamController]
|
|
55
56
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { ExecutionContext } from '@nestjs/common';
|
|
1
2
|
import type { Logger } from '@haatlabs-core/logger';
|
|
2
|
-
import type { Principal } from '@haatlabs-core/nest-auth';
|
|
3
3
|
import type { Redis } from 'ioredis';
|
|
4
4
|
import { z } from 'zod';
|
|
5
|
+
import type { NotificationsAuth } from './options.js';
|
|
5
6
|
import { MarkReadBodySchema, PreferenceChangesSchema, PushSubscriptionBodySchema, PushUnsubscribeBodySchema } from '../browser/contracts.js';
|
|
6
7
|
import type { NotificationListSchema, PreferencesSchema, PushKeySchema, UnreadSchema } from '../browser/contracts.js';
|
|
7
8
|
import type { NotificationRepository } from '../repository.js';
|
|
@@ -18,23 +19,24 @@ declare const ListQuerySchema: z.ZodObject<{
|
|
|
18
19
|
}, z.core.$strip>;
|
|
19
20
|
/** The caller's inbox, preferences and devices, and the organization's defaults. */
|
|
20
21
|
export declare class NotificationsController {
|
|
22
|
+
private readonly auth;
|
|
21
23
|
private readonly options;
|
|
22
24
|
private readonly repo;
|
|
23
25
|
private readonly redis;
|
|
24
26
|
private readonly logger;
|
|
25
27
|
private readonly events;
|
|
26
|
-
constructor(options: ResolvedNotificationsOptions, repo: NotificationRepository, redis: Redis, logger: Pick<Logger, 'warn'>);
|
|
27
|
-
list(
|
|
28
|
-
unreadCount(
|
|
29
|
-
read(
|
|
30
|
-
readAll(
|
|
31
|
-
preferences(
|
|
32
|
-
setPreferences(
|
|
33
|
-
defaults(
|
|
34
|
-
setDefaults(
|
|
35
|
-
pushKey(
|
|
36
|
-
subscribe(
|
|
37
|
-
unsubscribe(
|
|
28
|
+
constructor(auth: NotificationsAuth, options: ResolvedNotificationsOptions, repo: NotificationRepository, redis: Redis, logger: Pick<Logger, 'warn'>);
|
|
29
|
+
list(context: ExecutionContext, query: z.infer<typeof ListQuerySchema>): Promise<z.infer<typeof NotificationListSchema>>;
|
|
30
|
+
unreadCount(context: ExecutionContext): Promise<Unread>;
|
|
31
|
+
read(context: ExecutionContext, body: z.infer<typeof MarkReadBodySchema>): Promise<Unread>;
|
|
32
|
+
readAll(context: ExecutionContext): Promise<Unread>;
|
|
33
|
+
preferences(context: ExecutionContext): Promise<Preferences>;
|
|
34
|
+
setPreferences(context: ExecutionContext, body: z.infer<typeof PreferenceChangesSchema>): Promise<Preferences>;
|
|
35
|
+
defaults(context: ExecutionContext, audience: string): Promise<Preferences>;
|
|
36
|
+
setDefaults(context: ExecutionContext, audience: string, body: z.infer<typeof PreferenceChangesSchema>): Promise<Preferences>;
|
|
37
|
+
pushKey(context: ExecutionContext): z.infer<typeof PushKeySchema>;
|
|
38
|
+
subscribe(context: ExecutionContext, body: z.infer<typeof PushSubscriptionBodySchema>, userAgent?: string): Promise<void>;
|
|
39
|
+
unsubscribe(context: ExecutionContext, body: z.infer<typeof PushUnsubscribeBodySchema>): Promise<void>;
|
|
38
40
|
private preferencesFor;
|
|
39
41
|
private defaultsFor;
|
|
40
42
|
/** Only the roles the app names set defaults, and only for an audience some event has. */
|
|
@@ -22,13 +22,14 @@ __export(notifications_controller_exports, {
|
|
|
22
22
|
module.exports = __toCommonJS(notifications_controller_exports);
|
|
23
23
|
var import_common = require("@nestjs/common");
|
|
24
24
|
var import_errors = require("@haatlabs-core/errors");
|
|
25
|
-
var import_nest_auth = require("@haatlabs-core/nest-auth");
|
|
26
25
|
var import_nest_common = require("@haatlabs-core/nest-common");
|
|
27
26
|
var import_zod = require("zod");
|
|
27
|
+
var import_context = require("./context.js");
|
|
28
|
+
var import_options = require("./options.js");
|
|
28
29
|
var import_contracts = require("../browser/contracts.js");
|
|
29
30
|
var import_live = require("../live.js");
|
|
30
31
|
var import_preferences = require("../preferences.js");
|
|
31
|
-
var
|
|
32
|
+
var import_options2 = require("./options.js");
|
|
32
33
|
var __decorate = function(decorators, target, key, desc) {
|
|
33
34
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
34
35
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -85,76 +86,89 @@ function checkChanges(events, audience, changes) {
|
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
let NotificationsController = class NotificationsController2 {
|
|
89
|
+
auth;
|
|
88
90
|
options;
|
|
89
91
|
repo;
|
|
90
92
|
redis;
|
|
91
93
|
logger;
|
|
92
94
|
events;
|
|
93
|
-
constructor(options, repo, redis, logger) {
|
|
95
|
+
constructor(auth, options, repo, redis, logger) {
|
|
96
|
+
this.auth = auth;
|
|
94
97
|
this.options = options;
|
|
95
98
|
this.repo = repo;
|
|
96
99
|
this.redis = redis;
|
|
97
100
|
this.logger = logger;
|
|
98
101
|
this.events = options.events;
|
|
99
102
|
}
|
|
100
|
-
async list(
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
+
async list(context, query) {
|
|
104
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
105
|
+
const recipient = (0, import_options2.recipientFor)(this.options, principal);
|
|
106
|
+
return this.repo.list((0, import_options.tenantOf)(principal), recipient, {
|
|
103
107
|
unread: query.unread === "true",
|
|
104
108
|
...query.cursor ? { cursor: query.cursor } : {},
|
|
105
109
|
limit: query.limit
|
|
106
110
|
});
|
|
107
111
|
}
|
|
108
|
-
async unreadCount(
|
|
109
|
-
const
|
|
110
|
-
|
|
112
|
+
async unreadCount(context) {
|
|
113
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
114
|
+
const recipient = (0, import_options2.recipientFor)(this.options, principal);
|
|
115
|
+
return { unread: await this.repo.unreadCount((0, import_options.tenantOf)(principal), recipient) };
|
|
111
116
|
}
|
|
112
|
-
async read(
|
|
113
|
-
const
|
|
114
|
-
const
|
|
117
|
+
async read(context, body) {
|
|
118
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
119
|
+
const recipient = (0, import_options2.recipientFor)(this.options, principal);
|
|
120
|
+
const unread = await this.repo.markRead((0, import_options.tenantOf)(principal), recipient, body.ids);
|
|
115
121
|
await this.announce(principal, recipient, unread);
|
|
116
122
|
return { unread };
|
|
117
123
|
}
|
|
118
|
-
async readAll(
|
|
119
|
-
const
|
|
120
|
-
const
|
|
124
|
+
async readAll(context) {
|
|
125
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
126
|
+
const recipient = (0, import_options2.recipientFor)(this.options, principal);
|
|
127
|
+
const unread = await this.repo.markAllRead((0, import_options.tenantOf)(principal), recipient);
|
|
121
128
|
await this.announce(principal, recipient, unread);
|
|
122
129
|
return { unread };
|
|
123
130
|
}
|
|
124
|
-
async preferences(
|
|
125
|
-
|
|
131
|
+
async preferences(context) {
|
|
132
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
133
|
+
return this.preferencesFor(principal, (0, import_options2.recipientFor)(this.options, principal));
|
|
126
134
|
}
|
|
127
|
-
async setPreferences(
|
|
128
|
-
const
|
|
135
|
+
async setPreferences(context, body) {
|
|
136
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
137
|
+
const recipient = (0, import_options2.recipientFor)(this.options, principal);
|
|
129
138
|
checkChanges(this.events, recipient.type, body.changes);
|
|
130
|
-
await this.repo.setPreferences((0,
|
|
139
|
+
await this.repo.setPreferences((0, import_options.tenantOf)(principal), recipient, body.changes);
|
|
131
140
|
return this.preferencesFor(principal, recipient);
|
|
132
141
|
}
|
|
133
|
-
async defaults(
|
|
142
|
+
async defaults(context, audience) {
|
|
143
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
134
144
|
this.checkAudienceAdmin(principal, audience);
|
|
135
145
|
return this.defaultsFor(principal, audience);
|
|
136
146
|
}
|
|
137
|
-
async setDefaults(
|
|
147
|
+
async setDefaults(context, audience, body) {
|
|
148
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
138
149
|
this.checkAudienceAdmin(principal, audience);
|
|
139
150
|
checkChanges(this.events, audience, body.changes);
|
|
140
|
-
await this.repo.setDefaults((0,
|
|
151
|
+
await this.repo.setDefaults((0, import_options.tenantOf)(principal), audience, body.changes);
|
|
141
152
|
return this.defaultsFor(principal, audience);
|
|
142
153
|
}
|
|
143
|
-
pushKey(
|
|
144
|
-
(0, import_options.
|
|
154
|
+
pushKey(context) {
|
|
155
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
156
|
+
(0, import_options2.recipientFor)(this.options, principal);
|
|
145
157
|
return { webPush: this.options.webPushPublicKey };
|
|
146
158
|
}
|
|
147
|
-
async subscribe(
|
|
148
|
-
const
|
|
159
|
+
async subscribe(context, body, userAgent) {
|
|
160
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
161
|
+
const recipient = (0, import_options2.recipientFor)(this.options, principal);
|
|
149
162
|
const device = userAgent ? { userAgent: userAgent.slice(0, MAX_USER_AGENT) } : {};
|
|
150
|
-
await this.repo.saveSubscription((0,
|
|
163
|
+
await this.repo.saveSubscription((0, import_options.tenantOf)(principal), recipient, body.kind === "webpush" ? { kind: "webpush", endpoint: body.endpoint, ...body.keys, ...device } : { kind: "fcm", endpoint: body.token, ...device });
|
|
151
164
|
}
|
|
152
|
-
async unsubscribe(
|
|
153
|
-
const
|
|
154
|
-
|
|
165
|
+
async unsubscribe(context, body) {
|
|
166
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
167
|
+
const recipient = (0, import_options2.recipientFor)(this.options, principal);
|
|
168
|
+
await this.repo.deleteSubscription((0, import_options.tenantOf)(principal), recipient, body.endpoint);
|
|
155
169
|
}
|
|
156
170
|
async preferencesFor(principal, recipient) {
|
|
157
|
-
const ctx = (0,
|
|
171
|
+
const ctx = (0, import_options.tenantOf)(principal);
|
|
158
172
|
const [own, orgDefault] = await Promise.all([
|
|
159
173
|
this.repo.ownPreferences(ctx, recipient),
|
|
160
174
|
this.repo.defaults(ctx, recipient.type)
|
|
@@ -162,12 +176,12 @@ let NotificationsController = class NotificationsController2 {
|
|
|
162
176
|
return preferencesOf(this.events, recipient.type, own, orgDefault);
|
|
163
177
|
}
|
|
164
178
|
async defaultsFor(principal, audience) {
|
|
165
|
-
const orgDefault = await this.repo.defaults((0,
|
|
179
|
+
const orgDefault = await this.repo.defaults((0, import_options.tenantOf)(principal), audience);
|
|
166
180
|
return preferencesOf(this.events, audience, {}, orgDefault);
|
|
167
181
|
}
|
|
168
182
|
/** Only the roles the app names set defaults, and only for an audience some event has. */
|
|
169
183
|
checkAudienceAdmin(principal, audience) {
|
|
170
|
-
(0,
|
|
184
|
+
(0, import_options2.recipientFor)(this.options, principal);
|
|
171
185
|
if (!this.options.audienceAdmins.includes(principal.role)) {
|
|
172
186
|
throw new import_errors.AppError("FORBIDDEN", "Your role does not allow this action");
|
|
173
187
|
}
|
|
@@ -190,73 +204,74 @@ let NotificationsController = class NotificationsController2 {
|
|
|
190
204
|
};
|
|
191
205
|
__decorate([
|
|
192
206
|
(0, import_common.Get)(),
|
|
193
|
-
__param(0, (0,
|
|
207
|
+
__param(0, (0, import_context.CurrentContext)()),
|
|
194
208
|
__param(1, (0, import_common.Query)(new import_nest_common.ZodPipe(ListQuerySchema)))
|
|
195
209
|
], NotificationsController.prototype, "list", null);
|
|
196
210
|
__decorate([
|
|
197
211
|
(0, import_common.Get)("unread-count"),
|
|
198
|
-
__param(0, (0,
|
|
212
|
+
__param(0, (0, import_context.CurrentContext)())
|
|
199
213
|
], NotificationsController.prototype, "unreadCount", null);
|
|
200
214
|
__decorate([
|
|
201
215
|
(0, import_common.Post)("read"),
|
|
202
216
|
(0, import_common.HttpCode)(200),
|
|
203
|
-
(0,
|
|
204
|
-
__param(0, (0,
|
|
217
|
+
(0, import_nest_common.MinRole)("VIEWER"),
|
|
218
|
+
__param(0, (0, import_context.CurrentContext)()),
|
|
205
219
|
__param(1, (0, import_common.Body)(new import_nest_common.ZodPipe(import_contracts.MarkReadBodySchema)))
|
|
206
220
|
], NotificationsController.prototype, "read", null);
|
|
207
221
|
__decorate([
|
|
208
222
|
(0, import_common.Post)("read-all"),
|
|
209
223
|
(0, import_common.HttpCode)(200),
|
|
210
|
-
(0,
|
|
211
|
-
__param(0, (0,
|
|
224
|
+
(0, import_nest_common.MinRole)("VIEWER"),
|
|
225
|
+
__param(0, (0, import_context.CurrentContext)())
|
|
212
226
|
], NotificationsController.prototype, "readAll", null);
|
|
213
227
|
__decorate([
|
|
214
228
|
(0, import_common.Get)("preferences"),
|
|
215
|
-
__param(0, (0,
|
|
229
|
+
__param(0, (0, import_context.CurrentContext)())
|
|
216
230
|
], NotificationsController.prototype, "preferences", null);
|
|
217
231
|
__decorate([
|
|
218
232
|
(0, import_common.Put)("preferences"),
|
|
219
|
-
(0,
|
|
220
|
-
__param(0, (0,
|
|
233
|
+
(0, import_nest_common.MinRole)("VIEWER"),
|
|
234
|
+
__param(0, (0, import_context.CurrentContext)()),
|
|
221
235
|
__param(1, (0, import_common.Body)(new import_nest_common.ZodPipe(import_contracts.PreferenceChangesSchema)))
|
|
222
236
|
], NotificationsController.prototype, "setPreferences", null);
|
|
223
237
|
__decorate([
|
|
224
238
|
(0, import_common.Get)("defaults/:audience"),
|
|
225
|
-
__param(0, (0,
|
|
239
|
+
__param(0, (0, import_context.CurrentContext)()),
|
|
226
240
|
__param(1, (0, import_common.Param)("audience", new import_nest_common.ZodPipe(AudienceSchema)))
|
|
227
241
|
], NotificationsController.prototype, "defaults", null);
|
|
228
242
|
__decorate([
|
|
229
243
|
(0, import_common.Put)("defaults/:audience"),
|
|
230
|
-
(0,
|
|
231
|
-
__param(0, (0,
|
|
244
|
+
(0, import_nest_common.MinRole)("VIEWER"),
|
|
245
|
+
__param(0, (0, import_context.CurrentContext)()),
|
|
232
246
|
__param(1, (0, import_common.Param)("audience", new import_nest_common.ZodPipe(AudienceSchema))),
|
|
233
247
|
__param(2, (0, import_common.Body)(new import_nest_common.ZodPipe(import_contracts.PreferenceChangesSchema)))
|
|
234
248
|
], NotificationsController.prototype, "setDefaults", null);
|
|
235
249
|
__decorate([
|
|
236
250
|
(0, import_common.Get)("push/key"),
|
|
237
|
-
__param(0, (0,
|
|
251
|
+
__param(0, (0, import_context.CurrentContext)())
|
|
238
252
|
], NotificationsController.prototype, "pushKey", null);
|
|
239
253
|
__decorate([
|
|
240
254
|
(0, import_common.Post)("push/subscriptions"),
|
|
241
255
|
(0, import_common.HttpCode)(204),
|
|
242
|
-
(0,
|
|
243
|
-
__param(0, (0,
|
|
256
|
+
(0, import_nest_common.MinRole)("VIEWER"),
|
|
257
|
+
__param(0, (0, import_context.CurrentContext)()),
|
|
244
258
|
__param(1, (0, import_common.Body)(new import_nest_common.ZodPipe(import_contracts.PushSubscriptionBodySchema))),
|
|
245
259
|
__param(2, (0, import_common.Headers)("user-agent"))
|
|
246
260
|
], NotificationsController.prototype, "subscribe", null);
|
|
247
261
|
__decorate([
|
|
248
262
|
(0, import_common.Delete)("push/subscriptions"),
|
|
249
263
|
(0, import_common.HttpCode)(204),
|
|
250
|
-
(0,
|
|
251
|
-
__param(0, (0,
|
|
264
|
+
(0, import_nest_common.MinRole)("VIEWER"),
|
|
265
|
+
__param(0, (0, import_context.CurrentContext)()),
|
|
252
266
|
__param(1, (0, import_common.Body)(new import_nest_common.ZodPipe(import_contracts.PushUnsubscribeBodySchema)))
|
|
253
267
|
], NotificationsController.prototype, "unsubscribe", null);
|
|
254
268
|
NotificationsController = __decorate([
|
|
255
269
|
(0, import_common.Controller)("api/v1/notifications"),
|
|
256
|
-
__param(0, (0, import_common.Inject)(import_options.
|
|
257
|
-
__param(1, (0, import_common.Inject)(
|
|
258
|
-
__param(2, (0, import_common.Inject)(
|
|
259
|
-
__param(3, (0, import_common.Inject)(
|
|
270
|
+
__param(0, (0, import_common.Inject)(import_options.NOTIFICATIONS_AUTH)),
|
|
271
|
+
__param(1, (0, import_common.Inject)(import_options2.NOTIFICATIONS_OPTIONS)),
|
|
272
|
+
__param(2, (0, import_common.Inject)(import_options2.NOTIFICATION_REPOSITORY)),
|
|
273
|
+
__param(3, (0, import_common.Inject)(import_options2.NOTIFICATIONS_REDIS)),
|
|
274
|
+
__param(4, (0, import_common.Inject)(import_nest_common.LOGGER))
|
|
260
275
|
], NotificationsController);
|
|
261
276
|
// Annotate the CommonJS export names for ESM import in node:
|
|
262
277
|
0 && (module.exports = {
|
|
@@ -1,8 +1,29 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ExecutionContext, InjectionToken } from '@nestjs/common';
|
|
2
2
|
import type { Redis } from 'ioredis';
|
|
3
3
|
import type { NotificationEventDef, NotificationEvents, Recipient } from '../events.js';
|
|
4
4
|
import type { NotificationRepository } from '../repository.js';
|
|
5
|
-
|
|
5
|
+
/** How a provider is made: a factory and what it is given, as Nest takes one. */
|
|
6
|
+
export interface Provide<T> {
|
|
7
|
+
useFactory: (...deps: any[]) => T | Promise<T>;
|
|
8
|
+
inject?: InjectionToken[];
|
|
9
|
+
}
|
|
10
|
+
/** Who calls, as notifications needs to know: their organization, person and role. */
|
|
11
|
+
export interface NotificationCaller {
|
|
12
|
+
organizationId: string;
|
|
13
|
+
/** Null for a caller who is no person (an API key). */
|
|
14
|
+
userId: string | null;
|
|
15
|
+
/** The caller's role in the organization (OWNER, ADMIN, MEMBER, VIEWER). */
|
|
16
|
+
role: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Sign-in, as this package needs it: the adapter the app wires.
|
|
20
|
+
* `@haatlabs-core/notifications/nest-auth` is the one for the platform's own sign-in.
|
|
21
|
+
*/
|
|
22
|
+
export interface NotificationsAuth {
|
|
23
|
+
/** The signed-in caller, or null when no one is (refused, 401). */
|
|
24
|
+
callerOf(context: ExecutionContext): NotificationCaller | null;
|
|
25
|
+
}
|
|
26
|
+
type Role = string;
|
|
6
27
|
export type AnyEvents = Record<string, NotificationEventDef<any>>;
|
|
7
28
|
/** A comment every 25 s keeps the stream under Cloudflare's 100 s idle cut. */
|
|
8
29
|
export declare const DEFAULT_STREAM_PING_MS = 25000;
|
|
@@ -19,7 +40,9 @@ export interface NotificationsModuleOptions<E extends AnyEvents> {
|
|
|
19
40
|
/** `VAPID_PUBLIC_KEY`, or null where Web Push is not set up. */
|
|
20
41
|
webPushPublicKey: string | null;
|
|
21
42
|
/** Who a caller is as a recipient; null is refused (403). Default: the signed-in person. */
|
|
22
|
-
|
|
43
|
+
/** Who calls, from the app's sign-in. */
|
|
44
|
+
auth: Provide<NotificationsAuth>;
|
|
45
|
+
recipientOf?(caller: NotificationCaller): Recipient | null;
|
|
23
46
|
/** The roles that see and set the organization's defaults. Default OWNER and ADMIN. */
|
|
24
47
|
audienceAdmins?: Role[];
|
|
25
48
|
/** How often the stream writes `: ping`. Default 25 s. */
|
|
@@ -31,7 +54,7 @@ export interface ResolvedNotificationsOptions {
|
|
|
31
54
|
events: NotificationEvents<AnyEvents>;
|
|
32
55
|
keyPrefix: string;
|
|
33
56
|
webPushPublicKey: string | null;
|
|
34
|
-
recipientOf(
|
|
57
|
+
recipientOf(caller: NotificationCaller): Recipient | null;
|
|
35
58
|
audienceAdmins: Role[];
|
|
36
59
|
streamPingMs: number;
|
|
37
60
|
streamLifetimeMs: number;
|
|
@@ -39,7 +62,14 @@ export interface ResolvedNotificationsOptions {
|
|
|
39
62
|
export declare const NOTIFICATIONS_OPTIONS: symbol;
|
|
40
63
|
export declare const NOTIFICATION_REPOSITORY: symbol;
|
|
41
64
|
export declare const NOTIFICATIONS_REDIS: symbol;
|
|
65
|
+
export declare const NOTIFICATIONS_AUTH: symbol;
|
|
66
|
+
/** The tenant a caller acts for. */
|
|
67
|
+
export declare const tenantOf: (caller: NotificationCaller) => {
|
|
68
|
+
organizationId: string;
|
|
69
|
+
};
|
|
42
70
|
export declare function resolveOptions<E extends AnyEvents>(options: NotificationsModuleOptions<E>): ResolvedNotificationsOptions;
|
|
43
71
|
/** The caller as a recipient; a caller who is none (an API key) is refused. */
|
|
44
|
-
export declare function recipientFor(options: ResolvedNotificationsOptions,
|
|
72
|
+
export declare function recipientFor(options: ResolvedNotificationsOptions, caller: NotificationCaller): Recipient;
|
|
73
|
+
/** The caller the adapter names, or a refusal: these routes are for signed-in people. */
|
|
74
|
+
export declare function callerOf(auth: NotificationsAuth, context: ExecutionContext): NotificationCaller;
|
|
45
75
|
export {};
|
package/dist/cjs/nest/options.js
CHANGED
|
@@ -19,11 +19,14 @@ var options_exports = {};
|
|
|
19
19
|
__export(options_exports, {
|
|
20
20
|
DEFAULT_STREAM_LIFETIME_MS: () => DEFAULT_STREAM_LIFETIME_MS,
|
|
21
21
|
DEFAULT_STREAM_PING_MS: () => DEFAULT_STREAM_PING_MS,
|
|
22
|
+
NOTIFICATIONS_AUTH: () => NOTIFICATIONS_AUTH,
|
|
22
23
|
NOTIFICATIONS_OPTIONS: () => NOTIFICATIONS_OPTIONS,
|
|
23
24
|
NOTIFICATIONS_REDIS: () => NOTIFICATIONS_REDIS,
|
|
24
25
|
NOTIFICATION_REPOSITORY: () => NOTIFICATION_REPOSITORY,
|
|
26
|
+
callerOf: () => callerOf,
|
|
25
27
|
recipientFor: () => recipientFor,
|
|
26
|
-
resolveOptions: () => resolveOptions
|
|
28
|
+
resolveOptions: () => resolveOptions,
|
|
29
|
+
tenantOf: () => tenantOf
|
|
27
30
|
});
|
|
28
31
|
module.exports = __toCommonJS(options_exports);
|
|
29
32
|
var import_errors = require("@haatlabs-core/errors");
|
|
@@ -33,7 +36,11 @@ const token = (name) => Symbol.for(`@haatlabs-core/notifications.${name}`);
|
|
|
33
36
|
const NOTIFICATIONS_OPTIONS = token("NOTIFICATIONS_OPTIONS");
|
|
34
37
|
const NOTIFICATION_REPOSITORY = token("NOTIFICATION_REPOSITORY");
|
|
35
38
|
const NOTIFICATIONS_REDIS = token("NOTIFICATIONS_REDIS");
|
|
36
|
-
const
|
|
39
|
+
const NOTIFICATIONS_AUTH = token("NOTIFICATIONS_AUTH");
|
|
40
|
+
const tenantOf = (caller) => ({
|
|
41
|
+
organizationId: caller.organizationId
|
|
42
|
+
});
|
|
43
|
+
const signedInPerson = (caller) => caller.userId ? { type: "user", id: caller.userId } : null;
|
|
37
44
|
const positive = (name, value) => {
|
|
38
45
|
if (!Number.isInteger(value) || value <= 0) {
|
|
39
46
|
throw new Error(`NotificationsModule: ${name} must be a positive whole number of ms`);
|
|
@@ -51,19 +58,28 @@ function resolveOptions(options) {
|
|
|
51
58
|
streamLifetimeMs: positive("streamLifetimeMs", options.streamLifetimeMs ?? DEFAULT_STREAM_LIFETIME_MS)
|
|
52
59
|
};
|
|
53
60
|
}
|
|
54
|
-
function recipientFor(options,
|
|
55
|
-
const recipient = options.recipientOf(
|
|
61
|
+
function recipientFor(options, caller) {
|
|
62
|
+
const recipient = options.recipientOf(caller);
|
|
56
63
|
if (!recipient)
|
|
57
64
|
throw new import_errors.AppError("FORBIDDEN", "Notifications are for signed-in people");
|
|
58
65
|
return recipient;
|
|
59
66
|
}
|
|
67
|
+
function callerOf(auth, context) {
|
|
68
|
+
const caller = auth.callerOf(context);
|
|
69
|
+
if (!caller)
|
|
70
|
+
throw new import_errors.AppError("UNAUTHENTICATED", "Authentication required");
|
|
71
|
+
return caller;
|
|
72
|
+
}
|
|
60
73
|
// Annotate the CommonJS export names for ESM import in node:
|
|
61
74
|
0 && (module.exports = {
|
|
62
75
|
DEFAULT_STREAM_LIFETIME_MS,
|
|
63
76
|
DEFAULT_STREAM_PING_MS,
|
|
77
|
+
NOTIFICATIONS_AUTH,
|
|
64
78
|
NOTIFICATIONS_OPTIONS,
|
|
65
79
|
NOTIFICATIONS_REDIS,
|
|
66
80
|
NOTIFICATION_REPOSITORY,
|
|
81
|
+
callerOf,
|
|
67
82
|
recipientFor,
|
|
68
|
-
resolveOptions
|
|
83
|
+
resolveOptions,
|
|
84
|
+
tenantOf
|
|
69
85
|
});
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type { ExecutionContext } from '@nestjs/common';
|
|
1
2
|
import type { BeforeApplicationShutdown } from '@nestjs/common';
|
|
2
3
|
import type { Logger } from '@haatlabs-core/logger';
|
|
3
|
-
import type { Principal } from '@haatlabs-core/nest-auth';
|
|
4
4
|
import type { FastifyReply } from 'fastify';
|
|
5
5
|
import type { Redis } from 'ioredis';
|
|
6
6
|
import type { NotificationRepository } from '../repository.js';
|
|
7
|
+
import type { NotificationsAuth } from './options.js';
|
|
7
8
|
import type { ResolvedNotificationsOptions } from './options.js';
|
|
8
9
|
/**
|
|
9
10
|
* The caller's live stream (Server-Sent Events): `unread` on connect and after every change,
|
|
@@ -12,13 +13,14 @@ import type { ResolvedNotificationsOptions } from './options.js';
|
|
|
12
13
|
* reconnects to another instance), so closing the server does not wait out their lifetime.
|
|
13
14
|
*/
|
|
14
15
|
export declare class NotificationStreamController implements BeforeApplicationShutdown {
|
|
16
|
+
private readonly auth;
|
|
15
17
|
private readonly options;
|
|
16
18
|
private readonly repo;
|
|
17
19
|
private readonly redis;
|
|
18
20
|
private readonly logger;
|
|
19
21
|
/** How to end each stream open on this instance; a stream removes its own on close. */
|
|
20
22
|
private readonly open;
|
|
21
|
-
constructor(options: ResolvedNotificationsOptions, repo: NotificationRepository, redis: Redis, logger: Pick<Logger, 'warn'>);
|
|
23
|
+
constructor(auth: NotificationsAuth, options: ResolvedNotificationsOptions, repo: NotificationRepository, redis: Redis, logger: Pick<Logger, 'warn'>);
|
|
22
24
|
beforeApplicationShutdown(): void;
|
|
23
|
-
stream(
|
|
25
|
+
stream(context: ExecutionContext, reply: FastifyReply): Promise<void>;
|
|
24
26
|
}
|
|
@@ -22,10 +22,11 @@ __export(stream_controller_exports, {
|
|
|
22
22
|
module.exports = __toCommonJS(stream_controller_exports);
|
|
23
23
|
var import_common = require("@nestjs/common");
|
|
24
24
|
var import_errors = require("@haatlabs-core/errors");
|
|
25
|
-
var import_nest_auth = require("@haatlabs-core/nest-auth");
|
|
26
25
|
var import_nest_common = require("@haatlabs-core/nest-common");
|
|
27
26
|
var import_live = require("../live.js");
|
|
27
|
+
var import_context = require("./context.js");
|
|
28
28
|
var import_options = require("./options.js");
|
|
29
|
+
var import_options2 = require("./options.js");
|
|
29
30
|
var __decorate = function(decorators, target, key, desc) {
|
|
30
31
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
31
32
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -51,13 +52,15 @@ const parse = (text) => {
|
|
|
51
52
|
};
|
|
52
53
|
const gone = (reply) => reply.request.raw.destroyed || reply.raw.destroyed || reply.raw.socket?.destroyed === true;
|
|
53
54
|
let NotificationStreamController = class NotificationStreamController2 {
|
|
55
|
+
auth;
|
|
54
56
|
options;
|
|
55
57
|
repo;
|
|
56
58
|
redis;
|
|
57
59
|
logger;
|
|
58
60
|
/** How to end each stream open on this instance; a stream removes its own on close. */
|
|
59
61
|
open = /* @__PURE__ */ new Set();
|
|
60
|
-
constructor(options, repo, redis, logger) {
|
|
62
|
+
constructor(auth, options, repo, redis, logger) {
|
|
63
|
+
this.auth = auth;
|
|
61
64
|
this.options = options;
|
|
62
65
|
this.repo = repo;
|
|
63
66
|
this.redis = redis;
|
|
@@ -67,9 +70,10 @@ let NotificationStreamController = class NotificationStreamController2 {
|
|
|
67
70
|
for (const close of [...this.open])
|
|
68
71
|
close();
|
|
69
72
|
}
|
|
70
|
-
async stream(
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
+
async stream(context, reply) {
|
|
74
|
+
const principal = (0, import_options.callerOf)(this.auth, context);
|
|
75
|
+
const recipient = (0, import_options2.recipientFor)(this.options, principal);
|
|
76
|
+
const ctx = (0, import_options.tenantOf)(principal);
|
|
73
77
|
const channel = (0, import_live.liveChannel)(this.options.keyPrefix, principal.organizationId, recipient);
|
|
74
78
|
const raw = reply.raw;
|
|
75
79
|
if (gone(reply)) {
|
|
@@ -145,15 +149,16 @@ let NotificationStreamController = class NotificationStreamController2 {
|
|
|
145
149
|
};
|
|
146
150
|
__decorate([
|
|
147
151
|
(0, import_common.Get)("stream"),
|
|
148
|
-
__param(0, (0,
|
|
152
|
+
__param(0, (0, import_context.CurrentContext)()),
|
|
149
153
|
__param(1, (0, import_common.Res)())
|
|
150
154
|
], NotificationStreamController.prototype, "stream", null);
|
|
151
155
|
NotificationStreamController = __decorate([
|
|
152
156
|
(0, import_common.Controller)("api/v1/notifications"),
|
|
153
|
-
__param(0, (0, import_common.Inject)(import_options.
|
|
154
|
-
__param(1, (0, import_common.Inject)(
|
|
155
|
-
__param(2, (0, import_common.Inject)(
|
|
156
|
-
__param(3, (0, import_common.Inject)(
|
|
157
|
+
__param(0, (0, import_common.Inject)(import_options.NOTIFICATIONS_AUTH)),
|
|
158
|
+
__param(1, (0, import_common.Inject)(import_options2.NOTIFICATIONS_OPTIONS)),
|
|
159
|
+
__param(2, (0, import_common.Inject)(import_options2.NOTIFICATION_REPOSITORY)),
|
|
160
|
+
__param(3, (0, import_common.Inject)(import_options2.NOTIFICATIONS_REDIS)),
|
|
161
|
+
__param(4, (0, import_common.Inject)(import_nest_common.LOGGER))
|
|
157
162
|
], NotificationStreamController);
|
|
158
163
|
// Annotate the CommonJS export names for ESM import in node:
|
|
159
164
|
0 && (module.exports = {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { NotificationsAuth, Provide } from '../nest/options.js';
|
|
2
|
+
/**
|
|
3
|
+
* The auth adapter for the platform's own sign-in (@haatlabs-core/nest-auth): the caller is the
|
|
4
|
+
* principal `AuthModule`'s guard signed in.
|
|
5
|
+
*
|
|
6
|
+
* NotificationsModule.forRoot({ auth: nestAuthAdapter, … })
|
|
7
|
+
*/
|
|
8
|
+
export declare const nestAuthAdapter: Provide<NotificationsAuth>;
|