Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow hiding warnings #932

Merged
merged 1 commit into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
```