Skip to content

Commit 93763a7

Browse files
Merge pull request #261 from adambuczynski/eslint
Promise API and consistent callback function signature
2 parents 980c021 + afe0382 commit 93763a7

File tree

37 files changed

+1781
-1503
lines changed

37 files changed

+1781
-1503
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
examples
3+
test

.eslintrc.yaml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
root: true
2+
extends: eslint:recommended
3+
parserOptions:
4+
ecmaVersion: 6
5+
ecmaFeatures:
6+
impliedStrict: true
7+
sourceType: module
8+
env:
9+
es6: true
10+
node: true
11+
mocha: true
12+
jasmine: true
13+
rules:
14+
linebreak-style:
15+
- error
16+
- unix
17+
max-len:
18+
- warn
19+
- code: 80
20+
tabWidth: 2
21+
ignoreComments: true
22+
ignoreUrls: true
23+
indent:
24+
- error
25+
- 2
26+
- SwitchCase: 1
27+
VariableDeclarator:
28+
let: 2
29+
const: 3
30+
semi:
31+
- error
32+
- always
33+
consistent-this:
34+
- error
35+
- self
36+
- $ctrl
37+
quotes:
38+
- error
39+
- single
40+
- allowTemplateLiterals: true
41+
curly:
42+
- error
43+
- all
44+
comma-dangle:
45+
- error
46+
- always-multiline
47+
new-cap:
48+
- error
49+
- newIsCap: true
50+
capIsNew: true
51+
properties: false
52+
array-bracket-spacing:
53+
- error
54+
- never
55+
arrow-spacing:
56+
- error
57+
- before: true
58+
after: true
59+
block-spacing:
60+
- error
61+
- always
62+
comma-spacing:
63+
- error
64+
- before: false
65+
after: true
66+
computed-property-spacing:
67+
- error
68+
- never
69+
generator-star-spacing:
70+
- error
71+
- before: true
72+
after: false
73+
key-spacing:
74+
- error
75+
- beforeColon: false
76+
afterColon: true
77+
mode: minimum
78+
keyword-spacing:
79+
- error
80+
- before: true
81+
semi-spacing:
82+
- error
83+
- before: false
84+
after: true
85+
space-in-parens:
86+
- error
87+
- never
88+
space-unary-ops:
89+
- error
90+
- words: true
91+
nonwords: false
92+
space-before-function-paren:
93+
- error
94+
- never
95+
space-before-blocks:
96+
- error
97+
- always
98+
yoda:
99+
- error
100+
- never
101+
wrap-iife:
102+
- error
103+
- outside
104+
eqeqeq:
105+
- error
106+
- always
107+
newline-per-chained-call:
108+
- error
109+
- ignoreChainWithDepth: 3
110+
one-var-declaration-per-line:
111+
- error
112+
- initializations
113+
brace-style:
114+
- error
115+
- stroustrup
116+
no-implicit-coercion:
117+
- error
118+
- boolean: false
119+
no-multiple-empty-lines:
120+
- error
121+
- max: 1
122+
eol-last: error
123+
dot-notation: error
124+
space-infix-ops: error
125+
no-with: error
126+
no-unreachable: error
127+
no-redeclare: error
128+
no-unexpected-multiline: error
129+
no-multi-spaces: error
130+
no-multi-str: error
131+
no-trailing-spaces: error
132+
no-mixed-spaces-and-tabs: error
133+
no-spaced-func: error
134+
no-whitespace-before-property: error
135+
no-lonely-if: error
136+
no-console: off

