-
Notifications
You must be signed in to change notification settings - Fork 780
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
Promise API and consistent callback function signature #261
Conversation
@thinkingserious can you check why the build is failing? I haven't made any API changes in this PR yet, so everything should still be working, yet all the tests seem to fail on an incorrect status code. |
Ah, since you are not part of the SendGrid repo, Travis is not loading the MOCK_HOST variable. I'll send instructions on how to create the test environment locally. |
Yes I figured that's why the tests would be failing locally, but on Travis it should still work, no? |
To test locally:
This will set up a mock local SendGrid server. When you run your tests locally they should pass. For Node.js, we have have not automated this yet on Travis (it's coming), so it relies on their hosted server, whose unique URL is hidden behind the MOCK_HOST variable. Travis will only load that variable for those in the sengrid repo. |
Gotcha. I have added the changes to the API here. I might actually have to update the tests, as the callback now receives parameters in different order as per Node conventions. Notable changes:
Example usage: //Build request
let request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
//Send request (returns promise)
sg.API(request)
.then(response => ...)
.catch(error => ...);
//Send request (using callback)
sg.API(request, (error, response) => {
if (error) {
//boo
}
//response always populated, even with error
}); |
This is awesome! Can you please allow us to send you some swag? Just email [email protected] with a mailing address and T-shirt size. |
Sure will do, thanks :) I've just pushed another commit which updates the tests and examples to use the new function signature. As this technically is a breaking change from the current version, you might want to release this under a new major version. However, as it could also be considered a "fix" to adhere to Node standards, maybe you can get away with a minor release as well. But I'll leave that up to you :) Let me know if there's anything else. |
Have run the tests locally with Prism and all pass 👍 |
Awesome, love it! I've gotten this into our backlog for review and merge. Unfortunately, we have quite a few tickets in front of it, but this issue can move up our queue if others +1 or add comments. |
@thinkingserious I've updated the PR title and description to reflect what has changed. I think I might also remove the redundant Mind if we just export the main let sendgrid = require('sendgrid');
let sg = sendgrid(process.env.SENDGRID_API_KEY); Instead of the more verbose: let sendgrid = require('sendgrid');
let sg = sendgrid.SendGrid(process.env.SENDGRID_API_KEY); |
Beautiful and thanks again! |
Ok, done. I've also updated the The |
+1 LGTM |
@jamesdixon thanks for the vote, I've added your vote to help move this up the queue. |
+1 |
@adambuczynski, I'm now reviewing this pull request :) So far, I've just tried to run the tests ( /Users/thinkingserious/Workspace/sendgrid/sendgrid-nodejs/lib/sendgrid.js:78
this.API = function(request, callback) {
^
TypeError: Cannot set property 'API' of undefined
at SendGrid (/Users/thinkingserious/Workspace/sendgrid/sendgrid-nodejs/lib/sendgrid.js:78:12)
at Suite.<anonymous> (/Users/thinkingserious/Workspace/sendgrid/sendgrid-nodejs/test/test.js:13:12)
at context.describe.context.context (/usr/local/lib/node_modules/mocha/lib/interfaces/bdd.js:47:10)
at Object.<anonymous> (/Users/thinkingserious/Workspace/sendgrid/sendgrid-nodejs/test/test.js:4:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at /usr/local/lib/node_modules/mocha/lib/mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:216:14)
at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:468:10)
at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:142:18)
at node.js:939:3 |
@thinkingserious interesting, I didn't make any changes to the architectural style of that core function, the Also, I didn't run into this error running the tests. But I can refactor it to hide the |
@adambuczynski, How did you run the tests? |
Via the way you suggested above. However, I tried again and have been able to reproduce it now as well. I think it's related to the way the I have addressed this by referencing the Tests are working and passing now. |
That did the trick :) |
Excellent, let me know if anything else comes up. |
Great, thanks for merging. How are you going to version these changes? |
I'm working on that right now. This will be a major version bump to 4.0. Should be done in about ~10 min. |
Ok sounds sensible. Would you like to release it as a RC first and see if we can make other improvements that might be API breaking? That way we could include them without having to do another major release in the future. |
We don't currently have a RC workflow, but that sounds interesting. For now, we just make major version bumps when something breaks. We follow the semver.org convention. |
@adambuczynski oops, looks like we forget to remove the let's for var's. We have some breakage on the older node versions: https://travis-ci.org/sendgrid/sendgrid-nodejs/builds/149293172 |
For Node v.10 looks like I need to catch this error: https://travis-ci.org/sendgrid/sendgrid-nodejs/jobs/149295924#L228 |
Ok I'll have a look into these issues tomorrow. I am so used to using |
No worries, just about done fixing the issues :) This will be a 4.0.1 fix. |
Cool, as for the Promise error you can probably wrap that in an if statement to see if |
Yes, the build is passing now :) https://travis-ci.org/sendgrid/sendgrid-nodejs/builds/149298361 |
This PR addresses the following issues:
require('sendgrid-rest')
repeated so much in/lib/sendgrid.js
? #246Notable changes:
getEmptyRequest
helper to avoid code duplicationemtpyRequest
now accepts an object with data to extend the empty request with, this will allow simpler syntax for initializing requests.Sendgrid.Promise
to any value, e.g.Sendgrid.Promise = require('bluebird')
Example usage: