Skip to content

Promise API and consistent callback function signature #261

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

Merged
merged 14 commits into from
Aug 2, 2016
Merged
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
examples
test
136 changes: 136 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
root: true
extends: eslint:recommended
parserOptions:
ecmaVersion: 6
ecmaFeatures:
impliedStrict: true
sourceType: module
env:
es6: true
node: true
mocha: true
jasmine: true
rules:
linebreak-style:
- error
- unix
max-len:
- warn
- code: 80
tabWidth: 2
ignoreComments: true
ignoreUrls: true
indent:
- error
- 2
- SwitchCase: 1
VariableDeclarator:
let: 2
const: 3
semi:
- error
- always
consistent-this:
- error
- self
- $ctrl
quotes:
- error
- single
- allowTemplateLiterals: true
curly:
- error
- all
comma-dangle:
- error
- always-multiline
new-cap:
- error
- newIsCap: true
capIsNew: true
properties: false
array-bracket-spacing:
- error
- never
arrow-spacing:
- error
- before: true
after: true
block-spacing:
- error
- always
comma-spacing:
- error
- before: false
after: true
computed-property-spacing:
- error
- never
generator-star-spacing:
- error
- before: true
after: false
key-spacing:
- error
- beforeColon: false
afterColon: true
mode: minimum
keyword-spacing:
- error
- before: true
semi-spacing:
- error
- before: false
after: true
space-in-parens:
- error
- never
space-unary-ops:
- error
- words: true
nonwords: false
space-before-function-paren:
- error
- never
space-before-blocks:
- error
- always
yoda:
- error
- never
wrap-iife:
- error
- outside
eqeqeq:
- error
- always
newline-per-chained-call:
- error
- ignoreChainWithDepth: 3
one-var-declaration-per-line:
- error
- initializations
brace-style:
- error
- stroustrup
no-implicit-coercion:
- error
- boolean: false
no-multiple-empty-lines:
- error
- max: 1
eol-last: error
dot-notation: error
space-infix-ops: error
no-with: error
no-unreachable: error
no-redeclare: error
no-unexpected-multiline: error
no-multi-spaces: error
no-multi-str: error
no-trailing-spaces: error
no-mixed-spaces-and-tabs: error
no-spaced-func: error
no-whitespace-before-property: error
no-lonely-if: error
no-console: off
131 changes: 85 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ The following is the minimum needed code to send an email with the [/mail/send H

```javascript
var helper = require('sendgrid').mail
from_email = new helper.Email("[email protected]")
to_email = new helper.Email("[email protected]")
subject = "Hello World from the SendGrid Node.js Library!"
content = new helper.Content("text/plain", "Hello, Email!")
from_email = new helper.Email('[email protected]')
to_email = new helper.Email('[email protected]')
subject = 'Hello World from the SendGrid Node.js Library!'
content = new helper.Content('text/plain', 'Hello, Email!')
mail = new helper.Mail(from_email, subject, to_email, content)

var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
var requestBody = mail.toJSON()
var request = sg.emptyRequest()
request.method = 'POST'
request.path = '/v3/mail/send'
request.body = requestBody
sg.API(request, function (response) {
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});

sg.API(request, function(error, response) {
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
Expand All @@ -97,48 +98,86 @@ The `Mail` constructor creates a [personalization object](https://sendgrid.com/d
The following is the minimum needed code to send an email without the /mail/send Helper ([here](https://github.com/sendgrid/sendgrid-nodejs/blob/master/examples/mail/mail.js#L31) is a full example):

```javascript
var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
var request = sg.emptyRequest()
request.body = {
"personalizations": [
{
"to": [
{
"email": "[email protected]"
}
],
"subject": "Hello World from the SendGrid Node.js Library!"
}
],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/plain",
"value": "Hello, Email!"
}
]
};
request.method = 'POST'
request.path = '/v3/mail/send'
sg.API(request, function (response) {
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
})
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: {
personalizations: [
{
to: [
{
email: '[email protected]'
}
],
subject: 'Hello World from the SendGrid Node.js Library!'
}
],
from: {
email: '[email protected]'
},
content: [
{
type: 'text/plain',
value: 'Hello, Email!'
}
]
};
});

//With promise
sg.API(request)
.then(response => {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
})
.catch(error => {
//error is an instance of SendGridError
//The full response is attached to error.response
console.log(error.response.statusCode);
});

//With callback
sg.API(request, function(error, response) {
if (error) {
console.log('Error response received');
}
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
});
```

## General v3 Web API Usage

```javascript
var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY)

// GET Collection
var request = sg.emptyRequest()
request.method = 'GET'
request.path = '/v3/api_keys'
sg.API(request, function (response) {
var request = sg.emptyRequest({
method: 'GET',
path: '/v3/api_keys'
});

//With promise
sg.API(request)
.then(response => {
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
})
.catch(error => {
//error is an instance of SendGridError
//The full response is attached to error.response
console.log(error.response.statusCode);
});

//With callback
sg.API(request, function(error, response) {
if (error) {
console.log('Error response received');
}
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
Expand Down
4 changes: 2 additions & 2 deletions TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ To read the error message returned by SendGrid's API:
content = new helper.Content("text/plain", "Hello, Email!")
mail = new helper.Mail(from_email, subject, to_email, content)

var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY)
var requestBody = mail.toJSON()
var request = sg.emptyRequest()
request.method = 'POST'
request.path = '/v3/mail/send'
request.body = requestBody
sg.API(request, function (response) {
sg.API(request, function (error, response) {
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
Expand Down
Loading