@arkstack/notifications 0.17.11 → 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
@@ -222,18 +222,19 @@ var MailNotification = class extends NotificationContract {
222
222
  * Prepare the mail driver so we can use it to relay the message
223
223
  */
224
224
  async prepareDriver() {
225
+ const options = this.options;
226
+ this.fromAddress = options.from ?? this.driverConfig.from ?? env("MAIL_FROM_ADDRESS", "no-reply@example.com");
225
227
  if (typeof this.transport !== "string") {
226
228
  this.driver = nodemailer.createTransport(this.transport);
227
229
  return;
228
230
  }
229
- const options = this.options;
230
231
  const transport = configure(`transports.${this.transport}`, {});
232
+ this.fromAddress = transport.from || this.fromAddress;
231
233
  /**
232
234
  * Setup nodemailer to stream messages so we can store the mail to file
233
235
  */
234
236
  if (this.transport === "file") {
235
237
  this.fileDirectory = options.directory ?? transport.directory ?? env("MAIL_FILE_PATH", join(Arkstack.rootDir(), "./storage/framework/mails"));
236
- this.fromAddress = options.from ?? this.driverConfig.from ?? transport.from ?? env("MAIL_FROM_ADDRESS", "no-reply@example.com");
237
238
  this.driver = nodemailer.createTransport({
238
239
  streamTransport: true,
239
240
  buffer: true
@@ -272,7 +273,6 @@ var MailNotification = class extends NotificationContract {
272
273
  }
273
274
  const user = options.user ?? transport.auth?.user ?? transport.user ?? env("MAIL_USERNAME", "");
274
275
  const pass = options.pass ?? transport.auth?.pass ?? transport.pass ?? env("MAIL_PASSWORD", "");
275
- this.fromAddress = options.from ?? this.driverConfig.from ?? transport.from ?? env("MAIL_FROM_ADDRESS", "no-reply@example.com");
276
276
  this.driver = nodemailer.createTransport({
277
277
  url: transport.url ?? options.url ?? env("MAIL_URL"),
278
278
  host: options.host ?? transport.host ?? env("MAIL_HOST", "localhost"),
@@ -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.11",
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.11"
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/contract": "^0.17.11",
44
- "@arkstack/database": "^0.17.11"
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
  },