Skip to content

Commit c4a339b

Browse files
author
SomaticIT
committed
Add tests suite for sendgrid
1 parent eb1d778 commit c4a339b

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed

sendgrid/sendgrid-tests.ts

+246
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
/**
2+
* Test suite created by Maxime LUCE <https://github.com/SomaticIT>
3+
*
4+
* Created by using code samples from https://github.com/sendgrid/sendgrid-nodejs#usage
5+
*/
6+
7+
///<reference path="../node/node.d.ts" />
8+
///<reference path="sendgrid.d.ts" />
9+
10+
import sg = require("sendgrid");
11+
var sendgrid: Sendgrid = sg("api_user", "api_key");
12+
13+
/*
14+
* Simple Usage
15+
*/
16+
var payload = {
17+
18+
19+
subject: 'Saying Hi',
20+
text: 'This is my first email through SendGrid'
21+
};
22+
sendgrid.send(payload, function (err, json) {
23+
if (err) {
24+
console.error(err);
25+
}
26+
27+
console.log(json);
28+
});
29+
30+
/**
31+
* Email:
32+
* https://github.com/sendgrid/sendgrid-nodejs#email
33+
*/
34+
var email = new sendgrid.Email({
35+
36+
37+
subject: 'Subject goes here',
38+
text: 'Hello world'
39+
});
40+
41+
sendgrid.send(email, function (err, json) {
42+
if (err) {
43+
return console.error(err);
44+
}
45+
console.log(json);
46+
});
47+
48+
/**
49+
* Setting Params
50+
* https://github.com/sendgrid/sendgrid-nodejs#setting-params
51+
*/
52+
var email = new sendgrid.Email({ to: '[email protected]' });
53+
email.to = "[email protected]";
54+
email.replyto = "[email protected]";
55+
email.subject = "This is a subject";
56+
57+
/**
58+
* addTo
59+
* https://github.com/sendgrid/sendgrid-nodejs#addto
60+
*/
61+
var email = new sendgrid.Email();
62+
email.addTo('[email protected]');
63+
email.addTo('[email protected]');
64+
sendgrid.send(email, function (err, json) { });
65+
66+
67+
/**
68+
* setFrom
69+
* https://github.com/sendgrid/sendgrid-nodejs#setfrom
70+
*/
71+
var email = new sendgrid.Email();
72+
email.setFrom('[email protected]');
73+
sendgrid.send(email, function (err, json) { });
74+
75+
/**
76+
* setSubject
77+
* https://github.com/sendgrid/sendgrid-nodejs#setsubject
78+
*/
79+
var email = new sendgrid.Email();
80+
email.setSubject('Some subject');
81+
sendgrid.send(email, function (err, json) { });
82+
83+
/**
84+
* setText
85+
* https://github.com/sendgrid/sendgrid-nodejs#settext
86+
*/
87+
var email = new sendgrid.Email();
88+
email.setText('Some text');
89+
sendgrid.send(email, function (err, json) { });
90+
91+
/**
92+
* setHtml
93+
* https://github.com/sendgrid/sendgrid-nodejs#sethtml
94+
*/
95+
var email = new sendgrid.Email();
96+
email.setHtml('<h1>Some html</h1>');
97+
sendgrid.send(email, function (err, json) { });
98+
99+
/**
100+
* addHeader
101+
* https://github.com/sendgrid/sendgrid-nodejs#addheader
102+
*/
103+
var email = new sendgrid.Email();
104+
email.setHeaders({ full: 'hearts' }); // headers = {full: 'hearts'}
105+
email.addHeader('spin', 'attack'); // headers = {full: 'hearts', spin: 'attack'}
106+
email.addHeader('mask', 'salesman'); // headers = {full: 'hearts', spin: 'attack', mask: 'salesman'}
107+
sendgrid.send(email, function (err, json) { });
108+
109+
/**
110+
* setHeaders
111+
* https://github.com/sendgrid/sendgrid-nodejs#setheaders
112+
*/
113+
var email = new sendgrid.Email();
114+
email.setHeaders({ full: 'hearts' }); // headers = {full: 'hearts'}
115+
email.setHeaders({ mask: 'salesman' }); // headers = {mask: 'salesman'}
116+
sendgrid.send(email, function (err, json) { });
117+
118+
/**
119+
* addSubstitution
120+
* https://github.com/sendgrid/sendgrid-nodejs#addsubstitution
121+
*/
122+
var email = new sendgrid.Email();
123+
email.addSubstitution('keep', 'secret'); // sub = {keep: ['secret']}
124+
email.addSubstitution('other', ['one', 'two']); // sub = {keep: ['secret'], other: ['one', 'two']}
125+
126+
/**
127+
* setSubstitutions
128+
* https://github.com/sendgrid/sendgrid-nodejs#setsubstitutions
129+
*/
130+
var email = new sendgrid.Email();
131+
email.setSubstitutions({ keep: ['secret'], other: ['one', 'two'] }); // sub = {keep: ['secret'], other: ['one', 'two']});
132+
133+
/**
134+
* addSection
135+
* https://github.com/sendgrid/sendgrid-nodejs#addsection
136+
*/
137+
var email = new sendgrid.Email();
138+
email.addSection({ '-charge-': 'This ship is useless.' }); // section = {'-charge-': 'This ship is useless.'}
139+
140+
/**
141+
* setSections
142+
* https://github.com/sendgrid/sendgrid-nodejs#setsections
143+
*/
144+
var email = new sendgrid.Email();
145+
email.setSections({ '-charge-': 'This ship is useless.' }); // section = {'-charge-': 'This ship is useless.'}
146+
147+
/**
148+
* addUniqueArg
149+
* https://github.com/sendgrid/sendgrid-nodejs#adduniquearg
150+
*/
151+
var email = new sendgrid.Email();
152+
email.setUniqueArgs({ cow: 'chicken' }); // unique_args = {cow: 'chicken'}
153+
email.addUniqueArg({ cat: 'dog' }); // unique_args = {cow: 'chicken', cat: 'dog'}
154+
155+
/**
156+
* setUniqueArgs
157+
* https://github.com/sendgrid/sendgrid-nodejs#setuniqueargs
158+
*/
159+
var email = new sendgrid.Email();
160+
email.setUniqueArgs({ cow: 'chicken' }); // unique_args = {cow: 'chicken'}
161+
email.setUniqueArgs({ dad: 'proud' }); // unique_args = {dad: 'proud'}
162+
163+
164+
/**
165+
* addCategory
166+
* https://github.com/sendgrid/sendgrid-nodejs#addcategory
167+
*/
168+
var email = new sendgrid.Email();
169+
email.addCategory('tactics'); // category = ['tactics']
170+
email.addCategory('advanced'); // category = ['tactics', 'advanced']
171+
172+
/**
173+
* setCategories
174+
* https://github.com/sendgrid/sendgrid-nodejs#setcategories
175+
*/
176+
var email = new sendgrid.Email();
177+
email.setCategories(['tactics']); // category = ['tactics']
178+
email.setCategories(['snowball-fight']); // category = ['snowball-fight']
179+
180+
/**
181+
* addFilter
182+
* https://github.com/sendgrid/sendgrid-nodejs#addfilter
183+
*/
184+
var email = new sendgrid.Email();
185+
email.addFilter('footer', 'enable', 1);
186+
email.addFilter('footer', 'text/html', '<strong>boo</strong>');
187+
188+
/**
189+
* setFilters
190+
* https://github.com/sendgrid/sendgrid-nodejs#setfilters
191+
*/
192+
var email = new sendgrid.Email();
193+
var email = new sendgrid.Email();
194+
email.setFilters({
195+
'footer': {
196+
'setting': {
197+
'enable': 1,
198+
'text/plain': 'You can haz footers!'
199+
}
200+
}
201+
});
202+
203+
/**
204+
* addFile
205+
* https://github.com/sendgrid/sendgrid-nodejs#addfile
206+
*/
207+
var email = new sendgrid.Email();
208+
email.addFile({
209+
filename: 'secret.txt',
210+
content: new Buffer('You will never know....')
211+
});
212+
// or
213+
email.addFile({
214+
filename: 'icon.jpg',
215+
url: 'http://i.imgur.com/2fDh8.jpg'
216+
});
217+
// or
218+
email.addFile({
219+
path: '../files/resume.txt'
220+
});
221+
// or
222+
email.addFile({
223+
cid: 'the_logo', // should match cid value in html
224+
path: '../files/logo.png'
225+
});
226+
email.setHtml('<div>Our logo:<img src="cid:the_logo"></div>');
227+
228+
/**
229+
* Options - Changing URL
230+
* https://github.com/sendgrid/sendgrid-nodejs#changing-url
231+
*/
232+
var sendgrid1 = sg('username', 'password', { "protocol": "http", "host": "sendgrid.org", "endpoint": "/send", "port": "80" });
233+
var sendgrid2 = sg('username', 'password', { "uri": "http://sendgrid.org:80/send" });
234+
235+
/**
236+
* Options - Request
237+
* https://github.com/sendgrid/sendgrid-nodejs#request
238+
*/
239+
var sendgrid3 = sg('username', 'password', { proxy: "http://localproxy:3128" });
240+
// or
241+
var https = require('https');
242+
var agent = new https.Agent();
243+
agent.maxSockets = 500; // Set Max Sockets to 500
244+
var sendgrid4 = sg('username', 'password', { web: { pool: agent } });
245+
246+

0 commit comments

Comments
 (0)