types.ts 6.1 KB

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