diff --git a/packages/helpers/classes/mail.d.ts b/packages/helpers/classes/mail.d.ts index 43b316663..329c96ebb 100644 --- a/packages/helpers/classes/mail.d.ts +++ b/packages/helpers/classes/mail.d.ts @@ -153,6 +153,8 @@ export interface MailData { isMultiple?: boolean, dynamicTemplateData?: { [key: string]: any }, + + hideWarnings?: boolean, } export interface MailJSON { @@ -324,6 +326,11 @@ export default class Mail { */ setMailSettings(settings: MailSettings): void; + /** + * Set hide warnings + */ + setHideWarnings(hide: boolean): void; + /** * To JSON */ diff --git a/packages/helpers/classes/mail.js b/packages/helpers/classes/mail.js index b6f83321b..ca49afb72 100644 --- a/packages/helpers/classes/mail.js +++ b/packages/helpers/classes/mail.js @@ -23,6 +23,7 @@ class Mail { //Initialize array and object properties this.isDynamic = false; + this.hideWarnings = false; this.personalizations = []; this.attachments = []; this.content = []; @@ -66,6 +67,7 @@ class Mail { templateId, personalizations, attachments, ipPoolName, batchId, sections, headers, categories, category, customArgs, asm, mailSettings, trackingSettings, substitutions, substitutionWrappers, dynamicTemplateData, isMultiple, + hideWarnings, } = data; //Set data @@ -86,6 +88,7 @@ class Mail { this.setAsm(asm); this.setMailSettings(mailSettings); this.setTrackingSettings(trackingSettings); + this.setHideWarnings(hideWarnings); if (this.isDynamic) { this.setDynamicTemplateData(dynamicTemplateData); @@ -339,11 +342,13 @@ class Mail { } // Check dynamic template for non-escaped characters and warn if found - Object.values(dynamicTemplateData).forEach(value => { - if (/['"&]/.test(value)) { - console.warn(DYNAMIC_TEMPLATE_CHAR_WARNING); - } - }); + if (!this.hideWarnings) { + Object.values(dynamicTemplateData).forEach(value => { + if (/['"&]/.test(value)) { + console.warn(DYNAMIC_TEMPLATE_CHAR_WARNING); + } + }); + } this.dynamicTemplateData = dynamicTemplateData; } @@ -531,6 +536,19 @@ class Mail { this.mailSettings = settings; } + /** + * Set hide warnings + */ + setHideWarnings(hide) { + if (typeof hide === 'undefined') { + return; + } + if (typeof hide !== 'boolean') { + throw new Error('Boolean expected for `hideWarnings`'); + } + this.hideWarnings = hide; + } + /** * To JSON */ diff --git a/use-cases/README.md b/use-cases/README.md index 6609c8d26..75b88287d 100644 --- a/use-cases/README.md +++ b/use-cases/README.md @@ -11,6 +11,7 @@ This documentation provides examples for specific Twilio SendGrid v3 API use cas * [Advanced Usage](https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/advanced.md) * [Transactional Templates](https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/transactional-templates.md) * [Legacy Transactional Templates](https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/transactional-legacy-templates.md) + * [Hide Warnings](https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/hide-warnings.md) * [Attachments](https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/attachments.md) * [Customization Per Recipient](https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/customization.md) * [Manually Providing Content](https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/manual-content.md) diff --git a/use-cases/hide-warnings.md b/use-cases/hide-warnings.md new file mode 100644 index 000000000..e0e2839d8 --- /dev/null +++ b/use-cases/hide-warnings.md @@ -0,0 +1,23 @@ +# Hide Warnings + +When using dynamic templates, if one of the values in the template data contains a single quote, a double quote or an ampersand, a warning will be logged. + +To hide this warning, set `hideWarnings` to `true` in the message. + +```js +const sgMail = require('@sendgrid/mail'); +sgMail.setApiKey(process.env.SENDGRID_API_KEY); +const msg = { + to: 'recipient@example.org', + from: 'sender@example.org', + templateId: 'd-f43daeeaef504760851f727007e0b5d0', + dynamic_template_data: { + subject: 'Testing Templates', + name: 'Some One', + city: 'Denver', + company: 'Recipient & Sender' + }, + hideWarnings: true // now the warning won't be logged +}; +sgMail.send(msg); +``` \ No newline at end of file