diff --git a/types/email-templates/email-templates-tests.ts b/types/email-templates/email-templates-tests.ts index 66d83457d0..fbab9dfcd1 100644 --- a/types/email-templates/email-templates-tests.ts +++ b/types/email-templates/email-templates-tests.ts @@ -52,7 +52,7 @@ const withTransportInstance = new EmailTemplates({ withTransportInstance.render('tmpl', { firstName: 'TypeScript' }); -withTransportInstance.send({ +const emailOptions: EmailTemplates.EmailOptions = { template: 'tmpl', locals: { firstName: 'TypeScript' @@ -64,7 +64,9 @@ withTransportInstance.send({ content: 'an attachment' }] } -}); +}; + +withTransportInstance.send(emailOptions); email.renderAll('mars'); const promise = email.renderAll('mars', locals); diff --git a/types/email-templates/index.d.ts b/types/email-templates/index.d.ts index 5634e6b1e1..212ace0b96 100644 --- a/types/email-templates/index.d.ts +++ b/types/email-templates/index.d.ts @@ -6,6 +6,7 @@ // Vesa Poikajärvi // Philipp Katz // Piotr Błażejewicz +// ksewo // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.3 @@ -20,165 +21,167 @@ import SMTPPool = require('nodemailer/lib/smtp-pool'); import SMTPTransport = require('nodemailer/lib/smtp-transport'); import StreamTransport = require('nodemailer/lib/stream-transport'); -// email-templates accepts nodemailer.createTransport options directly -// too and calls createTransport if given a non-function, thus a lot -// of different types accepted for transport -type NodeMailerTransportOptions = - | Mail - | SMTPPool - | SMTPPool.Options - | SendmailTransport - | SendmailTransport.Options - | StreamTransport - | StreamTransport.Options - | JSONTransport - | JSONTransport.Options - | SESTransport - | SESTransport.Options - | SMTPTransport - | SMTPTransport.Options - | string; +declare namespace EmailTemplate { + // email-templates accepts nodemailer.createTransport options directly + // too and calls createTransport if given a non-function, thus a lot + // of different types accepted for transport + type NodeMailerTransportOptions = + | Mail + | SMTPPool + | SMTPPool.Options + | SendmailTransport + | SendmailTransport.Options + | StreamTransport + | StreamTransport.Options + | JSONTransport + | JSONTransport.Options + | SESTransport + | SESTransport.Options + | SMTPTransport + | SMTPTransport.Options + | string; -// No typedef for https://github.com/niftylettuce/preview-email -interface PreviewEmailOpts { - /** - * a path to a directory for saving the generated email previews (defaults to os.tmpdir() - */ - dir?: string; + // No typedef for https://github.com/niftylettuce/preview-email + interface PreviewEmailOpts { + /** + * a path to a directory for saving the generated email previews (defaults to os.tmpdir() + */ + dir?: string; - /** - * https://github.com/sindresorhus/open - */ - open?: any; + /** + * https://github.com/sindresorhus/open + */ + open?: any; - /** - * a unique ID for the file name created for the preview in dir (defaults to uuid.v4() from uuid) - */ - id?: string; + /** + * a unique ID for the file name created for the preview in dir (defaults to uuid.v4() from uuid) + */ + id?: string; - /** - * a file path to a pug template file (defaults to preview-email's template.pug by default) - */ - template?: string; -} + /** + * a file path to a pug template file (defaults to preview-email's template.pug by default) + */ + template?: string; + } -interface ViewOptions { - /** - * View extansion. defaults to 'pug', and is the default file extension for templates - */ - extension?: string; + interface ViewOptions { + /** + * View extansion. defaults to 'pug', and is the default file extension for templates + */ + extension?: string; - /** - * a template file extension mapping, defaults to { hbs: 'handlebars', njk: 'nunjucks' } - * (this is useful if you use different file extension naming conventions) - */ - map?: any; + /** + * a template file extension mapping, defaults to { hbs: 'handlebars', njk: 'nunjucks' } + * (this is useful if you use different file extension naming conventions) + */ + map?: any; - /** - * the default template engine source, defaults to consolidate - */ - engineSource?: any; -} + /** + * the default template engine source, defaults to consolidate + */ + engineSource?: any; + } -interface View { - /** - * View root. Defaults to the current working directory's "emails" folder via path.resolve('emails') - */ - root?: string; + interface View { + /** + * View root. Defaults to the current working directory's "emails" folder via path.resolve('emails') + */ + root?: string; - options?: ViewOptions; -} + options?: ViewOptions; + } -interface EmailConfig { - /** - * The message - */ - message: Mail.Options; - /** - * The nodemailer Transport created via nodemailer.createTransport - */ - transport?: NodeMailerTransportOptions; - /** - * The email template directory and engine information - */ - views?: View; - /** - * Do you really want to send, false for test or development - */ - send?: boolean; - /** - * Preview the email - */ - preview?: boolean | PreviewEmailOpts; - /** - * Set to object to configure and Enable - */ - i18n?: any; - /** - * defaults to false, unless you pass your own render function, - * and in that case it will be automatically set to true. - * @default false - */ - customRender?: boolean; - /** - * Pass a custom render function if necessary - */ - render?: (view: string, locals?: T) => Promise; - /** - * force text-only rendering of template (disregards template folder) - */ - textOnly?: boolean; - /** - * - * - * configuration object for html-to-text - */ - htmlToText?: HtmlToTextOptions | false; - /** - * You can pass an option to prefix subject lines with a string - * env === 'production' ? false : `[${env.toUpperCase()}] `; // <--- HERE - */ - subjectPrefix?: string | false; - /** - * - */ - juice?: boolean; - /** - * - */ - juiceResources?: any; - /** - * a function that returns the path to a template file - * @default (path: string, template: string) => string - */ - getPath?: (path: string, template: string, locals: any) => string; -} + interface EmailConfig { + /** + * The message + */ + message: Mail.Options; + /** + * The nodemailer Transport created via nodemailer.createTransport + */ + transport?: NodeMailerTransportOptions; + /** + * The email template directory and engine information + */ + views?: View; + /** + * Do you really want to send, false for test or development + */ + send?: boolean; + /** + * Preview the email + */ + preview?: boolean | PreviewEmailOpts; + /** + * Set to object to configure and Enable + */ + i18n?: any; + /** + * defaults to false, unless you pass your own render function, + * and in that case it will be automatically set to true. + * @default false + */ + customRender?: boolean; + /** + * Pass a custom render function if necessary + */ + render?: (view: string, locals?: T) => Promise; + /** + * force text-only rendering of template (disregards template folder) + */ + textOnly?: boolean; + /** + * + * + * configuration object for html-to-text + */ + htmlToText?: HtmlToTextOptions | false; + /** + * You can pass an option to prefix subject lines with a string + * env === 'production' ? false : `[${env.toUpperCase()}] `; // <--- HERE + */ + subjectPrefix?: string | false; + /** + * + */ + juice?: boolean; + /** + * + */ + juiceResources?: any; + /** + * a function that returns the path to a template file + * @default (path: string, template: string) => string + */ + getPath?: (path: string, template: string, locals: any) => string; + } -interface EmailOptions { - /** - * The template name - */ - template: string; - /** - * Nodemailer Message - * - * Overrides what is given for constructor - */ - message: Mail.Options; - /** - * The Template Variables - */ - locals?: T; -} + interface EmailOptions { + /** + * The template name + */ + template: string; + /** + * Nodemailer Message + * + * Overrides what is given for constructor + */ + message: Mail.Options; + /** + * The Template Variables + */ + locals?: T; + } -interface EmailMessage { - subject: string; - html: string; - text: string; + interface EmailMessage { + subject: string; + html: string; + text: string; + } } declare class EmailTemplate { - constructor(config: EmailConfig); + constructor(config: EmailTemplate.EmailConfig); /** * shorthand use of `juiceResources` with the config * mainly for custom renders like from a database). @@ -197,39 +200,11 @@ declare class EmailTemplate { * @param view Name of the template * @param locals The template variables */ - renderAll(view: string, locals?: T): Promise>; + renderAll(view: string, locals?: T): Promise>; /** * Send the Email */ - send(options: EmailOptions): Promise; + send(options: EmailTemplate.EmailOptions): Promise; } -declare namespace EmailTemplate { - /** - * shorthand use of `juiceResources` with the config - * mainly for custom renders like from a database). - */ - function juiceResources(html: string): Promise; - - /** - * - * @param view The Html pug to render - * @param locals The template Variables - */ - function render(view: string, locals?: any): Promise; - - /** - * Render all available template files for a given email - * template (e.g. `html.pug`, `text.pug`, and `subject.pug`) - * - * @param view Name of the template - * @param locals The template variables - */ - function renderAll(view: string, locals?: any): Promise>; - - /** - * Send the Email - */ - function send(options: EmailOptions): Promise; -} export = EmailTemplate;