@arkstack/notifications 0.17.12 → 0.17.13

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/dist/index.d.ts CHANGED
@@ -392,6 +392,20 @@ declare class FirebaseRealtimeDriver implements RealtimeDriver {
392
392
  }
393
393
  //#endregion
394
394
  //#region src/drivers/realtime/PusherRealtimeDriver.d.ts
395
+ /** The slice of the `pusher` server SDK this driver uses. */
396
+ interface PusherClient {
397
+ trigger(channel: string | string[], event: string, data: unknown): Promise<unknown>;
398
+ authorizeChannel(socketId: string, channel: string, data?: {
399
+ user_id: string;
400
+ user_info?: {
401
+ [key: string]: any;
402
+ };
403
+ }): {
404
+ auth: string;
405
+ channel_data?: string;
406
+ shared_secret?: string;
407
+ };
408
+ }
395
409
  /**
396
410
  * Broadcasts notifications over [Pusher Channels](https://pusher.com/channels).
397
411
  *
@@ -404,6 +418,21 @@ declare class PusherRealtimeDriver implements RealtimeDriver {
404
418
  constructor(options?: PusherTransportConfig);
405
419
  private client;
406
420
  broadcast(channel: string | string[], event: string, payload: RealtimeNotificationPayload): Promise<unknown>;
421
+ /**
422
+ * Authourize a pusher channel
423
+ *
424
+ * @param socketId
425
+ * @param channel
426
+ * @returns
427
+ */
428
+ auth(socketId: string, channel: string, data?: Parameters<PusherClient['authorizeChannel']>[2]): Promise<ReturnType<PusherClient['authorizeChannel']>>;
429
+ /**
430
+ * Register a realtime autorization route
431
+ *
432
+ * @param authEndpoint
433
+ * @param middleware
434
+ */
435
+ registerAuthRoute(authEndpoint?: string, middleware?: unknown | unknown[]): Promise<void>;
407
436
  }
408
437
  //#endregion
409
438
  //#region src/Contracts/RealtimeDriver.d.ts
@@ -417,6 +446,7 @@ declare class PusherRealtimeDriver implements RealtimeDriver {
417
446
  */
418
447
  interface RealtimeDriver {
419
448
  broadcast(channel: string | string[], event: string, payload: RealtimeNotificationPayload): Promise<unknown>;
449
+ auth?(socketId: string, channel: string, data?: unknown): unknown | Promise<unknown>;
420
450
  }
421
451
  type RealtimeNotificationDriver<T extends RealtimeDriverName> = T extends 'firebase' ? FirebaseRealtimeDriver : T extends 'firebase' ? PusherRealtimeDriver : RealtimeDriver;
422
452
  //#endregion
package/dist/index.js CHANGED
@@ -560,6 +560,44 @@ var PusherRealtimeDriver = class {
560
560
  async broadcast(channel, event, payload) {
561
561
  return await (await this.client()).trigger(channel, event, payload);
562
562
  }
563
+ /**
564
+ * Authourize a pusher channel
565
+ *
566
+ * @param socketId
567
+ * @param channel
568
+ * @returns
569
+ */
570
+ async auth(socketId, channel, data) {
571
+ return (await this.client()).authorizeChannel(socketId, channel, data);
572
+ }
573
+ /**
574
+ * Register a realtime autorization route
575
+ *
576
+ * @param authEndpoint
577
+ * @param middleware
578
+ */
579
+ async registerAuthRoute(authEndpoint = "/realtime/auth", middleware) {
580
+ const client = await this.client();
581
+ for (const [name, path] of Object.entries({
582
+ express: "@arkstack/driver-express",
583
+ h3: "@arkstack/driver-express"
584
+ })) {
585
+ const midsPath = `${path}/middlewares`;
586
+ try {
587
+ const { Router } = await import(path);
588
+ const { auth } = await import(midsPath);
589
+ const middlewares = new Set(Array.isArray(middleware) ? middleware.concat(auth) : [middleware, auth]);
590
+ Router.post(authEndpoint, ({ clearRequest }) => {
591
+ const channel = clearRequest.input("channel_name");
592
+ const socketId = clearRequest.input("socket_id");
593
+ return client.authorizeChannel(socketId, channel);
594
+ }).middleware(Array.from(middlewares));
595
+ break;
596
+ } catch (e) {
597
+ console.error(`Failed to register auth route for ${name}: ${e instanceof Error ? e.message : e}`);
598
+ }
599
+ }
600
+ }
563
601
  };
564
602
  //#endregion
565
603
  //#region src/drivers/RealtimeNotification.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkstack/notifications",
3
- "version": "0.17.12",
3
+ "version": "0.17.13",
4
4
  "type": "module",
5
5
  "description": "Framework-agnostic notification module for Arkstack and Nodejs, providing support for multi-channel notification delivery.",
6
6
  "homepage": "https://arkstack.toneflix.net/guide/notifications",
@@ -34,16 +34,24 @@
34
34
  "africastalking": "^0.8.0",
35
35
  "nodemailer": "^9.0.3",
36
36
  "twilio": "^6.0.2",
37
- "@arkstack/common": "^0.17.12"
37
+ "@arkstack/common": "^0.17.13"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@kanun-hq/plugin-phone": "^0.1.8",
41
41
  "firebase-admin": "^13.0.0",
42
42
  "pusher": "^5.2.0",
43
- "@arkstack/database": "^0.17.12",
44
- "@arkstack/contract": "^0.17.12"
43
+ "@arkstack/contract": "^0.17.13",
44
+ "@arkstack/database": "^0.17.13",
45
+ "@arkstack/driver-express": "^0.17.13",
46
+ "@arkstack/driver-h3": "^0.17.13"
45
47
  },
46
48
  "peerDependenciesMeta": {
49
+ "@arkstack/driver-express": {
50
+ "optional": true
51
+ },
52
+ "@arkstack/driver-h3": {
53
+ "optional": true
54
+ },
47
55
  "pusher": {
48
56
  "optional": true
49
57
  },