Skip to main content
zerotal

Documentation


Documentation / @zerotal/notifications / Notification

Abstract Class: Notification

Defined in: notifications/src/Notification.ts:45

Base class for all notifications.

Extend this class, declare which channels() to use, then implement the corresponding to*() method for each declared channel.

Supported channels: 'mail' — implement toMail() → returns a MailMessage 'database' — implement toDatabase() → returns a plain object 'slack' — implement toSlack() → returns a SlackMessage 'sms' — implement toSms() → returns an SmsMessage

Example

export class OrderShippedNotification extends Notification {
  constructor(private order: Order) { super(); }

  channels() { return ['mail', 'slack', 'database']; }

  toMail(notifiable: Notifiable): MailMessage {
    return new MailMessage()
      .subject(`Order #${this.order.id} shipped`)
      .line('Your order is on its way.')
      .action('Track', `https://app.test/orders/${this.order.id}`);
  }

  toSlack(_notifiable: Notifiable): SlackMessage | Promise<SlackMessage> {
    return {
      webhookUrl: 'https://hooks.slack.com/services/...',
      text:       `Order #${this.order.id} was shipped!`,
    };
  }

  toDatabase() {
    return { orderId: this.order.id, status: 'shipped' };
  }
}

Constructors

Constructor

new Notification(): Notification

Returns

Notification

Methods

channels()

abstract channels(notifiable?): string[]

Defined in: notifications/src/Notification.ts:58

Declare which channels to deliver on: 'mail', 'database', 'slack', 'sms', 'broadcast', or any channel registered with extend().

The recipient is passed in, so routing can follow their preferences. Ignore the parameter when every recipient gets the same channels.

Parameters

notifiable?

Notifiable

Returns

string[]

Example

channels(user: Notifiable) {
  return user.wantsSms ? ["database", "sms"] : ["database", "mail"];
}

toMail()

toMail(_notifiable): MailMessage | Promise<MailMessage>

Defined in: notifications/src/Notification.ts:60

Parameters

_notifiable

Notifiable

Returns

MailMessage | Promise<MailMessage>


toDatabase()

toDatabase(_notifiable): Record<string, unknown> | Promise<Record<string, unknown>>

Defined in: notifications/src/Notification.ts:64

Parameters

_notifiable

Notifiable

Returns

Record<string, unknown> | Promise<Record<string, unknown>>


toSlack()

toSlack(_notifiable): SlackMessage | Promise<SlackMessage>

Defined in: notifications/src/Notification.ts:68

Parameters

_notifiable

Notifiable

Returns

SlackMessage | Promise<SlackMessage>


toSms()

toSms(_notifiable): SmsMessage | Promise<SmsMessage>

Defined in: notifications/src/Notification.ts:72

Parameters

_notifiable

Notifiable

Returns

SmsMessage | Promise<SmsMessage>


toBroadcast()

toBroadcast(_notifiable): Record<string, unknown> | BroadcastMessage | Promise<Record<string, unknown> | BroadcastMessage>

Defined in: notifications/src/Notification.ts:80

The real-time representation for the 'broadcast' channel. Return a BroadcastMessage (or a plain data object). Required when channels() includes 'broadcast'.

Parameters

_notifiable

Notifiable

Returns

Record<string, unknown> | BroadcastMessage | Promise<Record<string, unknown> | BroadcastMessage>


broadcastType()

broadcastType(): string

Defined in: notifications/src/Notification.ts:90

The wire type of a broadcast notification (lets clients distinguish kinds). Default: class name.

Returns

string


payload()

payload(): Record<string, unknown>

Defined in: notifications/src/Notification.ts:111

Serialize this notification's state so it can be queued.

The default copies own enumerable fields, which covers the usual case of a constructor assigning plain values. Override it when the notification holds something JSON cannot carry — a model instance, a Date-keyed Map, a closure — and pair the override with a matching static fromPayload().

Returns

Record<string, unknown>

Example

override payload() {
  return { orderId: this.order.id };
}

static override async fromPayload(data: Record<string, unknown>) {
  return new OrderShipped(await Order.find(data.orderId as number));
}