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 default data to be sent on all requests #1169

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/helpers/classes/mail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface MailData {
cc?: EmailData|EmailData[],
bcc?: EmailData|EmailData[],

from: EmailData,
from?: EmailData,
childish-sambino marked this conversation as resolved.
Show resolved Hide resolved
replyTo?: EmailData,

sendAt?: number,
Expand Down
3 changes: 2 additions & 1 deletion packages/mail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"dependencies": {
"@sendgrid/client": "^7.2.2",
"@sendgrid/helpers": "^7.2.0"
"@sendgrid/helpers": "^7.2.0",
"deepmerge": "^4.2.2"
childish-sambino marked this conversation as resolved.
Show resolved Hide resolved
},
"tags": [
"http",
Expand Down
18 changes: 17 additions & 1 deletion packages/mail/src/classes/mail-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
const {Client} = require('@sendgrid/client');
const {classes: {Mail}} = require('@sendgrid/helpers');
const deepmerge = require('deepmerge');

/**
* Mail service class
Expand All @@ -20,6 +21,7 @@ class MailService {
this.setClient(new Client());
this.setSubstitutionWrappers('{{', '}}');
this.secretRules = [];
this.defaultData = {};
}

/**
Expand Down Expand Up @@ -110,6 +112,17 @@ class MailService {
});
}

/**
* Set default data to for all requests. Can be overwritten by incoming data on send
*/
setDefaultData(data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example usage also needed.

if (Object.prototype.toString.call(data) !== "[object Object]") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking data is an object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to throw an exception if it's not an object instead of just returning.

return;
}

this.defaultData = data;
}

/**
* Check if the e-mail is safe to be sent
*/
Expand Down Expand Up @@ -184,8 +197,11 @@ class MailService {
data.substitutionWrappers = this.substitutionWrappers;
}

//Deep merge default data
const dataWithDefaults = deepmerge(this.defaultData, data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests needed to ensure the default data is actually getting merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need some help on testing, documentation is not very clear to me https://github.com/sendgrid/sendgrid-nodejs/blob/main/CONTRIBUTING.md#testing
Seems there is no prism:install and prism command. Is there a reason why this is not on devDependencies?
Thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing section updated with latest instructions.


//Create Mail instance from data and get JSON body for request
const mail = Mail.create(data);
const mail = Mail.create(dataWithDefaults);
const body = mail.toJSON();

//Filters the Mail body to avoid sensitive content leakage
Expand Down
5 changes: 5 additions & 0 deletions packages/mail/src/mail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ declare class MailService {
*/
setSubstitutionWrappers(left: string, right: string): void;

/**
* Set default data to for all requests. Can be overwritten by incoming data on send
*/
setDefaultData(data: Partial<MailDataRequired>): void;

/**
* Send email
*/
Expand Down