types.ts 7.2 KB

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