README.md

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,20 @@ The following is the minimum needed code to send an email with the [/mail/send H
7171

7272
```javascript
7373
var helper = require('sendgrid').mail
74-
from_email = new helper.Email("[email protected]")
75-
to_email = new helper.Email("[email protected]")
76-
subject = "Hello World from the SendGrid Node.js Library!"
77-
content = new helper.Content("text/plain", "Hello, Email!")
74+
from_email = new helper.Email('[email protected]')
75+
to_email = new helper.Email('[email protected]')
76+
subject = 'Hello World from the SendGrid Node.js Library!'
77+
content = new helper.Content('text/plain', 'Hello, Email!')
7878
mail = new helper.Mail(from_email, subject, to_email, content)
7979

80-
var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
81-
var requestBody = mail.toJSON()
82-
var request = sg.emptyRequest()
83-
request.method = 'POST'
84-
request.path = '/v3/mail/send'
85-
request.body = requestBody
86-
sg.API(request, function (response) {
80+
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
81+
var request = sg.emptyRequest({
82+
method: 'POST',
83+
path: '/v3/mail/send',
84+
body: mail.toJSON()
85+
});
86+
87+
sg.API(request, function(error, response) {
8788
console.log(response.statusCode)
8889
console.log(response.body)
8990
console.log(response.headers)
@@ -97,48 +98,86 @@ The `Mail` constructor creates a [personalization object](https://sendgrid.com/d
9798
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):
9899

99100
```javascript
100-
var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
101-
var request = sg.emptyRequest()
102-
request.body = {
103-
"personalizations": [
104-
{
105-
"to": [
106-
{
107-
"email": "[email protected]"
108-
}
109-
],
110-
"subject": "Hello World from the SendGrid Node.js Library!"
111-
}
112-
],
113-
"from": {
114-
"email": "[email protected]"
115-
},
116-
"content": [
117-
{
118-
"type": "text/plain",
119-
"value": "Hello, Email!"
120-
}
121-
]
122-
};
123-
request.method = 'POST'
124-
request.path = '/v3/mail/send'
125-
sg.API(request, function (response) {
126-
console.log(response.statusCode)
127-
console.log(response.body)
128-
console.log(response.headers)
129-
})
101+
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
102+
var request = sg.emptyRequest({
103+
method: 'POST',
104+
path: '/v3/mail/send',
105+
body: {
106+
personalizations: [
107+
{
108+
to: [
109+
{
110+
111+
}
112+
],
113+
subject: 'Hello World from the SendGrid Node.js Library!'
114+
}
115+
],
116+
from: {
117+
118+
},
119+
content: [
120+
{
121+
type: 'text/plain',
122+
value: 'Hello, Email!'
123+
}
124+
]
125+
};
126+
});
127+
128+
//With promise
129+
sg.API(request)
130+
.then(response => {
131+
console.log(response.statusCode);
132+
console.log(response.body);
133+
console.log(response.headers);
134+
})
135+
.catch(error => {
136+
//error is an instance of SendGridError
137+
//The full response is attached to error.response
138+
console.log(error.response.statusCode);
139+
});
140+
141+
//With callback
142+
sg.API(request, function(error, response) {
143+
if (error) {
144+
console.log('Error response received');
145+
}
146+
console.log(response.statusCode);
147+
console.log(response.body);
148+
console.log(response.headers);
149+
});
130150
```
131151

132152
## General v3 Web API Usage
133153

134154
```javascript
135-
var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
155+
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY)
136156

137157
// GET Collection
138-
var request = sg.emptyRequest()
139-
request.method = 'GET'
140-
request.path = '/v3/api_keys'
141-
sg.API(request, function (response) {
158+
var request = sg.emptyRequest({
159+
method: 'GET',
160+
path: '/v3/api_keys'
161+
});
162+
163+
//With promise
164+
sg.API(request)
165+
.then(response => {
166+
console.log(response.statusCode)
167+
console.log(response.body)
168+
console.log(response.headers)
169+
})
170+
.catch(error => {
171+
//error is an instance of SendGridError
172+
//The full response is attached to error.response
173+
console.log(error.response.statusCode);
174+
});
175+
176+
//With callback
177+
sg.API(request, function(error, response) {
178+
if (error) {
179+
console.log('Error response received');
180+
}
142181
console.log(response.statusCode)
143182
console.log(response.body)
144183
console.log(response.headers)

TROUBLESHOOTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ To read the error message returned by SendGrid's API:
7373
content = new helper.Content("text/plain", "Hello, Email!")
7474
mail = new helper.Mail(from_email, subject, to_email, content)
7575

76-
var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
76+
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY)
7777
var requestBody = mail.toJSON()
7878
var request = sg.emptyRequest()
7979
request.method = 'POST'
8080
request.path = '/v3/mail/send'
8181
request.body = requestBody
82-
sg.API(request, function (response) {
82+
sg.API(request, function (error, response) {
8383
console.log(response.statusCode)
8484
console.log(response.body)
8585
console.log(response.headers)

0 commit comments

Comments
 (0)