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

Conversation

seromenho
Copy link
Contributor

@seromenho seromenho commented Jul 31, 2020

Resolves: #1128
👋
With this PR you can set default data you would like to send on each request. Eg: sandbox mode, from, etc..
I think this would be helpful, let me know what do you think.

Usage

sgMail.setApiKey(config.get('SENDGRID.API_KEY'))
sgMail.setDefaultData({
  from: '[email protected]',
  mailSettings:  {
    sandboxMode: {
      enable: config.get('NODE_ENV') !== 'production'
    }
  }
});

Notes

  1. Naming might not be the best, please let me know.
  2. Used deepmerge but I've seen there's merge-data-deep on helpers although not used and there's also deepmerge dependency on helpers.
  3. If you are willing to accept this the documentation and tests can come after 😬

Checklist

  • I acknowledge that all my contributions will be made under the project's license
  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the Contribution Guidelines and my PR follows them
  • I have titled the PR appropriately
  • I have updated my branch with the master branch
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation about the functionality in the appropriate .md file
  • I have added inline documentation to the code I modified

@thinkingserious thinkingserious added the status: code review request requesting a community code review or review from Twilio label Jul 31, 2020
@seromenho seromenho force-pushed the feature/default-data branch from 4ab0e55 to bfb7a14 Compare July 31, 2020 04:07
@eshanholtz eshanholtz changed the title Feature: Allow default data to be sent on all requests feat: Allow default data to be sent on all requests Aug 4, 2020
* Set default data to for all requests. Can be overwritten by incoming data on send
*/
setDefaultData(data) {
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.

@seromenho
Copy link
Contributor Author

seromenho commented Aug 5, 2020

Btw, I was looking for something like this because I was trying to create a wrapper to the .send with some defaults. My issue was.... typescript 😅 It was being a bit tricky create this wrapper with typescript because both signatures have 3 params. It is picking the last signature send method where ideally, for my usage, I would like to have them combined or use the first one. (Works fine though using the second signature)

@seromenho
Copy link
Contributor Author

Just created #1172 and this one might not be needed.

@childish-sambino
Copy link
Contributor

@seromenho I'm approving and merging #1172. Do you still want/need these changes?

@seromenho
Copy link
Contributor Author

@childish-sambino Thank you. With #1172 I can make my own wrapper instead.
I saw #1128 was also requesting for something like this. I'll leave it to you to decide if this add some value or not.

packages/mail/package.json Show resolved Hide resolved
* Set default data to for all requests. Can be overwritten by incoming data on send
*/
setDefaultData(data) {
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.

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

@@ -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.

/**
* 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.

@childish-sambino
Copy link
Contributor

Closing due to inactivity. Please ping here if work is to continue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: code review request requesting a community code review or review from Twilio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable sandbox mode for all requests
4 participants