Skip to content

Commit

Permalink
Allows hiding warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
albertor24 committed May 16, 2019
1 parent 8912bd0 commit c134ecd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/helpers/classes/mail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export interface MailData {

isMultiple?: boolean,
dynamicTemplateData?: { [key: string]: any },

hideWarnings?: boolean,
}

export interface MailJSON {
Expand Down Expand Up @@ -324,6 +326,11 @@ export default class Mail {
*/
setMailSettings(settings: MailSettings): void;

/**
* Set hide warnings
*/
setHideWarnings(hide: boolean): void;

/**
* To JSON
*/
Expand Down
28 changes: 23 additions & 5 deletions packages/helpers/classes/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Mail {

//Initialize array and object properties
this.isDynamic = false;
this.hideWarnings = false;
this.personalizations = [];
this.attachments = [];
this.content = [];
Expand Down Expand Up @@ -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
Expand All @@ -86,6 +88,7 @@ class Mail {
this.setAsm(asm);
this.setMailSettings(mailSettings);
this.setTrackingSettings(trackingSettings);
this.setHideWarnings(hideWarnings);

if (this.isDynamic) {
this.setDynamicTemplateData(dynamicTemplateData);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
*/
Expand Down
1 change: 1 addition & 0 deletions use-cases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions use-cases/hide-warnings.md
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
from: '[email protected]',
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);
```

0 comments on commit c134ecd

Please sign in to comment.