@openfin/cloud-notification-core-api 0.0.1-alpha.e9e4a55 → 0.0.1-alpha.ef26f57
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/bundle.d.ts +203 -43
- package/index.cjs +46 -7
- package/index.mjs +46 -7
- package/package.json +1 -1
package/bundle.d.ts
CHANGED
|
@@ -4,13 +4,38 @@ export declare class AuthorizationError extends CloudNotificationAPIError {
|
|
|
4
4
|
constructor(message?: string, code?: string);
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Represents common properties for all notification events
|
|
9
|
+
*/
|
|
7
10
|
export declare type BaseNotificationReceivedEvent = {
|
|
11
|
+
/**
|
|
12
|
+
* The action that was performed on the notification
|
|
13
|
+
*/
|
|
8
14
|
action: 'new' | 'update' | 'delete';
|
|
15
|
+
/**
|
|
16
|
+
* The unique identifier for the notification
|
|
17
|
+
*/
|
|
9
18
|
notificationId: string;
|
|
19
|
+
/**
|
|
20
|
+
* The correlation identifier for the notification
|
|
21
|
+
*/
|
|
10
22
|
correlationId?: string;
|
|
23
|
+
/**
|
|
24
|
+
* The identifier of the target of the notification
|
|
25
|
+
*/
|
|
11
26
|
target: string;
|
|
27
|
+
/**
|
|
28
|
+
* The name of the target i.e, the group or user that the notification was sent to
|
|
29
|
+
*/
|
|
12
30
|
targetName?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The type of the target i.e, 'group' or 'user'
|
|
33
|
+
*/
|
|
13
34
|
targetType: string;
|
|
35
|
+
/**
|
|
36
|
+
* The payload of the notification.
|
|
37
|
+
* See the documentation for the notification center events for more information on the payload
|
|
38
|
+
*/
|
|
14
39
|
payload: unknown;
|
|
15
40
|
};
|
|
16
41
|
|
|
@@ -20,11 +45,30 @@ export declare type BaseNotificationReceivedEvent = {
|
|
|
20
45
|
*/
|
|
21
46
|
export declare class CloudNotificationAPI {
|
|
22
47
|
#private;
|
|
23
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Constructs a new instance of the CloudNotificationAPI
|
|
50
|
+
*
|
|
51
|
+
* @param cloudNotificationSettings - The settings for the Cloud Notification API.
|
|
52
|
+
*/
|
|
24
53
|
constructor(cloudNotificationSettings: CloudNotificationSettings);
|
|
25
54
|
/**
|
|
26
55
|
* Connects and creates a session on the Cloud Notifications service.
|
|
27
56
|
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const notificationApi = new CloudNotificationAPI({
|
|
60
|
+
* url: process.env.NOTIFICATION_SERVER_HOST
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* let connectionResult: ConnectionResult;
|
|
64
|
+
* try {
|
|
65
|
+
* connectionResult = await notificationApi.connect(connectSettings);
|
|
66
|
+
* } catch (errorConnect) {
|
|
67
|
+
* terminal.write(chalk.red(`\nError connecting to notification server: ${errorConnect}\n`));
|
|
68
|
+
* process.exit(1);
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
28
72
|
* @param parameters - The parameters to use to connect.
|
|
29
73
|
* @returns A promise that resolves when the connection is established.
|
|
30
74
|
* @throws {@link CloudNotificationAPIError} If an error occurs during connection.
|
|
@@ -56,7 +100,7 @@ export declare class CloudNotificationAPI {
|
|
|
56
100
|
* Raises a notification to the Cloud Notification service.
|
|
57
101
|
*
|
|
58
102
|
* @param options - The options for the notification.
|
|
59
|
-
* @param payload - The payload of the notification
|
|
103
|
+
* @param payload - The payload of the notification which should generally conform to the notification center schema
|
|
60
104
|
* @returns A promise that resolves with the notification raise result.
|
|
61
105
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
62
106
|
* @throws {@link PublishError} If an error occurs during publishing.
|
|
@@ -67,7 +111,7 @@ export declare class CloudNotificationAPI {
|
|
|
67
111
|
*
|
|
68
112
|
* @param notificationId - The ID of the notification to update.
|
|
69
113
|
* @param options - The options for the notification update.
|
|
70
|
-
* @param payload - The new payload of the notification
|
|
114
|
+
* @param payload - The new payload of the notification which should generally conform to the notification center update schema
|
|
71
115
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
72
116
|
* @throws {@link PublishError} If an error occurs during publishing of the update.
|
|
73
117
|
*/
|
|
@@ -75,6 +119,8 @@ export declare class CloudNotificationAPI {
|
|
|
75
119
|
/**
|
|
76
120
|
* Marks a notification as deleted in the Cloud Notification service.
|
|
77
121
|
*
|
|
122
|
+
* This in turn causes notification events to be raised for the notification
|
|
123
|
+
*
|
|
78
124
|
* @param notificationId - The ID of the notification to delete.
|
|
79
125
|
* @returns A promise that resolves when the notification is deleted.
|
|
80
126
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
@@ -83,8 +129,10 @@ export declare class CloudNotificationAPI {
|
|
|
83
129
|
/**
|
|
84
130
|
* Replays notifications from the Cloud Notification service.
|
|
85
131
|
*
|
|
132
|
+
* This is called at platform startup to populate the notification center with notifications that were missed since the last time the platform was started.
|
|
133
|
+
*
|
|
86
134
|
* @param pageItemLimit - The maximum number of items per page.
|
|
87
|
-
* @param pageStartCursor - The cursor to start the page from.
|
|
135
|
+
* @param pageStartCursor - The cursor to start the page from. This is retrieved from the pageInfo property.
|
|
88
136
|
* @returns A promise that resolves with the notifications replay details.
|
|
89
137
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
90
138
|
* @throws {@link NotificationRetrievalError} If an error occurs during retrieval.
|
|
@@ -126,6 +174,8 @@ export declare class CloudNotificationAPI {
|
|
|
126
174
|
export declare type CloudNotificationSettings = {
|
|
127
175
|
/**
|
|
128
176
|
* The URL of the notification server to connect to
|
|
177
|
+
*
|
|
178
|
+
* @example 'https://the-environment.example.com/notifications'
|
|
129
179
|
*/
|
|
130
180
|
url: string;
|
|
131
181
|
/**
|
|
@@ -141,7 +191,7 @@ export declare class CloudNotificationAPI {
|
|
|
141
191
|
/**
|
|
142
192
|
* Optional function to call with any logging messages to allow integration with the host application's logging
|
|
143
193
|
*
|
|
144
|
-
*
|
|
194
|
+
* Defaults to console.log
|
|
145
195
|
*/
|
|
146
196
|
logger?: CloudNotificationLogger;
|
|
147
197
|
/**
|
|
@@ -156,7 +206,7 @@ export declare class CloudNotificationAPI {
|
|
|
156
206
|
};
|
|
157
207
|
|
|
158
208
|
/**
|
|
159
|
-
*
|
|
209
|
+
* Represents the result of a successful connection to the server
|
|
160
210
|
*/
|
|
161
211
|
export declare type ConnectionResult = {
|
|
162
212
|
/**
|
|
@@ -190,7 +240,10 @@ export declare class CloudNotificationAPI {
|
|
|
190
240
|
export declare type ConnectParameters = {
|
|
191
241
|
/**
|
|
192
242
|
* ID for a group of shared applications.
|
|
243
|
+
*
|
|
193
244
|
* This acts as a namespace for the notification messages that allows separation of messages between different groups of applications for the same user
|
|
245
|
+
*
|
|
246
|
+
* This, in general, should be the uuid of the platform that you are communicating
|
|
194
247
|
*/
|
|
195
248
|
platformId: string;
|
|
196
249
|
/**
|
|
@@ -199,11 +252,10 @@ export declare class CloudNotificationAPI {
|
|
|
199
252
|
sourceId: string;
|
|
200
253
|
/**
|
|
201
254
|
* Determines the type of authentication to use with the service gateway
|
|
202
|
-
* defaults to 'none'
|
|
203
255
|
*
|
|
204
|
-
* 'jwt' - Use JWT authentication, in this case jwtAuthenticationParameters must also be provided
|
|
205
|
-
* 'basic' - Use basic authentication, in this case basicAuthenticationParameters must also be provided
|
|
206
|
-
* 'default' - Authentication will be inherited from the current session
|
|
256
|
+
* - 'jwt' - Use JWT authentication, in this case jwtAuthenticationParameters must also be provided
|
|
257
|
+
* - 'basic' - Use basic authentication, in this case basicAuthenticationParameters must also be provided
|
|
258
|
+
* - 'default' - Authentication will be inherited from the current session
|
|
207
259
|
*/
|
|
208
260
|
authenticationType?: 'jwt' | 'basic' | 'default';
|
|
209
261
|
/**
|
|
@@ -225,6 +277,15 @@ export declare class CloudNotificationAPI {
|
|
|
225
277
|
jwtAuthenticationParameters?: {
|
|
226
278
|
/**
|
|
227
279
|
* When JWT authentication is being used, this will be invoked just whenever a JWT token is required for a request
|
|
280
|
+
*
|
|
281
|
+
* This token should be conform to the configuration set within your environment. Contact Here support for more details
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```typescript
|
|
285
|
+
* const jwtRequestCallback = () => {
|
|
286
|
+
* return 'your-jwt-token';
|
|
287
|
+
* };
|
|
288
|
+
* ```
|
|
228
289
|
*/
|
|
229
290
|
jwtRequestCallback: () => string | object;
|
|
230
291
|
/**
|
|
@@ -240,19 +301,62 @@ export declare class CloudNotificationAPI {
|
|
|
240
301
|
action: 'delete';
|
|
241
302
|
};
|
|
242
303
|
|
|
243
|
-
export declare type EventListenersMap = Map<keyof EventMap, Array<(...args: Parameters<EventMap[keyof EventMap]>) => void>>;
|
|
244
|
-
|
|
245
304
|
export declare type EventMap = {
|
|
305
|
+
/**
|
|
306
|
+
* Emitted when the connection has been re-established
|
|
307
|
+
* @returns void
|
|
308
|
+
*/
|
|
246
309
|
reconnected: () => void;
|
|
247
|
-
|
|
310
|
+
/**
|
|
311
|
+
* Emitted when the connection has been disconnected
|
|
312
|
+
* @returns void
|
|
313
|
+
*/
|
|
248
314
|
reconnecting: (attemptNo: number) => void;
|
|
315
|
+
/**
|
|
316
|
+
* Emitted when the connection has been disconnected
|
|
317
|
+
* @returns void
|
|
318
|
+
*/
|
|
319
|
+
disconnected: () => void;
|
|
320
|
+
/**
|
|
321
|
+
* Emitted when an error occurs
|
|
322
|
+
* @returns void
|
|
323
|
+
*/
|
|
249
324
|
error: (error: Error) => void;
|
|
325
|
+
/**
|
|
326
|
+
* Emitted when the session has expired
|
|
327
|
+
* @returns void
|
|
328
|
+
*/
|
|
250
329
|
'session-expired': () => void;
|
|
330
|
+
/**
|
|
331
|
+
* Emitted when the session has been extended
|
|
332
|
+
* @returns void
|
|
333
|
+
*/
|
|
251
334
|
'session-extended': () => void;
|
|
335
|
+
/**
|
|
336
|
+
* Emitted when a new notification is received
|
|
337
|
+
* @returns void
|
|
338
|
+
*/
|
|
252
339
|
'new-notification': (event: NewNotificationEvent) => void;
|
|
340
|
+
/**
|
|
341
|
+
* Emitted when an update to a notification is received
|
|
342
|
+
* @returns void
|
|
343
|
+
*/
|
|
253
344
|
'update-notification': (event: UpdateNotificationEvent) => void;
|
|
345
|
+
/**
|
|
346
|
+
* Emitted when a notification is deleted
|
|
347
|
+
* @returns void
|
|
348
|
+
*/
|
|
254
349
|
'delete-notification': (event: DeleteNotificationEvent) => void;
|
|
350
|
+
/**
|
|
351
|
+
* Emitted when a notification event is received for this specific session
|
|
352
|
+
* @returns void
|
|
353
|
+
*/
|
|
255
354
|
'notification-event': (event: NotificationEvent) => void;
|
|
355
|
+
/**
|
|
356
|
+
* Emitted when a notification event is received for all sessions
|
|
357
|
+
* @returns void
|
|
358
|
+
*/
|
|
359
|
+
'global-notification-event': (event: NotificationEvent) => void;
|
|
256
360
|
};
|
|
257
361
|
|
|
258
362
|
export declare class EventPublishError extends CloudNotificationAPIError {
|
|
@@ -273,11 +377,32 @@ export declare class CloudNotificationAPI {
|
|
|
273
377
|
action: 'new';
|
|
274
378
|
};
|
|
275
379
|
|
|
380
|
+
/**
|
|
381
|
+
* Represents a notification event
|
|
382
|
+
*/
|
|
276
383
|
export declare type NotificationEvent = {
|
|
384
|
+
/**
|
|
385
|
+
* The unique identifier for the notification
|
|
386
|
+
*/
|
|
277
387
|
notificationId: string;
|
|
388
|
+
/**
|
|
389
|
+
* The correlation identifier for the notification
|
|
390
|
+
*/
|
|
278
391
|
correlationId?: string;
|
|
392
|
+
/**
|
|
393
|
+
* The category of the notification.
|
|
394
|
+
* For notification center events this will be 'notification-center-event'
|
|
395
|
+
*/
|
|
279
396
|
category: string;
|
|
397
|
+
/**
|
|
398
|
+
* The type of the notification.
|
|
399
|
+
* For example 'notification-close' when a user closes a notification in the notification center
|
|
400
|
+
*/
|
|
280
401
|
type: string;
|
|
402
|
+
/**
|
|
403
|
+
* The payload of the notification.
|
|
404
|
+
* See the documentation for the notification center events for more information on the payload
|
|
405
|
+
*/
|
|
281
406
|
payload?: unknown;
|
|
282
407
|
};
|
|
283
408
|
|
|
@@ -287,16 +412,29 @@ export declare class CloudNotificationAPI {
|
|
|
287
412
|
declare type NotificationOptions_2 = {
|
|
288
413
|
/**
|
|
289
414
|
* A caller specified identifier for the notification
|
|
415
|
+
*
|
|
290
416
|
* This is used to identify the notification in the system when events and responses are received
|
|
291
417
|
*/
|
|
292
418
|
correlationId?: string;
|
|
293
419
|
/**
|
|
294
420
|
* The targets to send the notification to
|
|
421
|
+
*
|
|
295
422
|
* This is a set of groups and users that the notification will be sent to
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* ```typescript
|
|
426
|
+
* const notificationOptions: NotificationOptions = {
|
|
427
|
+
* targets: {
|
|
428
|
+
* groups: ['all-users'],
|
|
429
|
+
* users: ['someuser@company.com']
|
|
430
|
+
* }
|
|
431
|
+
* };
|
|
432
|
+
* ```
|
|
296
433
|
*/
|
|
297
434
|
targets: NotificationTargets;
|
|
298
435
|
/**
|
|
299
436
|
* Optional number of seconds to keep the notification alive
|
|
437
|
+
*
|
|
300
438
|
* After this time the notification will be expired and a delete update will be raised
|
|
301
439
|
*/
|
|
302
440
|
ttlSeconds?: number;
|
|
@@ -321,22 +459,67 @@ export declare class CloudNotificationAPI {
|
|
|
321
459
|
constructor(message?: string, code?: string, cause?: unknown);
|
|
322
460
|
}
|
|
323
461
|
|
|
462
|
+
/**
|
|
463
|
+
* Represents a single notification replay detail.
|
|
464
|
+
*/
|
|
324
465
|
export declare type NotificationsReplayDetail = {
|
|
466
|
+
/**
|
|
467
|
+
* The cursor of the notification.
|
|
468
|
+
*/
|
|
325
469
|
cursor: string;
|
|
470
|
+
/**
|
|
471
|
+
* The session ID of the notification.
|
|
472
|
+
*/
|
|
326
473
|
sessionId: string;
|
|
474
|
+
/**
|
|
475
|
+
* The notification ID.
|
|
476
|
+
*/
|
|
327
477
|
notificationId: string;
|
|
478
|
+
/**
|
|
479
|
+
* The version of the notification.
|
|
480
|
+
*/
|
|
328
481
|
version: number;
|
|
482
|
+
/**
|
|
483
|
+
* The type of notification record.
|
|
484
|
+
*/
|
|
329
485
|
action: 'new' | 'update' | 'delete';
|
|
486
|
+
/**
|
|
487
|
+
* The payload of the notification.
|
|
488
|
+
*/
|
|
330
489
|
payload?: unknown;
|
|
490
|
+
/**
|
|
491
|
+
* The original correlation ID of the notification if specified.
|
|
492
|
+
*/
|
|
331
493
|
correlationId?: string | null;
|
|
494
|
+
/**
|
|
495
|
+
* The timestamp of the notification.
|
|
496
|
+
*/
|
|
332
497
|
timestamp: Date;
|
|
333
498
|
};
|
|
334
499
|
|
|
500
|
+
/**
|
|
501
|
+
* Represents the results of a notifications replay request.
|
|
502
|
+
*/
|
|
335
503
|
export declare type NotificationsReplayDetails = {
|
|
504
|
+
/**
|
|
505
|
+
* The list of notifications replay details.
|
|
506
|
+
*/
|
|
336
507
|
data: NotificationsReplayDetail[];
|
|
508
|
+
/**
|
|
509
|
+
* The pagination information for the notifications replay request.
|
|
510
|
+
*/
|
|
337
511
|
pageInfo: {
|
|
512
|
+
/**
|
|
513
|
+
* The cursor of the first notification in the page.
|
|
514
|
+
*/
|
|
338
515
|
pageStart?: string;
|
|
516
|
+
/**
|
|
517
|
+
* The cursor of the next page.
|
|
518
|
+
*/
|
|
339
519
|
nextPage?: string;
|
|
520
|
+
/**
|
|
521
|
+
* Whether there is a next page of notifications.
|
|
522
|
+
*/
|
|
340
523
|
hasNextPage: boolean;
|
|
341
524
|
};
|
|
342
525
|
};
|
|
@@ -346,11 +529,16 @@ export declare class CloudNotificationAPI {
|
|
|
346
529
|
*/
|
|
347
530
|
export declare type NotificationTargets = {
|
|
348
531
|
/**
|
|
349
|
-
* The groups to send the notification to
|
|
532
|
+
* The optional groups to send the notification to
|
|
533
|
+
*
|
|
534
|
+
* This can be a collection of either group names e.g. all-users which can be retrieved from the admin console or the
|
|
535
|
+
* UUID of the group itself
|
|
350
536
|
*/
|
|
351
537
|
groups: string[];
|
|
352
538
|
/**
|
|
353
|
-
* The users to send the notification to
|
|
539
|
+
* The optional specific users to send the notification to
|
|
540
|
+
*
|
|
541
|
+
* This can be the username e.g. someuser\@company.com or the UUID of the user itself
|
|
354
542
|
*/
|
|
355
543
|
users: string[];
|
|
356
544
|
};
|
|
@@ -370,40 +558,12 @@ export declare class CloudNotificationAPI {
|
|
|
370
558
|
constructor(message?: string, code?: string, cause?: unknown);
|
|
371
559
|
}
|
|
372
560
|
|
|
373
|
-
export declare type PublishResult = {
|
|
374
|
-
notificationId: string;
|
|
375
|
-
correlationId: string | undefined;
|
|
376
|
-
};
|
|
377
|
-
|
|
378
561
|
export declare class SessionNotConnectedError extends CloudNotificationAPIError {
|
|
379
562
|
constructor(message?: string, code?: string);
|
|
380
563
|
}
|
|
381
564
|
|
|
382
|
-
/**
|
|
383
|
-
* Simple set-like structure that allows you to add values with a TTL (time to live).
|
|
384
|
-
* Values are automatically removed from the set after the TTL expires.
|
|
385
|
-
*/
|
|
386
|
-
declare class SetWithTTL<T> {
|
|
387
|
-
#private;
|
|
388
|
-
private ttl;
|
|
389
|
-
private store;
|
|
390
|
-
private nextCollection;
|
|
391
|
-
private collectionDelay;
|
|
392
|
-
constructor(ttl: number, collectionCheckInterval?: number);
|
|
393
|
-
get size(): number;
|
|
394
|
-
add(value: T, customTtl?: number): this;
|
|
395
|
-
has(value: T): boolean;
|
|
396
|
-
delete(value: T): this;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
565
|
export declare type UpdateNotificationEvent = BaseNotificationReceivedEvent & {
|
|
400
566
|
action: 'update';
|
|
401
567
|
};
|
|
402
568
|
|
|
403
|
-
export declare type UserGroupWithTopic = {
|
|
404
|
-
uuid: string;
|
|
405
|
-
name: string;
|
|
406
|
-
topic?: string;
|
|
407
|
-
};
|
|
408
|
-
|
|
409
569
|
export { }
|
package/index.cjs
CHANGED
|
@@ -285,7 +285,7 @@ class CloudNotificationAPI {
|
|
|
285
285
|
#keepAliveIntervalSeconds = 30;
|
|
286
286
|
#syncTime = true;
|
|
287
287
|
#timeDifferenceTracker;
|
|
288
|
-
deDuplicator;
|
|
288
|
+
#deDuplicator;
|
|
289
289
|
#reconnectRetries = 0;
|
|
290
290
|
#connectionParams;
|
|
291
291
|
#attemptingToReconnect = false;
|
|
@@ -293,9 +293,14 @@ class CloudNotificationAPI {
|
|
|
293
293
|
#defaultSubscriptionOptions = { qos: 2, nl: true };
|
|
294
294
|
#logger = (level, message) => console[level](message);
|
|
295
295
|
#getCurrentTimeOffset = () => this.#timeDifferenceTracker?.currentOffset || 0;
|
|
296
|
+
/**
|
|
297
|
+
* Constructs a new instance of the CloudNotificationAPI
|
|
298
|
+
*
|
|
299
|
+
* @param cloudNotificationSettings - The settings for the Cloud Notification API.
|
|
300
|
+
*/
|
|
296
301
|
constructor(cloudNotificationSettings) {
|
|
297
302
|
this.#cloudNotificationSettings = cloudNotificationSettings;
|
|
298
|
-
this
|
|
303
|
+
this.#deDuplicator = new SetWithTTL(cloudNotificationSettings?.deduplicationTTLms || 10_000);
|
|
299
304
|
this.#reconnectRetryLimit = cloudNotificationSettings.reconnectRetryLimit || this.#reconnectRetryLimit;
|
|
300
305
|
this.#keepAliveIntervalSeconds = cloudNotificationSettings.keepAliveIntervalSeconds || this.#keepAliveIntervalSeconds;
|
|
301
306
|
this.#logger = cloudNotificationSettings.logger || this.#logger;
|
|
@@ -304,6 +309,21 @@ class CloudNotificationAPI {
|
|
|
304
309
|
/**
|
|
305
310
|
* Connects and creates a session on the Cloud Notifications service.
|
|
306
311
|
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```typescript
|
|
314
|
+
* const notificationApi = new CloudNotificationAPI({
|
|
315
|
+
* url: process.env.NOTIFICATION_SERVER_HOST
|
|
316
|
+
* });
|
|
317
|
+
*
|
|
318
|
+
* let connectionResult: ConnectionResult;
|
|
319
|
+
* try {
|
|
320
|
+
* connectionResult = await notificationApi.connect(connectSettings);
|
|
321
|
+
* } catch (errorConnect) {
|
|
322
|
+
* terminal.write(chalk.red(`\nError connecting to notification server: ${errorConnect}\n`));
|
|
323
|
+
* process.exit(1);
|
|
324
|
+
* }
|
|
325
|
+
* ```
|
|
326
|
+
*
|
|
307
327
|
* @param parameters - The parameters to use to connect.
|
|
308
328
|
* @returns A promise that resolves when the connection is established.
|
|
309
329
|
* @throws {@link CloudNotificationAPIError} If an error occurs during connection.
|
|
@@ -385,7 +405,7 @@ class CloudNotificationAPI {
|
|
|
385
405
|
* Raises a notification to the Cloud Notification service.
|
|
386
406
|
*
|
|
387
407
|
* @param options - The options for the notification.
|
|
388
|
-
* @param payload - The payload of the notification
|
|
408
|
+
* @param payload - The payload of the notification which should generally conform to the notification center schema
|
|
389
409
|
* @returns A promise that resolves with the notification raise result.
|
|
390
410
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
391
411
|
* @throws {@link PublishError} If an error occurs during publishing.
|
|
@@ -424,7 +444,7 @@ class CloudNotificationAPI {
|
|
|
424
444
|
*
|
|
425
445
|
* @param notificationId - The ID of the notification to update.
|
|
426
446
|
* @param options - The options for the notification update.
|
|
427
|
-
* @param payload - The new payload of the notification
|
|
447
|
+
* @param payload - The new payload of the notification which should generally conform to the notification center update schema
|
|
428
448
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
429
449
|
* @throws {@link PublishError} If an error occurs during publishing of the update.
|
|
430
450
|
*/
|
|
@@ -454,6 +474,8 @@ class CloudNotificationAPI {
|
|
|
454
474
|
/**
|
|
455
475
|
* Marks a notification as deleted in the Cloud Notification service.
|
|
456
476
|
*
|
|
477
|
+
* This in turn causes notification events to be raised for the notification
|
|
478
|
+
*
|
|
457
479
|
* @param notificationId - The ID of the notification to delete.
|
|
458
480
|
* @returns A promise that resolves when the notification is deleted.
|
|
459
481
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
@@ -477,8 +499,10 @@ class CloudNotificationAPI {
|
|
|
477
499
|
/**
|
|
478
500
|
* Replays notifications from the Cloud Notification service.
|
|
479
501
|
*
|
|
502
|
+
* This is called at platform startup to populate the notification center with notifications that were missed since the last time the platform was started.
|
|
503
|
+
*
|
|
480
504
|
* @param pageItemLimit - The maximum number of items per page.
|
|
481
|
-
* @param pageStartCursor - The cursor to start the page from.
|
|
505
|
+
* @param pageStartCursor - The cursor to start the page from. This is retrieved from the pageInfo property.
|
|
482
506
|
* @returns A promise that resolves with the notifications replay details.
|
|
483
507
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
484
508
|
* @throws {@link NotificationRetrievalError} If an error occurs during retrieval.
|
|
@@ -649,6 +673,10 @@ class CloudNotificationAPI {
|
|
|
649
673
|
this.#mqttClient?.subscribeAsync(this.#sessionDetails.mqtt.userNotificationTopic, this.#defaultSubscriptionOptions),
|
|
650
674
|
// The users notifications status updates
|
|
651
675
|
this.#mqttClient?.subscribeAsync(this.#sessionDetails.mqtt.userNotificationEventsTopic, this.#defaultSubscriptionOptions));
|
|
676
|
+
// Global notification events
|
|
677
|
+
if (this.#sessionDetails.mqtt.allNotificationEventsTopic) {
|
|
678
|
+
subscribePromises.push(this.#mqttClient?.subscribeAsync(this.#sessionDetails.mqtt.allNotificationEventsTopic, this.#defaultSubscriptionOptions));
|
|
679
|
+
}
|
|
652
680
|
await Promise.all(subscribePromises);
|
|
653
681
|
}
|
|
654
682
|
async #extendSession() {
|
|
@@ -732,6 +760,9 @@ class CloudNotificationAPI {
|
|
|
732
760
|
else if (topic === sessionDetails.mqtt.userNotificationEventsTopic) {
|
|
733
761
|
this.#handleUserNotificationEventMessage(messagePayload);
|
|
734
762
|
}
|
|
763
|
+
else if (topic === sessionDetails.mqtt.allNotificationEventsTopic) {
|
|
764
|
+
this.#handleAllNotificationEventMessage(messagePayload);
|
|
765
|
+
}
|
|
735
766
|
else {
|
|
736
767
|
this.#logger('warn', `Received message on unknown topic ${topic}`);
|
|
737
768
|
this.#events.emitEvent('error', new CloudNotificationAPIError(`Received message on unknown topic ${topic}`, 'ERR_UNKNOWN_TOPIC'));
|
|
@@ -754,10 +785,10 @@ class CloudNotificationAPI {
|
|
|
754
785
|
return;
|
|
755
786
|
}
|
|
756
787
|
// We might have received this update from a different group or user topic so de-dupe
|
|
757
|
-
if (this
|
|
788
|
+
if (this.#deDuplicator.has(txInstanceId)) {
|
|
758
789
|
return;
|
|
759
790
|
}
|
|
760
|
-
this
|
|
791
|
+
this.#deDuplicator.add(txInstanceId);
|
|
761
792
|
this.#sendNotificationDeliveredStatus(notificationId);
|
|
762
793
|
let targetName = target;
|
|
763
794
|
if (targetType === 'group') {
|
|
@@ -787,6 +818,14 @@ class CloudNotificationAPI {
|
|
|
787
818
|
}
|
|
788
819
|
this.#events.emitEvent('notification-event', parseResult.data);
|
|
789
820
|
}
|
|
821
|
+
#handleAllNotificationEventMessage(messagePayload) {
|
|
822
|
+
const parseResult = f.safeParse(messagePayload);
|
|
823
|
+
if (!parseResult.success) {
|
|
824
|
+
this.#logger('warn', `Received invalid notification event message payload format ${parseResult.error?.toString()}`);
|
|
825
|
+
throw new InvalidMessageFormatError(parseResult);
|
|
826
|
+
}
|
|
827
|
+
this.#events.emitEvent('global-notification-event', parseResult.data);
|
|
828
|
+
}
|
|
790
829
|
#lookupGroupNameByUuid(uuid) {
|
|
791
830
|
if (!this.#sessionDetails) {
|
|
792
831
|
return undefined;
|
package/index.mjs
CHANGED
|
@@ -283,7 +283,7 @@ class CloudNotificationAPI {
|
|
|
283
283
|
#keepAliveIntervalSeconds = 30;
|
|
284
284
|
#syncTime = true;
|
|
285
285
|
#timeDifferenceTracker;
|
|
286
|
-
deDuplicator;
|
|
286
|
+
#deDuplicator;
|
|
287
287
|
#reconnectRetries = 0;
|
|
288
288
|
#connectionParams;
|
|
289
289
|
#attemptingToReconnect = false;
|
|
@@ -291,9 +291,14 @@ class CloudNotificationAPI {
|
|
|
291
291
|
#defaultSubscriptionOptions = { qos: 2, nl: true };
|
|
292
292
|
#logger = (level, message) => console[level](message);
|
|
293
293
|
#getCurrentTimeOffset = () => this.#timeDifferenceTracker?.currentOffset || 0;
|
|
294
|
+
/**
|
|
295
|
+
* Constructs a new instance of the CloudNotificationAPI
|
|
296
|
+
*
|
|
297
|
+
* @param cloudNotificationSettings - The settings for the Cloud Notification API.
|
|
298
|
+
*/
|
|
294
299
|
constructor(cloudNotificationSettings) {
|
|
295
300
|
this.#cloudNotificationSettings = cloudNotificationSettings;
|
|
296
|
-
this
|
|
301
|
+
this.#deDuplicator = new SetWithTTL(cloudNotificationSettings?.deduplicationTTLms || 10_000);
|
|
297
302
|
this.#reconnectRetryLimit = cloudNotificationSettings.reconnectRetryLimit || this.#reconnectRetryLimit;
|
|
298
303
|
this.#keepAliveIntervalSeconds = cloudNotificationSettings.keepAliveIntervalSeconds || this.#keepAliveIntervalSeconds;
|
|
299
304
|
this.#logger = cloudNotificationSettings.logger || this.#logger;
|
|
@@ -302,6 +307,21 @@ class CloudNotificationAPI {
|
|
|
302
307
|
/**
|
|
303
308
|
* Connects and creates a session on the Cloud Notifications service.
|
|
304
309
|
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```typescript
|
|
312
|
+
* const notificationApi = new CloudNotificationAPI({
|
|
313
|
+
* url: process.env.NOTIFICATION_SERVER_HOST
|
|
314
|
+
* });
|
|
315
|
+
*
|
|
316
|
+
* let connectionResult: ConnectionResult;
|
|
317
|
+
* try {
|
|
318
|
+
* connectionResult = await notificationApi.connect(connectSettings);
|
|
319
|
+
* } catch (errorConnect) {
|
|
320
|
+
* terminal.write(chalk.red(`\nError connecting to notification server: ${errorConnect}\n`));
|
|
321
|
+
* process.exit(1);
|
|
322
|
+
* }
|
|
323
|
+
* ```
|
|
324
|
+
*
|
|
305
325
|
* @param parameters - The parameters to use to connect.
|
|
306
326
|
* @returns A promise that resolves when the connection is established.
|
|
307
327
|
* @throws {@link CloudNotificationAPIError} If an error occurs during connection.
|
|
@@ -383,7 +403,7 @@ class CloudNotificationAPI {
|
|
|
383
403
|
* Raises a notification to the Cloud Notification service.
|
|
384
404
|
*
|
|
385
405
|
* @param options - The options for the notification.
|
|
386
|
-
* @param payload - The payload of the notification
|
|
406
|
+
* @param payload - The payload of the notification which should generally conform to the notification center schema
|
|
387
407
|
* @returns A promise that resolves with the notification raise result.
|
|
388
408
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
389
409
|
* @throws {@link PublishError} If an error occurs during publishing.
|
|
@@ -422,7 +442,7 @@ class CloudNotificationAPI {
|
|
|
422
442
|
*
|
|
423
443
|
* @param notificationId - The ID of the notification to update.
|
|
424
444
|
* @param options - The options for the notification update.
|
|
425
|
-
* @param payload - The new payload of the notification
|
|
445
|
+
* @param payload - The new payload of the notification which should generally conform to the notification center update schema
|
|
426
446
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
427
447
|
* @throws {@link PublishError} If an error occurs during publishing of the update.
|
|
428
448
|
*/
|
|
@@ -452,6 +472,8 @@ class CloudNotificationAPI {
|
|
|
452
472
|
/**
|
|
453
473
|
* Marks a notification as deleted in the Cloud Notification service.
|
|
454
474
|
*
|
|
475
|
+
* This in turn causes notification events to be raised for the notification
|
|
476
|
+
*
|
|
455
477
|
* @param notificationId - The ID of the notification to delete.
|
|
456
478
|
* @returns A promise that resolves when the notification is deleted.
|
|
457
479
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
@@ -475,8 +497,10 @@ class CloudNotificationAPI {
|
|
|
475
497
|
/**
|
|
476
498
|
* Replays notifications from the Cloud Notification service.
|
|
477
499
|
*
|
|
500
|
+
* This is called at platform startup to populate the notification center with notifications that were missed since the last time the platform was started.
|
|
501
|
+
*
|
|
478
502
|
* @param pageItemLimit - The maximum number of items per page.
|
|
479
|
-
* @param pageStartCursor - The cursor to start the page from.
|
|
503
|
+
* @param pageStartCursor - The cursor to start the page from. This is retrieved from the pageInfo property.
|
|
480
504
|
* @returns A promise that resolves with the notifications replay details.
|
|
481
505
|
* @throws {@link SessionNotConnectedError} If the session is not connected.
|
|
482
506
|
* @throws {@link NotificationRetrievalError} If an error occurs during retrieval.
|
|
@@ -647,6 +671,10 @@ class CloudNotificationAPI {
|
|
|
647
671
|
this.#mqttClient?.subscribeAsync(this.#sessionDetails.mqtt.userNotificationTopic, this.#defaultSubscriptionOptions),
|
|
648
672
|
// The users notifications status updates
|
|
649
673
|
this.#mqttClient?.subscribeAsync(this.#sessionDetails.mqtt.userNotificationEventsTopic, this.#defaultSubscriptionOptions));
|
|
674
|
+
// Global notification events
|
|
675
|
+
if (this.#sessionDetails.mqtt.allNotificationEventsTopic) {
|
|
676
|
+
subscribePromises.push(this.#mqttClient?.subscribeAsync(this.#sessionDetails.mqtt.allNotificationEventsTopic, this.#defaultSubscriptionOptions));
|
|
677
|
+
}
|
|
650
678
|
await Promise.all(subscribePromises);
|
|
651
679
|
}
|
|
652
680
|
async #extendSession() {
|
|
@@ -730,6 +758,9 @@ class CloudNotificationAPI {
|
|
|
730
758
|
else if (topic === sessionDetails.mqtt.userNotificationEventsTopic) {
|
|
731
759
|
this.#handleUserNotificationEventMessage(messagePayload);
|
|
732
760
|
}
|
|
761
|
+
else if (topic === sessionDetails.mqtt.allNotificationEventsTopic) {
|
|
762
|
+
this.#handleAllNotificationEventMessage(messagePayload);
|
|
763
|
+
}
|
|
733
764
|
else {
|
|
734
765
|
this.#logger('warn', `Received message on unknown topic ${topic}`);
|
|
735
766
|
this.#events.emitEvent('error', new CloudNotificationAPIError(`Received message on unknown topic ${topic}`, 'ERR_UNKNOWN_TOPIC'));
|
|
@@ -752,10 +783,10 @@ class CloudNotificationAPI {
|
|
|
752
783
|
return;
|
|
753
784
|
}
|
|
754
785
|
// We might have received this update from a different group or user topic so de-dupe
|
|
755
|
-
if (this
|
|
786
|
+
if (this.#deDuplicator.has(txInstanceId)) {
|
|
756
787
|
return;
|
|
757
788
|
}
|
|
758
|
-
this
|
|
789
|
+
this.#deDuplicator.add(txInstanceId);
|
|
759
790
|
this.#sendNotificationDeliveredStatus(notificationId);
|
|
760
791
|
let targetName = target;
|
|
761
792
|
if (targetType === 'group') {
|
|
@@ -785,6 +816,14 @@ class CloudNotificationAPI {
|
|
|
785
816
|
}
|
|
786
817
|
this.#events.emitEvent('notification-event', parseResult.data);
|
|
787
818
|
}
|
|
819
|
+
#handleAllNotificationEventMessage(messagePayload) {
|
|
820
|
+
const parseResult = f.safeParse(messagePayload);
|
|
821
|
+
if (!parseResult.success) {
|
|
822
|
+
this.#logger('warn', `Received invalid notification event message payload format ${parseResult.error?.toString()}`);
|
|
823
|
+
throw new InvalidMessageFormatError(parseResult);
|
|
824
|
+
}
|
|
825
|
+
this.#events.emitEvent('global-notification-event', parseResult.data);
|
|
826
|
+
}
|
|
788
827
|
#lookupGroupNameByUuid(uuid) {
|
|
789
828
|
if (!this.#sessionDetails) {
|
|
790
829
|
return undefined;
|