types.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { Omit } from '@vendure/common/lib/omit';
  2. import { Injector, RequestContext, VendureEvent, WorkerMessage } from '@vendure/core';
  3. import { EmailEventHandler } from './event-handler';
  4. /**
  5. * @description
  6. * A VendureEvent which also includes a `ctx` property containing the current
  7. * {@link RequestContext}, which is used to determine the channel and language
  8. * to use when generating the email.
  9. *
  10. * @docsCategory EmailPlugin
  11. * @docsPage Email Plugin Types
  12. */
  13. export type EventWithContext = VendureEvent & { ctx: RequestContext };
  14. /**
  15. * @description
  16. * A VendureEvent with a {@link RequestContext} and a `data` property which contains the
  17. * value resolved from the {@link EmailEventHandler}`.loadData()` callback.
  18. *
  19. * @docsCategory EmailPlugin
  20. * @docsPage Email Plugin Types
  21. */
  22. export type EventWithAsyncData<Event extends EventWithContext, R> = Event & { data: R };
  23. /**
  24. * @description
  25. * Configuration for the EmailPlugin.
  26. *
  27. * @docsCategory EmailPlugin
  28. * @docsPage EmailPluginOptions
  29. * */
  30. export interface EmailPluginOptions {
  31. /**
  32. * @description
  33. * The path to the location of the email templates. In a default Vendure installation,
  34. * the templates are installed to `<project root>/vendure/email/templates`.
  35. */
  36. templatePath: string;
  37. /**
  38. * @description
  39. * Configures how the emails are sent.
  40. */
  41. transport: EmailTransportOptions;
  42. /**
  43. * @description
  44. * An array of {@link EmailEventHandler}s which define which Vendure events will trigger
  45. * emails, and how those emails are generated.
  46. */
  47. handlers: EmailEventHandler[];
  48. /**
  49. * @description
  50. * An object containing variables which are made available to all templates. For example,
  51. * the storefront URL could be defined here and then used in the "email address verification"
  52. * email.
  53. */
  54. globalTemplateVars?: { [key: string]: any };
  55. }
  56. /**
  57. * @description
  58. * Configuration for running the EmailPlugin in development mode.
  59. *
  60. * @docsCategory EmailPlugin
  61. * @docsPage EmailPluginOptions
  62. */
  63. export interface EmailPluginDevModeOptions extends Omit<EmailPluginOptions, 'transport'> {
  64. devMode: true;
  65. /**
  66. * @description
  67. * The path to which html email files will be saved rather than being sent.
  68. */
  69. outputPath: string;
  70. /**
  71. * @description
  72. * If set, a "mailbox" server will be started which will serve the contents of the
  73. * `outputPath` similar to a web-based email client, available at the route `/mailbox`,
  74. * e.g. http://localhost:3000/mailbox.
  75. */
  76. mailboxPort?: number;
  77. }
  78. /**
  79. * @description
  80. * The credentials used for sending email via SMTP
  81. *
  82. * @docsCategory EmailPlugin
  83. * @docsPage Email Plugin Types
  84. */
  85. export interface SMTPCredentials {
  86. /** @description The username */
  87. user: string;
  88. /** @description The password */
  89. pass: string;
  90. }
  91. /**
  92. * @description
  93. * A union of all the possible transport options for sending emails.
  94. *
  95. * @docsCategory EmailPlugin
  96. * @docsPage Transport Options
  97. */
  98. export type EmailTransportOptions =
  99. | SMTPTransportOptions
  100. | SendmailTransportOptions
  101. | FileTransportOptions
  102. | NoopTransportOptions
  103. | TestingTransportOptions;
  104. /**
  105. * @description
  106. * A subset of the SMTP transport options of [Nodemailer](https://nodemailer.com/smtp/)
  107. *
  108. * @docsCategory EmailPlugin
  109. * @docsPage Transport Options
  110. */
  111. export interface SMTPTransportOptions {
  112. type: 'smtp';
  113. /**
  114. * @description
  115. * the hostname or IP address to connect to (defaults to ‘localhost’)
  116. */
  117. host: string;
  118. /**
  119. * @description
  120. * The port to connect to (defaults to 25 or 465)
  121. */
  122. port: number;
  123. /**
  124. * @description
  125. * Defines authentication data
  126. */
  127. auth: SMTPCredentials;
  128. /**
  129. * @description
  130. * Defines if the connection should use SSL (if true) or not (if false)
  131. */
  132. secure?: boolean;
  133. /**
  134. * @description
  135. * Turns off STARTTLS support if true
  136. */
  137. ignoreTLS?: boolean;
  138. /**
  139. * @description
  140. * Forces the client to use STARTTLS. Returns an error if upgrading the connection is not possible or fails.
  141. */
  142. requireTLS?: boolean;
  143. /**
  144. * @description
  145. * Optional hostname of the client, used for identifying to the server
  146. */
  147. name?: string;
  148. /**
  149. * @description
  150. * The local interface to bind to for network connections
  151. */
  152. localAddress?: string;
  153. /**
  154. * @description
  155. * Defines preferred authentication method, e.g. ‘PLAIN’
  156. */
  157. authMethod?: string;
  158. /**
  159. * @description
  160. * If true, uses the configured {@link VendureLogger} to log messages from Nodemailer as it interacts with
  161. * the SMTP server.
  162. *
  163. * @default false
  164. */
  165. logging?: boolean;
  166. /**
  167. * @description
  168. * If set to true, then logs SMTP traffic without message content.
  169. *
  170. * @default false
  171. */
  172. transactionLog?: boolean;
  173. /**
  174. * @description
  175. * If set to true, then logs SMTP traffic and message content, otherwise logs only transaction events.
  176. *
  177. * @default false
  178. */
  179. debug?: boolean;
  180. }
  181. /**
  182. * @description
  183. * Uses the local Sendmail program to send the email.
  184. *
  185. * @docsCategory EmailPlugin
  186. * @docsPage Transport Options
  187. */
  188. export interface SendmailTransportOptions {
  189. type: 'sendmail';
  190. /** path to the sendmail command (defaults to ‘sendmail’) */
  191. path?: string;
  192. /** either ‘windows’ or ‘unix’ (default). Forces all newlines in the output to either use Windows syntax <CR><LF> or Unix syntax <LF> */
  193. newline?: string;
  194. }
  195. /**
  196. * @description
  197. * Outputs the email as an HTML file for development purposes.
  198. *
  199. * @docsCategory EmailPlugin
  200. * @docsPage Transport Options
  201. */
  202. export interface FileTransportOptions {
  203. type: 'file';
  204. /** The directory in which the emails will be saved */
  205. outputPath: string;
  206. /** When set to true, a raw text file will be output rather than an HTML file */
  207. raw?: boolean;
  208. }
  209. /**
  210. * @description
  211. * Does nothing with the generated email. Mainly intended for use in testing where we don't care about the email transport.
  212. *
  213. * @docsCategory EmailPlugin
  214. * @docsPage Transport Options
  215. */
  216. export interface NoopTransportOptions {
  217. type: 'none';
  218. }
  219. /**
  220. * @description
  221. * The final, generated email details to be sent.
  222. *
  223. * @docsCategory EmailPlugin
  224. * @docsPage Email Plugin Types
  225. */
  226. export interface EmailDetails {
  227. from: string;
  228. recipient: string;
  229. subject: string;
  230. body: string;
  231. }
  232. /**
  233. * @description
  234. * Forwards the raw GeneratedEmailContext object to a provided callback, for use in testing.
  235. *
  236. * @docsCategory EmailPlugin
  237. * @docsPage Transport Options
  238. */
  239. export interface TestingTransportOptions {
  240. type: 'testing';
  241. /**
  242. * @description
  243. * Callback to be invoked when an email would be sent.
  244. */
  245. onSend: (details: EmailDetails) => void;
  246. }
  247. /**
  248. * @description
  249. * An EmailGenerator generates the subject and body details of an email.
  250. */
  251. export interface EmailGenerator<T extends string = any, E extends VendureEvent = any> {
  252. /**
  253. * @description
  254. * Any necessary setup can be performed here.
  255. */
  256. onInit?(options: EmailPluginOptions): void | Promise<void>;
  257. /**
  258. * @description
  259. * Given a subject and body from an email template, this method generates the final
  260. * interpolated email text.
  261. */
  262. generate(
  263. from: string,
  264. subject: string,
  265. body: string,
  266. templateVars: { [key: string]: any },
  267. ): Omit<EmailDetails, 'recipient'>;
  268. }
  269. /**
  270. * @description
  271. * A function used to load async data for use by an {@link EmailEventHandler}.
  272. *
  273. * @docsCategory EmailPlugin
  274. */
  275. export type LoadDataFn<Event extends EventWithContext, R> = (context: {
  276. event: Event;
  277. injector: Injector;
  278. }) => Promise<R>;
  279. export type IntermediateEmailDetails = {
  280. type: string;
  281. from: string;
  282. recipient: string;
  283. templateVars: any;
  284. subject: string;
  285. templateFile: string;
  286. };
  287. export class EmailWorkerMessage extends WorkerMessage<IntermediateEmailDetails, boolean> {
  288. static readonly pattern = 'send-email';
  289. }