1 import { INotificationProvider, NotificationTypes } from "@medusajs/types"
2
3 /**
4 * ### constructor
5 *
6 * The constructor allows you to access resources from the module's container using the first parameter,
7 * and the module's options using the second parameter.
8 *
9 * If you're creating a client or establishing a connection with a third-party service, do it in the constructor.
10 *
11 * #### Example
12 *
13 * ```ts
14 * import { AbstractNotificationProviderService } from "@medusajs/framework/utils"
15 * import { Logger } from "@medusajs/framework/types"
16 *
17 * type InjectedDependencies = {
18 * logger: Logger
19 * }
20 *
21 * type Options = {
22 * apiKey: string
23 * }
24 *
25 * class MyNotificationProviderService extends AbstractNotificationProviderService {
26 * protected logger_: Logger
27 * protected options_: Options
28 * // assuming you're initializing a client
29 * protected client
30 *
31 * constructor (
32 * { logger }: InjectedDependencies,
33 * options: Options
34 * ) {
35 * super()
36 *
37 * this.logger_ = logger
38 * this.options_ = options
39 *
40 * // assuming you're initializing a client
41 * this.client = new Client(options)
42 * }
43 * }
44 *
45 * export default MyNotificationProviderService
46 * ```
47 */
48 export class AbstractNotificationProviderService
49 implements INotificationProvider
50 {
51 /**
52 * Each notification provider has a unique ID used to identify it.
53 *
54 * @example
55 * class MyNotificationProviderService extends AbstractNotificationProviderService {
56 * static identifier = "my-notification"
57 * // ...
58 * }
59 */
60 static identifier: string
61 /**
62 * This method validates the options of the provider set in `medusa-config.ts`.
63 * Implementing this method is optional. It's useful if your provider requires custom validation.
64 *
65 * If the options aren't valid, throw an error.
66 *
67 * @param options - The provider's options.
68 *
69 * @example
70 * class MyNotificationProviderService extends AbstractNotificationProviderService {
71 * static validateOptions(options: Record<any, any>) {
72 * if (!options.apiKey) {
73 * throw new MedusaError(
74 * MedusaError.Types.INVALID_DATA,
75 * "API key is required in the provider's options."
76 * )
77 * }
78 * }
79 * }
80 */
81 static validateOptions(options: Record<any, any>): void | never {}
82
83 /**
84 * This method is used to send a notification using the third-party provider or your custom logic.
85 *
86 * @param {NotificationTypes.ProviderSendNotificationDTO} notification - The details of the
87 * notification to send.
88 * @returns {Promise<NotificationTypes.ProviderSendNotificationResultsDTO>} The result of sending
89 * the notification.
90 *
91 * @example
92 * // other imports...
93 * import {
94 * ProviderSendNotificationDTO,
95 * ProviderSendNotificationResultsDTO
96 * } from "@medusajs/framework/types"
97 *
98 * class MyNotificationProviderService extends AbstractNotificationProviderService {
99 * // ...
100 * async send(
101 * notification: ProviderSendNotificationDTO
102 * ): Promise<ProviderSendNotificationResultsDTO> {
103 * // TODO send the notification using a third-party
104 * // provider or custom logic.
105 * // for example:
106 * return this.client.send({
107 * email: notification.to,
108 * template: notification.template,
109 * template_data: notification.data
110 * })
111 * }
112 * }
113 */
114 async send(
115 notification: NotificationTypes.ProviderSendNotificationDTO
116 ): Promise<NotificationTypes.ProviderSendNotificationResultsDTO> {
117 throw Error(
118 `send is not implemented in ${
119 Object.getPrototypeOf(this).constructor.name
120 }`
121 )
122 }
123 }