How to Create a Notification Provider Module
In this document, you’ll learn how to create a notification provider module and the methods you must implement in it.
1. Create Module Directory#
Start by creating a new directory for your module. For example, src/modules/my-notification
.
2. Create the Notification Provider Service#
Create the file src/modules/my-notification/service.ts
that holds the implementation of the notification service.
The Notification Provider Module's main service must extend the AbstractNotificationProviderService
class imported from @medusajs/utils
:
constructor#
The constructor allows you to access resources from the module's container using the first parameter, and the module's options using the second parameter.
If you're creating a client or establishing a connection with a third-party service, do it in the constructor.
Example
1import { AbstractNotificationProviderService } from "@medusajs/utils"2import { Logger } from "@medusajs/types"3 4type InjectedDependencies = {5 logger: Logger6}7 8type Options = {9 apiKey: string10}11 12class MyNotificationProviderService extends AbstractNotificationProviderService {13 protected logger_: Logger14 protected options_: Options15 // assuming you're initializing a client16 protected client17 18 constructor (19 { logger }: InjectedDependencies,20 options: Options21 ) {22 super()23 24 this.logger_ = logger25 this.options_ = options26 27 // assuming you're initializing a client28 this.client = new Client(options)29 }30}31 32export default MyNotificationProviderService
constructor#
validateOptions#
Override this static method in order for the loader to validate the options provided to the module provider.
Parameters
options
Record<any, any>Returns
void
voidsend#
This method is used to send a notification using the third-party provider or your custom logic.
Example
1// other imports...2import {3 ProviderSendNotificationDTO,4 ProviderSendNotificationResultsDTO5} from "@medusajs/types"6 7class MyNotificationProviderService extends AbstractNotificationProviderService {8 // ...9 async send(10 notification: ProviderSendNotificationDTO11 ): Promise<ProviderSendNotificationResultsDTO> {12 // TODO send the notification using a third-party13 // provider or custom logic.14 // for example:15 return this.client.send({16 email: notification.to,17 template: notification.template,18 template_data: notification.data19 })20 }21}
Parameters
notification
ProviderSendNotificationDTOThe details of the
notification to send.
notification
ProviderSendNotificationDTOReturns
Promise
Promise<ProviderSendNotificationResultsDTO>The result of sending
the notification.
Promise
Promise<ProviderSendNotificationResultsDTO>3. Create Module Definition File#
Create the file src/modules/my-notification/index.ts
with the following content:
This exports the module's definition, indicating that the MyNotificationProviderService
is the module's service.
4. Use Module#
To use your Notification Module Provider, add it to the providers
array of the Notification Module:
1const { Modules } = require("@medusajs/utils")2 3// ...4 5module.exports = defineConfig({6 // ...7 modules: {8 [Modules.NOTIFICATION]: {9 resolve: "@medusajs/notification",10 options: {11 providers: [12 {13 resolve: "./modules/my-notification",14 id: "my-notification",15 options: {16 channels: ["email"],17 // provider options...18 },19 },20 ],21 },22 },23 }24})