🤖 Merge PR #45272 [@types/email-templates] Export all interfaces and types and remove duplicated code by @ksewo

* change require to import, added emailOptions const to check if interfaces are correctly exported

* Export all interfaces and remove unnecessary interface which is duplicate of EmailTemplate class

* Disable NeedsExportEquals rule

* Added myself to definition contributors

* Restored old import = require syntax

* Added EmailTemplate namespace in which are stored all types, so the EmailTemplate class can be exported as CommonJS with typings.

* Removed unnecessary tslint rules
This commit is contained in:
ksewo 2020-07-02 23:57:55 +02:00 committed by GitHub
parent f41b0557a8
commit 9aedc1ffdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 153 additions and 176 deletions

View File

@ -52,7 +52,7 @@ const withTransportInstance = new EmailTemplates<Locals>({
withTransportInstance.render('tmpl', { firstName: 'TypeScript' });
withTransportInstance.send({
const emailOptions: EmailTemplates.EmailOptions<Locals> = {
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);

View File

@ -6,6 +6,7 @@
// Vesa Poikajärvi <https://github.com/vesse>
// Philipp Katz <https://github.com/qqilihq>
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
// ksewo <https://github.com/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<T = any> {
/**
* The message <Nodemailer.com/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 <https://github.com/ladjs/il8n>
*/
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<any>;
/**
* force text-only rendering of template (disregards template folder)
*/
textOnly?: boolean;
/**
* <Https://github.com/werk85/node-html-to-text>
*
* 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;
/**
* <https://github.com/Automattic/juice>
*/
juice?: boolean;
/**
* <https://github.com/Automattic/juice>
*/
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<T = any> {
/**
* The message <Nodemailer.com/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 <https://github.com/ladjs/il8n>
*/
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<any>;
/**
* force text-only rendering of template (disregards template folder)
*/
textOnly?: boolean;
/**
* <Https://github.com/werk85/node-html-to-text>
*
* 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;
/**
* <https://github.com/Automattic/juice>
*/
juice?: boolean;
/**
* <https://github.com/Automattic/juice>
*/
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<T = any> {
/**
* The template name
*/
template: string;
/**
* Nodemailer Message <Nodemailer.com/message/>
*
* Overrides what is given for constructor
*/
message: Mail.Options;
/**
* The Template Variables
*/
locals?: T;
}
interface EmailOptions<T = any> {
/**
* The template name
*/
template: string;
/**
* Nodemailer Message <Nodemailer.com/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<T = any> {
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<T = any> {
* @param view Name of the template
* @param locals The template variables
*/
renderAll(view: string, locals?: T): Promise<Partial<EmailMessage>>;
renderAll(view: string, locals?: T): Promise<Partial<EmailTemplate.EmailMessage>>;
/**
* Send the Email
*/
send(options: EmailOptions<T>): Promise<any>;
send(options: EmailTemplate.EmailOptions<T>): Promise<any>;
}
declare namespace EmailTemplate {
/**
* shorthand use of `juiceResources` with the config
* mainly for custom renders like from a database).
*/
function juiceResources(html: string): Promise<string>;
/**
*
* @param view The Html pug to render
* @param locals The template Variables
*/
function render(view: string, locals?: any): Promise<string>;
/**
* 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<Partial<EmailMessage>>;
/**
* Send the Email
*/
function send(options: EmailOptions): Promise<any>;
}
export = EmailTemplate;