From ff0e6eb19af0d2b7f22c064f1f08cd126595f8b3 Mon Sep 17 00:00:00 2001 From: Jacob Copeland Date: Thu, 26 Oct 2017 23:52:51 -0400 Subject: [PATCH 1/5] Updated type definitions for email-templates for v3.1 --- .../email-templates/email-templates-tests.ts | 37 +--- types/email-templates/index.d.ts | 204 ++++++------------ 2 files changed, 79 insertions(+), 162 deletions(-) diff --git a/types/email-templates/email-templates-tests.ts b/types/email-templates/email-templates-tests.ts index 3c4493402c..c36852de31 100644 --- a/types/email-templates/email-templates-tests.ts +++ b/types/email-templates/email-templates-tests.ts @@ -1,29 +1,14 @@ -import EmailTemplates = require('email-templates'); +import * as Email from 'email-templates'; -const EmailTemplate = EmailTemplates.EmailTemplate; -const template = new EmailTemplate("./"); -const templateWithOptions = new EmailTemplate('./', {disableJuice: true, sassOptions: {}, juiceOptions: {}}); -const users = [ - { - email: 'pappa.pizza@spaghetti.com', - name: { - first: 'Pappa', - last: 'Pizza' - } +const email = new Email.EmailTemplate({ + message: { + from: 'Test@tesitng.com' }, - { - email: 'mister.geppetto@spaghetti.com', - name: { - first: 'Mister', - last: 'Geppetto' - } - } -]; + transport: { + jsonTransport: true + }} +); -const templates = users.map((user) => { - return template.render(user) - .then((results) => { - const {html, subject, text} = results; - return html; - }); -}); +email.juiceResources('

bob

'); +email.render('mars/html.pug', {name: 'elon'}); +email.send({template: 'mars', message: {to: 'elon@spacex.com'}, locals: {name: 'Elon'}}); diff --git a/types/email-templates/index.d.ts b/types/email-templates/index.d.ts index d76c23aed3..bb6a3321c7 100644 --- a/types/email-templates/index.d.ts +++ b/types/email-templates/index.d.ts @@ -1,152 +1,84 @@ -// Type definitions for node-email-templates 2.6 +// Type definitions for node-email-templates 3.1 // Project: https://github.com/niftylettuce/node-email-templates // Definitions by: Cyril Schumacher // Matus Gura +// Jacob Copeland // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/** - * @summary Interface for result of email template. - * @interface - */ -interface EmailTemplateResults { +export interface EmailConfig { /** - * @summary HTML result. - * @type {string} + * The message */ - html: string; - + message: any; /** - * @summary Text result. - * @type {string} + * The nodemailer Transport created via nodemailer.createTransport */ - text: string; - + transport: any; /** - * @summary Subject result. - * @type {string} + * The email template directory and engine information */ - subject: string; + views?: any; + /** + * Do you really want to send, false for test or development + */ + send?: boolean; + /** + * Preview the email + */ + preview?: boolean; + /** + * Set to object to configure and Enable + */ + i18n?: any; + /** + * Pass a custom render function if necessary + */ + render?: { view: string, locals: any }; + /** + * + */ + htmlToText?: any; + /** + * + */ + juice?: boolean; + /** + * + */ + juiceResources?: any; } -/** - * @summary Callback signature. - */ -type EmailTemplateCallback = (err: any, results: EmailTemplateResults) => void; - -/** - * @summary Interface for email-template options - * @interface - */ -interface EmailTemplateOptions { - disableJuice?: boolean; - juiceOptions?: any; - sassOptions?: any; -} - -declare module "email-templates" { +export interface EmailOptions { /** - * @summary Email template class. - * @class + * The template name */ - class EmailTemplate { - /** - * @summary Constructor. - * @param {string} templateDir The template directory. - */ - constructor(templateDir: string, options?: EmailTemplateOptions); - - /** - * @summary Render a single template. - * @param locals The template variables. - * @param locale The language code. - */ - render(locals: any, locale?: string): Promise; - - /** - * @summary Render a single template. - * @param callback The callback function. - */ - render(callback: EmailTemplateCallback): void; - - /** - * @summary Render a single template. - * @param locals The template variables. - * @param callback The callback function. - */ - render(locals: any, callback: EmailTemplateCallback): void; - - /** - * @summary Render a single template. - * @param locals The template variables. - * @param locale The language code. - * @param callback The callback function. - */ - render(locals: any, locale: string, callback: EmailTemplateCallback): void; - - /** - * @summary Render text - * @param locals The template variables. - * @param locale The language code. - */ - renderText(locals: any, locale?: string): Promise; - - /** - * @summary Render text - * @param locals The template variables. - * @param callback The language code. - */ - renderText(locals: any, callback: EmailTemplateCallback): void; - - /** - * @summary Render text - * @param locals The template variables. - * @param locale The language code. - * @param callback The language code. - */ - renderText(locals: any, locale: string, callback: EmailTemplateCallback): void; - - /** - * @summary Render subject - * @param locals The template variables. - * @param locale The language code. - */ - renderSubject(locals: any, locale?: string): Promise; - - /** - * @summary Render subject - * @param locals The template variables. - * @param callback The language code. - */ - renderSubject(locals: any, callback: EmailTemplateCallback): void; - - /** - * @summary Render subject - * @param locals The template variables. - * @param locale The language code. - * @param callback The language code. - */ - renderSubject(locals: any, locale: string, callback: EmailTemplateCallback): void; - - /** - * @summary Render HTML - * @param locals The template variables. - * @param locale The language code. - */ - renderHtml(locals: any, locale?: string): Promise; - - /** - * @summary Render HTML - * @param locals The template variables. - * @param callback The language code. - */ - renderHtml(locals: any, callback: EmailTemplateCallback): void; - - /** - * @summary Render HTML - * @param locals The template variables. - * @param locale The language code. - * @param callback The language code. - */ - renderHtml(locals: any, locale: string, callback: EmailTemplateCallback): void; - } + template: string; + /** + * Nodemailer Message + */ + message: any; + /** + * The Template Variables + */ + locals: any; +} + +export class EmailTemplate { + constructor(config: EmailConfig); + + /** + * shorthand use of `juiceResources` with the config + * mainly for custom renders like from a database). + */ + juiceResources(html: string): Promise ; + + /** + * Render the Email, Used by send, but can be called by itself. + */ + render(view: string, locals: any): Promise; + + /** + * Send the Email. + */ + send(options: EmailOptions): any; } From 0c6218ec00d5b5ff06306c0e4d9b883d2ede0775 Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 27 Oct 2017 18:44:08 -0400 Subject: [PATCH 2/5] Updated for proper module export After reading today about module export and having a problem importing the definition, I updated to declare namespace and export. now import * as Email from 'email-templates' works with const email = new Email(etc) --- types/email-templates/index.d.ts | 36 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/types/email-templates/index.d.ts b/types/email-templates/index.d.ts index bb6a3321c7..12dceb181d 100644 --- a/types/email-templates/index.d.ts +++ b/types/email-templates/index.d.ts @@ -5,7 +5,7 @@ // Jacob Copeland // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -export interface EmailConfig { +interface EmailConfig { /** * The message */ @@ -48,7 +48,7 @@ export interface EmailConfig { juiceResources?: any; } -export interface EmailOptions { +interface EmailOptions { /** * The template name */ @@ -63,22 +63,42 @@ export interface EmailOptions { locals: any; } -export class EmailTemplate { +declare class EmailTemplate { constructor(config: EmailConfig); - /** * shorthand use of `juiceResources` with the config * mainly for custom renders like from a database). */ juiceResources(html: string): Promise ; - /** - * Render the Email, Used by send, but can be called by itself. + * + * @param view The Html pug to render + * @param locals The template Variables */ render(view: string, locals: any): Promise; - /** - * Send the Email. + * Send the Email */ send(options: EmailOptions): any; } + +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; + + /** + * Send the Email + */ + function send(options: EmailOptions): any; +} +export = EmailTemplate; From c6e432c71466572e2fedd3a35d56adfa985ab230 Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 27 Oct 2017 18:46:11 -0400 Subject: [PATCH 3/5] Updated test to reflect module export Correctly fixed the definition to use import * as Email from 'email-templates' --- types/email-templates/email-templates-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/email-templates/email-templates-tests.ts b/types/email-templates/email-templates-tests.ts index c36852de31..d16d0c0620 100644 --- a/types/email-templates/email-templates-tests.ts +++ b/types/email-templates/email-templates-tests.ts @@ -1,6 +1,6 @@ import * as Email from 'email-templates'; -const email = new Email.EmailTemplate({ +const email = new Email({ message: { from: 'Test@tesitng.com' }, From 8c196e90dfb50a6e3429fc7c615cbb6798a42073 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 28 Oct 2017 23:00:37 -0400 Subject: [PATCH 4/5] Updated per Reviewer Request changed import statement per reviewer's request. --- types/email-templates/email-templates-tests.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/types/email-templates/email-templates-tests.ts b/types/email-templates/email-templates-tests.ts index d16d0c0620..20401cb83e 100644 --- a/types/email-templates/email-templates-tests.ts +++ b/types/email-templates/email-templates-tests.ts @@ -1,6 +1,7 @@ -import * as Email from 'email-templates'; +import EmailTemplates = require('email-templates'); -const email = new Email({ + +const email = new EmailTemplates({ message: { from: 'Test@tesitng.com' }, From 239b0857a586c97f033ef958af851175f9888e23 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 28 Oct 2017 23:08:10 -0400 Subject: [PATCH 5/5] Removed extra whitespace Not sure how an extra line showed up. This should be good now for linting. --- types/email-templates/email-templates-tests.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/email-templates/email-templates-tests.ts b/types/email-templates/email-templates-tests.ts index 20401cb83e..a3311d6e81 100644 --- a/types/email-templates/email-templates-tests.ts +++ b/types/email-templates/email-templates-tests.ts @@ -1,6 +1,5 @@ import EmailTemplates = require('email-templates'); - const email = new EmailTemplates({ message: { from: 'Test@tesitng.com'