Skip to content

Commit a01a277

Browse files
committedAug 29, 2017
Add TypeScript definitions for v6 packages
1 parent 01a9efe commit a01a277

34 files changed

+1244
-124
lines changed
 

‎.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ script:
1717
- yarn test:mail
1818
- yarn test:client
1919
- yarn test:helpers
20+
- yarn test:typescript
2021
notifications:
2122
hipchat:
2223
rooms:

‎package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"mocha-clean": "^1.0.0",
2121
"mocha-sinon": "^2.0.0",
2222
"sinon": "^2.3.2",
23-
"sinon-chai": "^2.10.0"
23+
"sinon-chai": "^2.10.0",
24+
"typescript": "~2.4.2"
2425
},
2526
"scripts": {
2627
"lint": "eslint . --fix",
@@ -30,6 +31,7 @@
3031
"test:helpers": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/helpers/**/*.spec.js\"",
3132
"test:client": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/client/**/*.spec.js\"",
3233
"test:mail": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/mail/**/*.spec.js\"",
34+
"test:typescript": "tsc",
3335
"test": "npm run test:all -s",
3436
"coverage": "open -a \"Google Chrome\" ./coverage/lcov-report/index.html"
3537
}

‎packages/client/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Client = require("@sendgrid/client/src/client");
2+
3+
export = Client;

‎packages/client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"dependencies": {
3030
"@sendgrid/helpers": "^6.0.0",
31-
"request": "^2.81.0"
31+
"request": "^2.81.0",
32+
"@types/request": "^2.0.3"
3233
},
3334
"tags": [
3435
"http",

‎packages/client/src/client.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {ResponseError} from "@sendgrid/helpers/classes";
2+
import {OptionsWithUrl} from "request";
3+
4+
declare type ClientRequest = OptionsWithUrl;
5+
6+
declare class Client {
7+
/**
8+
* Set API key
9+
*/
10+
setApiKey(apiKey: string): void;
11+
12+
/**
13+
* Set default header
14+
*/
15+
setDefaultHeader(key: string, value: string): this;
16+
17+
/**
18+
* Set default request
19+
*/
20+
setDefaultRequest<K extends keyof ClientRequest>(key: K, value: ClientRequest[K]): this;
21+
22+
/**
23+
* Create headers for request
24+
*/
25+
createHeaders(data: { [key: string]: string }): { [key: string]: string };
26+
27+
/**
28+
* Create request
29+
*/
30+
createRequest(data: ClientRequest): ClientRequest;
31+
32+
/**
33+
* Do a request
34+
*/
35+
request(data: ClientRequest, cb?: (err: ResponseError, response: any[]) => void): Promise<any[]>;
36+
}
37+
38+
export = new Client()

‎packages/client/yarn.lock

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
22
# yarn lockfile v1
3+
"@sendgrid/helpers@^6.0.0":
4+
version "6.0.0"
5+
resolved "https://registry.yarnpkg.com/@sendgrid/helpers/-/helpers-6.0.0.tgz#b7ae9b927822e6a3a3e44abb2486e68d03e25220"
6+
dependencies:
7+
chalk "^2.0.1"
8+
9+
"@types/form-data@*":
10+
version "2.2.0"
11+
resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-2.2.0.tgz#a98aac91dc99857b6af24caef7ca6df302f31565"
12+
dependencies:
13+
"@types/node" "*"
314

15+
"@types/node@*":
16+
version "8.0.25"
17+
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.25.tgz#66ecaf4df93f5281b48427ee96fbcdfc4f0cdce1"
18+
19+
"@types/request@^2.0.3":
20+
version "2.0.3"
21+
resolved "https://registry.yarnpkg.com/@types/request/-/request-2.0.3.tgz#bdf0fba9488c822f77e97de3dd8fe357b2fb8c06"
22+
dependencies:
23+
"@types/form-data" "*"
24+
"@types/node" "*"
425

526
ajv@^4.9.1:
627
version "4.11.8"
@@ -9,18 +30,24 @@ ajv@^4.9.1:
930
co "^4.6.0"
1031
json-stable-stringify "^1.0.1"
1132

33+
ansi-styles@^3.1.0:
34+
version "3.2.0"
35+
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.0.tgz#c159b8d5be0f9e5a6f346dab94f16ce022161b88"
36+
dependencies:
37+
color-convert "^1.9.0"
38+
1239
asn1@~0.2.3:
1340
version "0.2.3"
1441
resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.3.tgz#dac8787713c9966849fc8180777ebe9c1ddf3b86"
1542

16-
assert-plus@1.0.0, assert-plus@^1.0.0:
17-
version "1.0.0"
18-
resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525"
19-
2043
assert-plus@^0.2.0:
2144
version "0.2.0"
2245
resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-0.2.0.tgz#d74e1b87e7affc0db8aadb7021f3fe48101ab234"
2346

47+
assert-plus@^1.0.0, assert-plus@1.0.0:
48+
version "1.0.0"
49+
resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525"
50+
2451
asynckit@^0.4.0:
2552
version "0.4.0"
2653
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
@@ -49,10 +76,28 @@ caseless@~0.12.0:
4976
version "0.12.0"
5077
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
5178

79+
chalk@^2.0.1:
80+
version "2.1.0"
81+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.1.0.tgz#ac5becf14fa21b99c6c92ca7a7d7cfd5b17e743e"
82+
dependencies:
83+
ansi-styles "^3.1.0"
84+
escape-string-regexp "^1.0.5"
85+
supports-color "^4.0.0"
86+
5287
co@^4.6.0:
5388
version "4.6.0"
5489
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
5590

91+
color-convert@^1.9.0:
92+
version "1.9.0"
93+
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.0.tgz#1accf97dd739b983bf994d56fec8f95853641b7a"
94+
dependencies:
95+
color-name "^1.1.1"
96+
97+
color-name@^1.1.1:
98+
version "1.1.3"
99+
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
100+
56101
combined-stream@^1.0.5, combined-stream@~1.0.5:
57102
version "1.0.5"
58103
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009"
@@ -81,6 +126,10 @@ ecc-jsbn@~0.1.1:
81126
dependencies:
82127
jsbn "~0.1.0"
83128

129+
escape-string-regexp@^1.0.5:
130+
version "1.0.5"
131+
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
132+
84133
extend@~3.0.0:
85134
version "3.0.1"
86135
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444"
@@ -118,6 +167,10 @@ har-validator@~4.2.1:
118167
ajv "^4.9.1"
119168
har-schema "^1.0.5"
120169

170+
has-flag@^2.0.0:
171+
version "2.0.0"
172+
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51"
173+
121174
hawk@~3.1.3:
122175
version "3.1.3"
123176
resolved "https://registry.yarnpkg.com/hawk/-/hawk-3.1.3.tgz#078444bd7c1640b0fe540d2c9b73d59678e8e1c4"
@@ -259,6 +312,12 @@ stringstream@~0.0.4:
259312
version "0.0.5"
260313
resolved "https://registry.yarnpkg.com/stringstream/-/stringstream-0.0.5.tgz#4e484cd4de5a0bbbee18e46307710a8a81621878"
261314

315+
supports-color@^4.0.0:
316+
version "4.2.1"
317+
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.2.1.tgz#65a4bb2631e90e02420dba5554c375a4754bb836"
318+
dependencies:
319+
has-flag "^2.0.0"
320+
262321
tough-cookie@~2.3.0:
263322
version "2.3.2"
264323
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a"
@@ -284,3 +343,4 @@ verror@1.3.6:
284343
resolved "https://registry.yarnpkg.com/verror/-/verror-1.3.6.tgz#cff5df12946d297d2baaefaa2689e25be01c005c"
285344
dependencies:
286345
extsprintf "1.0.2"
346+

‎packages/contact-importer/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Importer = require("@sendgrid/contact-importer/src/importer");
2+
3+
export = Importer;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
declare interface ContactImporterOptions {
2+
batchSize?: number;
3+
rateLimitLimit?: number;
4+
rateLimitPeriod?: number;
5+
}
6+
7+
declare interface Contact {
8+
email: string;
9+
first_name?: string;
10+
last_name?: string;
11+
age?: number;
12+
}
13+
14+
declare class ContactImporter {
15+
constructor(sg, options?: ContactImporterOptions);
16+
17+
push(data: Contact|Contact[])
18+
19+
on(event: "success", cb: (result: any, batch: Contact[]) => void): void;
20+
on(event: "error", cb: (err: Error, batch?: Contact[]) => void): void;
21+
on(event: "drain", cb: () => void): void;
22+
on(event: string, cb: (data: any) => void): void;
23+
24+
once(event: "success", cb: (result: any, batch: Contact[]) => void): void;
25+
once(event: "error", cb: (err: Error, batch?: Contact[]) => void): void;
26+
once(event: "drain", cb: () => void): void;
27+
once(event: string, cb: (data: any) => void): void;
28+
}
29+
30+
export = ContactImporter;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export interface AttachmentData {
2+
content: string;
3+
filename: string;
4+
type?: string;
5+
disposition?: string;
6+
contentId?: string;
7+
}
8+
9+
export interface AttachmentJSON {
10+
content: string;
11+
filename: string;
12+
type?: string;
13+
disposition?: string;
14+
content_id?: string;
15+
}
16+
17+
export default class Attachment {
18+
constructor(data?: AttachmentData);
19+
20+
fromData(data: AttachmentData): void;
21+
setContent(content: string): void;
22+
setFilename(filename: string): void;
23+
setType(type: string): void;
24+
setDisposition(disposition: string): void;
25+
setContentId(contentId: string): void;
26+
toJSON(): AttachmentJSON;
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export type EmailData = string|{ name?: string; email: string; }
2+
3+
export type EmailJSON = { name?: string; email: string }
4+
5+
export default class EmailAddress {
6+
constructor(data?: EmailData);
7+
8+
/**
9+
* From data
10+
*/
11+
fromData(data: EmailData);
12+
13+
/**
14+
* Set name
15+
*/
16+
setName(name: string);
17+
18+
/**
19+
* Set email (mandatory)
20+
*/
21+
setEmail(email: string);
22+
23+
toJSON(): EmailJSON;
24+
}

‎packages/helpers/classes/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Attachment from "@sendgrid/helpers/classes/attachment";
2+
import EmailAddress from "@sendgrid/helpers/classes/email-address";
3+
import Mail from "@sendgrid/helpers/classes/mail"
4+
import Personalization from "@sendgrid/helpers/classes/personalization";
5+
import ResponseError from "@sendgrid/helpers/classes/response-error";
6+
7+
export {
8+
Attachment,
9+
EmailAddress,
10+
Mail,
11+
Personalization,
12+
ResponseError,
13+
}

‎packages/helpers/classes/mail.d.ts

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
import {AttachmentData, AttachmentJSON} from "./attachment";
2+
import {EmailData, EmailJSON} from "./email-address";
3+
import Personalization from "./personalization";
4+
import {PersonalizationData, PersonalizationJSON} from "./personalization";
5+
6+
export interface MailContent {
7+
type: string;
8+
value: string;
9+
}
10+
11+
export interface ASMOptions {
12+
groupId: number;
13+
groupsToDisplay?: number[];
14+
}
15+
16+
export interface ASMOptionsJSON {
17+
group_id: number;
18+
groups_to_display?: number[];
19+
}
20+
21+
export interface MailSettings {
22+
bcc?: {
23+
enable?: boolean;
24+
email?: string;
25+
};
26+
bypassListManagement?: {
27+
enable?: boolean;
28+
};
29+
footer?: {
30+
enable?: boolean;
31+
text?: string;
32+
html?: string;
33+
};
34+
sandboxMode?: {
35+
enable?: boolean;
36+
};
37+
spamCheck?: {
38+
enable?: boolean;
39+
threshold?: number;
40+
postToUrl?: string;
41+
};
42+
}
43+
44+
export interface MailSettingsJSON {
45+
bcc?: {
46+
enable?: boolean;
47+
email?: string;
48+
};
49+
bypass_list_management?: {
50+
enable?: boolean;
51+
};
52+
footer?: {
53+
enable?: boolean;
54+
text?: string;
55+
html?: string;
56+
};
57+
sandbox_mode?: {
58+
enable?: boolean;
59+
};
60+
spam_check?: {
61+
enable?: boolean;
62+
threshold?: number;
63+
post_to_url?: string;
64+
};
65+
}
66+
67+
export interface TrackingSettings {
68+
clickTracking?: {
69+
enable?: boolean;
70+
enableText?: boolean;
71+
};
72+
openTracking?: {
73+
enable?: boolean;
74+
substitutionTag?: string;
75+
};
76+
subscriptionTracking?: {
77+
enable?: boolean;
78+
text?: string;
79+
html?: string;
80+
substitutionTag?: string;
81+
};
82+
ganalytics?: {
83+
enable?: boolean;
84+
utmSource?: string;
85+
utmMedium?: string;
86+
utmTerm?: string;
87+
utmContent?: string;
88+
utmCampaign?: string;
89+
};
90+
}
91+
92+
export interface TrackingSettingsJSON {
93+
click_tracking?: {
94+
enable?: boolean;
95+
enable_text?: boolean;
96+
};
97+
open_tracking?: {
98+
enable?: boolean;
99+
substitution_tag?: string;
100+
};
101+
subscription_tracking?: {
102+
enable?: boolean;
103+
text?: string;
104+
html?: string;
105+
substitution_tag?: string;
106+
};
107+
ganalytics?: {
108+
enable?: boolean;
109+
utm_source?: string;
110+
utm_medium?: string;
111+
utm_term?: string;
112+
utm_content?: string;
113+
utm_campaign?: string;
114+
};
115+
}
116+
117+
export interface MailData {
118+
to?: EmailData|EmailData[],
119+
cc?: EmailData|EmailData[],
120+
bcc?: EmailData|EmailData[],
121+
122+
from: EmailData,
123+
replyTo?: EmailData,
124+
125+
sendAt?: number,
126+
127+
subject?: string,
128+
text?: string,
129+
html?: string,
130+
content?: MailContent[],
131+
templateId?: string,

Comment on line R131

Maxwell2022 commented on Sep 13, 2018

@Maxwell2022

@spartan563 the doc is mentioning a property called dynamicTemplateData or dynamic_template_data but this props is not listed here. Does this prop name has changed?

notheotherben replied on Sep 13, 2018

@notheotherben
ContributorAuthor

Hi @Maxwell2022, it looks like dynamic_template_data was added in 2fbcd05, about 15 days ago. This commit was made over a year ago.

In other words, dynamic_template_data is a new feature which needs to be added to the definition, I've opened #740 to get this looked into and fixed when I have the time.

Code has comments. Press enter to view.
132+
133+
personalizations?: PersonalizationData[],
134+
attachments?: AttachmentData[],
135+
136+
ipPoolName?: string,
137+
batchId?: string,
138+
139+
sections?: { [key: string]: string },
140+
headers?: { [key: string]: string },
141+
142+
categories?: string[],
143+
category?: string,
144+
145+
customArgs?: { [key: string]: any },
146+
asm?: ASMOptions,
147+
148+
mailSettings?: MailSettings,
149+
trackingSettings?: TrackingSettings,
150+
151+
substitutions?: { [key: string]: string },
152+
substitutionWrappers?: string[],
153+
154+
isMultiple?: boolean,
155+
}
156+
157+
export interface MailJSON {
158+
from: EmailJSON;
159+
subject: string;
160+
content: MailContent[];
161+
personalizations: PersonalizationJSON[];
162+
attachments?: AttachmentJSON[];
163+
categories?: string[];
164+
headers?: { [key: string]: string };
165+
mail_settings?: MailSettingsJSON;
166+
tracking_settings?: TrackingSettingsJSON;
167+
custom_args?: { [key: string]: string };
168+
sections?: { [key: string]: string };
169+
asm?: ASMOptionsJSON;
170+
171+
reply_to?: EmailJSON;
172+
send_at?: number;
173+
batch_id?: string;
174+
template_id?: string;
175+
ip_pool_name?: string;
176+
}
177+
178+
export default class Mail {
179+
constructor(data?: MailData);
180+
181+
/**
182+
* Build from data
183+
*/
184+
fromData(data: MailData): void;
185+
186+
/**
187+
* Set from email
188+
*/
189+
setFrom(from: EmailData): void;
190+
191+
/**
192+
* Set reply to
193+
*/
194+
setReplyTo(replyTo: EmailData): void;
195+
196+
/**
197+
* Set subject
198+
*/
199+
setSubject(subject: string): void;
200+
201+
/**
202+
* Set send at
203+
*/
204+
setSendAt(sendAt: number): void;
205+
206+
/**
207+
* Set template ID
208+
*/
209+
setTemplateId(templateId: string): void;
210+
211+
/**
212+
* Set batch ID
213+
*/
214+
setBatchId(batchId: string): void;
215+
216+
/**
217+
* Set IP pool name
218+
*/
219+
setIpPoolName(ipPoolName: string): void;
220+
221+
/**
222+
* Set ASM
223+
*/
224+
setAsm(asm: ASMOptions): void;
225+
226+
/**
227+
* Set personalizations
228+
*/
229+
setPersonalizations(personalizations: PersonalizationData[]): void;
230+
231+
/**
232+
* Add personalization
233+
*/
234+
addPersonalization(personalization: PersonalizationData): void;
235+
236+
/**
237+
* Convenience method for quickly creating personalizations
238+
*/
239+
addTo(to: EmailData|EmailData[], cc: EmailData|EmailData[], bcc: EmailData|EmailData[]): void;
240+
241+
/**
242+
* Set substitutions
243+
*/
244+
setSubstitutions(substitutions: { [key: string]: string }): void;
245+
246+
/**
247+
* Set substitution wrappers
248+
*/
249+
setSubstitutionWrappers(wrappers: string[]): void;
250+
251+
/**
252+
* Helper which applies globally set substitutions to personalizations
253+
*/
254+
applySubstitutions(personalization: Personalization): void;
255+
256+
/**
257+
* Set content
258+
*/
259+
setContent(content: MailContent[]): void;
260+
261+
/**
262+
* Add content
263+
*/
264+
addContent(content: MailContent): void;
265+
266+
/**
267+
* Add text content
268+
*/
269+
addTextContent(text: string): void;
270+
271+
/**
272+
* Add HTML content
273+
*/
274+
addHtmlContent(html: string): void;
275+
276+
/**
277+
* Set attachments
278+
*/
279+
setAttachments(attachments: AttachmentData[]): void;
280+
281+
/**
282+
* Add attachment
283+
*/
284+
addAttachment(attachment: AttachmentData): void;
285+
286+
/**
287+
* Set categories
288+
*/
289+
setCategories(categories: string[]): void;
290+
291+
/**
292+
* Add category
293+
*/
294+
addCategory(category: string): void;
295+
296+
/**
297+
* Set headers
298+
*/
299+
setHeaders(headers: { [key: string]: string }): void;
300+
301+
/**
302+
* Add a header
303+
*/
304+
addHeader(key: string, value: string): void;
305+
306+
/**
307+
* Set sections
308+
*/
309+
setSections(sections: { [key: string]: string }): void;
310+
311+
/**
312+
* Set custom args
313+
*/
314+
setCustomArgs(customArgs: { [key: string]: string }): void;
315+
316+
/**
317+
* Set tracking settings
318+
*/
319+
setTrackingSettings(settings: TrackingSettings): void;
320+
321+
/**
322+
* Set mail settings
323+
*/
324+
setMailSettings(settings: MailSettings): void;
325+
326+
/**
327+
* To JSON
328+
*/
329+
toJSON(): MailJSON;
330+
331+
/**
332+
* Create a Mail instance from given data
333+
*/
334+
static create(data: MailData): Mail;
335+
336+
/**
337+
* Create a Mail instance from given data
338+
*/
339+
static create(data: Mail): Mail;
340+
341+
/**
342+
* Create a Mail instance from given data
343+
*/
344+
static create(data: MailData[]): Mail[];
345+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import {EmailData, EmailJSON} from "./email-address";
2+
3+
export interface PersonalizationData {
4+
to: EmailData|EmailData[],
5+
cc?: EmailData|EmailData[],
6+
bcc?: EmailData|EmailData[],
7+
subject?: string;
8+
headers?: { [key: string]: string };
9+
substitutions?: { [key: string]: string };
10+
customArgs?: { [key: string]: string };
11+
sendAt?: number;
12+
}
13+
14+
export interface PersonalizationJSON {
15+
to: EmailJSON|EmailJSON[];
16+
cc?: EmailJSON[];
17+
bcc?: EmailJSON[];
18+
headers?: { [key: string]: string; };
19+
substitutions?: { [key: string]: string; };
20+
custom_args?: { [key: string]: string; };
21+
subject?: string;
22+
send_at?: number;
23+
}
24+
25+
export default class Personalization {
26+
constructor(data?: PersonalizationData);
27+
28+
fromData(data: PersonalizationData): void;
29+
30+
/**
31+
* Set subject
32+
*/
33+
setSubject(subject: string): void;
34+
35+
/**
36+
* Set send at
37+
*/
38+
setSendAt(sendAt: number): void;
39+
40+
/**
41+
* Set to
42+
*/
43+
setTo(to: EmailData|EmailData[]): void;
44+
45+
/**
46+
* Add a single to
47+
*/
48+
addTo(to: EmailData): void;
49+
50+
/**
51+
* Set cc
52+
*/
53+
setCc(cc: EmailData|EmailData[]): void;
54+
55+
/**
56+
* Add a single cc
57+
*/
58+
addCc(cc: EmailData): void;
59+
60+
/**
61+
* Set bcc
62+
*/
63+
setBcc(bcc: EmailData|EmailData[]): void;
64+
65+
/**
66+
* Add a single bcc
67+
*/
68+
addBcc(bcc: EmailData): void;
69+
70+
/**
71+
* Set headers
72+
*/
73+
setHeaders(headers: { [key: string]: string }): void;
74+
75+
/**
76+
* Add a header
77+
*/
78+
addHeader(key: string, value: string): void;
79+
80+
/**
81+
* Set custom args
82+
*/
83+
setCustomArgs(customArgs: { [key: string]: string }): void;
84+
85+
/**
86+
* Add a custom arg
87+
*/
88+
addCustomArg(key: string, value: string): void;
89+
90+
/**
91+
* Set substitutions
92+
*/
93+
setSubstitutions(substitutions: { [key: string]: string }): void;
94+
95+
/**
96+
* Add a substitution
97+
*/
98+
addSubstitution(key: string, value: string): void;
99+
100+
/**
101+
* Reverse merge substitutions, preserving existing ones
102+
*/
103+
reverseMergeSubstitutions(substitutions: { [key: string]: string }): void;
104+
105+
/**
106+
* Set substitution wrappers
107+
*/
108+
setSubstitutionWrappers(wrappers: string[]): void;
109+
110+
/**
111+
* To JSON
112+
*/
113+
toJSON(): PersonalizationJSON;
114+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default class ResponseError extends Error {
2+
code: number;
3+
message: string;
4+
response: {
5+
headers: { [key: string]: string; };
6+
body: string;
7+
};
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Helper to convert an array of objects to JSON
3+
*/
4+
declare function arrayToJSON(arr: any[]): any[];
5+
6+
export = arrayToJSON;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Helper to convert an object's keys
3+
*/
4+
declare function convertKeys<T extends {}, S extends {}>(obj: T, converter: (key: string) => string, ignored?: string[]): S;
5+
6+
export = convertKeys;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Deep cloning helper for objects
3+
*/
4+
declare function deepClone<T>(obj: T): T;
5+
6+
export = deepClone;

‎packages/helpers/helpers/index.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import arrayToJSON = require("@sendgrid/helpers/helpers/array-to-json");
2+
import convertKeys = require("@sendgrid/helpers/helpers/convert-keys");
3+
import deepClone = require("@sendgrid/helpers/helpers/deep-clone");
4+
import mergeData = require("@sendgrid/helpers/helpers/merge-data");
5+
import splitNameEmail = require("@sendgrid/helpers/helpers/split-name-email");
6+
import toCamelCase = require("@sendgrid/helpers/helpers/to-camel-case");
7+
import toSnakeCase = require("@sendgrid/helpers/helpers/to-snake-case");
8+
import wrapSubstitutions = require("@sendgrid/helpers/helpers/wrap-substitutions");
9+
10+
export {
11+
arrayToJSON,
12+
convertKeys,
13+
deepClone,
14+
mergeData,
15+
splitNameEmail,
16+
toCamelCase,
17+
toSnakeCase,
18+
wrapSubstitutions,
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Merge data helper
3+
*/
4+
declare function mergeData<T, S>(base: T, data: S): T&S;
5+
6+
export = mergeData;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Split name and email address from string
3+
*/
4+
declare function splitNameEmail(str: string): string[];
5+
6+
export = splitNameEmail;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Convert object keys to camel case
3+
*/
4+
declare function toCamelCase<T extends {}, S extends {}>(obj: T, ignored?: string[]): S;
5+
6+
export = toCamelCase;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Convert object keys to snake case
3+
*/
4+
declare function toSnakeCase<T extends {}, S extends {}>(obj: T, ignored?: string[]): S;
5+
6+
export = toSnakeCase;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Wrap substitutions
3+
*/
4+
declare function wrapSubstitutions(substitutions: { [key: string]: string }, left?: string, right?: string): { [key: string]: string };
5+
6+
export = wrapSubstitutions;

‎packages/helpers/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as helpers from "@sendgrid/helpers/helpers/index"
2+
import * as classes from "@sendgrid/helpers/classes/index"
3+
4+
export {
5+
helpers,
6+
classes,
7+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as Parser from "./src/parser";
2+
3+
export = Parser;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {Attachment} from "@sendgrid/helpers/classes"
2+
3+
declare interface ParseConfig {
4+
keys: string[];
5+
}
6+
7+
declare interface ParseRequest {
8+
body?: {};
9+
files?: any[];
10+
}
11+
12+
declare class Parse {
13+
constructor(config: ParseConfig, request: ParseRequest);
14+
15+
/**
16+
* Return an object literal of key/values in the payload received from webhook
17+
* @return {Object} Valid key/values in the webhook payload
18+
*/
19+
keyValues(): { [key: string]: any };
20+
21+
/**
22+
* Whether the payload contains the raw email (Only applies to raw payloads)
23+
* @return {Boolean}
24+
*/
25+
hasRawEmail(): boolean;
26+
27+
/**
28+
* Parses the raw email and returns the mail object in a callback (Only applies to raw payloads)
29+
* @param {Function} callback Function which will receive the parsed email object as the sole argument
30+
*/
31+
getRawEmail(callback: (mail: any) => void): void;
32+
// TODO: Type information for MailParser result in callback
33+
34+
/**
35+
* Retrieves all attachments received from the webhook payload
36+
* @param {Function} callback Function which will receive an array, of attachments found, as the sole argument
37+
*/
38+
attachments(callback: (attachments: Attachment[]) => void): void;
39+
}
40+
41+
export = Parse;

‎packages/mail/USE_CASES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ const msg = {
444444
myCustomArg: 123,
445445
},
446446
batchId: 'sendgrid-batch-id',
447-
asm: {},
447+
asm: {
448+
groupId: 1
449+
},
448450
ipPoolName: 'sendgrid-ip-pool-name',
449451
mailSettings: {},
450452
trackingSettings: {},

‎packages/mail/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import MailService = require("@sendgrid/mail/src/mail");
2+
3+
export = MailService;

‎packages/mail/src/mail.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Client = require("@sendgrid/client");
2+
import {ResponseError} from "@sendgrid/helpers/classes";
3+
import {MailData} from "@sendgrid/helpers/classes/mail";
4+
5+
declare class MailService {
6+
/**
7+
* API key pass through for convenience
8+
*/
9+
setApiKey(apiKey: string): void;
10+
11+
/**
12+
* Set substitution wrappers
13+
*/
14+
setSubstitutionWrappers(left: string, right: string): void;
15+
16+
/**
17+
* Send email
18+
*/
19+
send(data: MailData, isMultiple?: boolean, cb?: (err: Error|ResponseError, result) => void): Promise<any>;
20+
21+
/**
22+
* Send emails
23+
*/
24+
send(data: MailData[], isMultiple?: boolean, cb?: (err: Error|ResponseError, result) => void): Promise<any>;
25+
26+
/**
27+
* Send multiple emails (shortcut)
28+
*/
29+
sendMultiple(data: MailData, cb?: (error: Error|ResponseError, result) => void): Promise<any>;
30+
}
31+
32+
export = new MailService()

‎test/typescript/client.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Client = require("@sendgrid/client");
2+
3+
// Test setApiKey() method
4+
Client.setApiKey("MY_SENDGRID_API_KEY");
5+
6+
// Test setDefaultHeader() method
7+
Client.setDefaultHeader("X-Testing", "yes")
8+
.setDefaultHeader("X-Testing-Type", "TypeScript");
9+
10+
// Test setDefaultRequest() method
11+
Client.setDefaultRequest("method", "POST");
12+
Client.setDefaultRequest("method", "POST")
13+
.setDefaultRequest("url", "/test");
14+
15+
// Test createHeaders() method
16+
Client.createHeaders({
17+
"X-Testing": "yes"
18+
});
19+
20+
// Test createRequest() method
21+
const req = Client.createRequest({
22+
url: "/test"
23+
});

‎test/typescript/helpers.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as helpers from "@sendgrid/helpers";
2+
new helpers.classes.Attachment({
3+
content: "",
4+
filename: "test.txt",
5+
type: "text/plain"
6+
});
7+
8+
new helpers.classes.EmailAddress("someone@example.org");
9+
new helpers.classes.EmailAddress({ name: "Some One", email: "someone@example.org" });
10+
11+
new helpers.classes.Personalization({
12+
to: "someone@example.org",
13+
subject: "Hello Some One"
14+
});
15+
16+
new helpers.classes.Mail({
17+
to: "someone@example.org",
18+
from: "someone@example.org",
19+
subject: "Hello Some One",
20+
text: "This is a test message.",
21+
html: "<p>This is a test message.</p>"
22+
});

‎test/typescript/mail.ts

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
import sgMail = require("@sendgrid/mail");
2+
3+
// Test setApiKey() method
4+
sgMail.setApiKey("MY_SENDGRID_API_KEY");
5+
6+
// Test setSubstitutionWrappers() method
7+
sgMail.setSubstitutionWrappers("{{", "}}")
8+
9+
// Test send() method
10+
sgMail.send({
11+
from: "someone@example.org",
12+
to: "Some One <someone@example.org>",
13+
cc: { name: "Some One", email: "someone@example.org" },
14+
subject: "Test Email",
15+
text: "This is a test email",
16+
html: "<p>This is a test email</p>"
17+
}).then(result => {
18+
console.log("Sent email");
19+
}, err => {
20+
console.error(err);
21+
});
22+
23+
sgMail.send({
24+
from: "someone@example.org",
25+
to: "Some One <someone@example.org>",
26+
cc: { name: "Some One", email: "someone@example.org" },
27+
subject: "Test Email",
28+
text: "This is a test email",
29+
html: "<p>This is a test email</p>"
30+
}, false, (err) => {
31+
if(err) console.error(err);
32+
else console.log("Sent email");
33+
});
34+
35+
// Test sendMultiple() method
36+
sgMail.sendMultiple({
37+
from: "someone@example.org",
38+
to: [
39+
"Some One <someone@example.org>",
40+
{ name: "Some One", email: "someone@example.org" }
41+
],
42+
cc: { name: "Some One", email: "someone@example.org" },
43+
subject: "Test Email",
44+
text: "This is a test email",
45+
html: "<p>This is a test email</p>"
46+
}).then(result => {
47+
console.log("Sent email");
48+
}, err => {
49+
console.error(err);
50+
});
51+
52+
sgMail.sendMultiple({
53+
from: "someone@example.org",
54+
to: [
55+
"Some One <someone@example.org>",
56+
{ name: "Some One", email: "someone@example.org" }
57+
],
58+
cc: { name: "Some One", email: "someone@example.org" },
59+
subject: "Test Email",
60+
text: "This is a test email",
61+
html: "<p>This is a test email</p>"
62+
}, (err) => {
63+
if(err) console.error(err);
64+
else console.log("Sent email");
65+
});
66+
67+
// Examples from the USE_CASES.md file
68+
sgMail.send({
69+
to: 'recipient@example.org',
70+
from: 'sender@example.org',
71+
subject: 'Hello world',
72+
text: 'Hello plain world!',
73+
html: '<p>Hello HTML world!</p>',
74+
});
75+
76+
sgMail.send({
77+
to: ['recipient1@example.org', 'recipient2@example.org'],
78+
from: 'sender@example.org',
79+
subject: 'Hello world',
80+
text: 'Hello plain world!',
81+
html: '<p>Hello HTML world!</p>',
82+
});
83+
84+
sgMail.sendMultiple({
85+
to: ['recipient1@example.org', 'recipient2@example.org'],
86+
from: 'sender@example.org',
87+
subject: 'Hello world',
88+
text: 'Hello plain world!',
89+
html: '<p>Hello HTML world!</p>',
90+
});
91+
92+
sgMail.send({
93+
to: 'recipient@example.org',
94+
cc: 'someone@example.org',
95+
bcc: ['me@example.org', 'you@example.org'],
96+
from: 'sender@example.org',
97+
replyTo: 'othersender@example.org',
98+
subject: 'Hello world',
99+
text: 'Hello plain world!',
100+
html: '<p>Hello HTML world!</p>',
101+
});
102+
103+
sgMail.send([
104+
{
105+
to: 'recipient1@example.org',
106+
from: 'sender@example.org',
107+
subject: 'Hello recipient 1',
108+
text: 'Hello plain world!',
109+
html: '<p>Hello HTML world!</p>',
110+
},
111+
{
112+
to: 'recipient2@example.org',
113+
from: 'other-sender@example.org',
114+
subject: 'Hello recipient 2',
115+
text: 'Hello other plain world!',
116+
html: '<p>Hello other HTML world!</p>',
117+
},
118+
]);
119+
120+
sgMail.send({
121+
to: 'recipient@example.org',
122+
from: 'sender@example.org',
123+
subject: 'Hello world',
124+
text: 'Hello plain world!',
125+
html: '<p>Hello HTML world!</p>',
126+
templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
127+
substitutions: {
128+
name: 'Some One',
129+
city: 'Denver',
130+
},
131+
});
132+
133+
sgMail.send({
134+
to: 'recipient@example.org',
135+
from: 'sender@example.org',
136+
subject: 'Hello attachment',
137+
html: '<p>Here’s an attachment for you!</p>',
138+
attachments: [
139+
{
140+
content: 'Some base 64 encoded attachment content',
141+
filename: 'some-attachment.txt',
142+
type: 'plain/text',
143+
disposition: 'attachment',
144+
contentId: 'mytext'
145+
},
146+
],
147+
});
148+
149+
sgMail.send({
150+
personalizations: [
151+
{
152+
to: 'recipient1@example.org',
153+
subject: 'Hello recipient 1',
154+
substitutions: {
155+
name: 'Recipient 1',
156+
id: '123',
157+
},
158+
headers: {
159+
'X-Custom-Header': 'Recipient 1',
160+
},
161+
customArgs: {
162+
myArg: 'Recipient 1',
163+
},
164+
},
165+
{
166+
to: 'recipient2@example.org',
167+
subject: 'Hello recipient 2',
168+
substitutions: {
169+
name: 'Recipient 2',
170+
id: '456',
171+
},
172+
headers: {
173+
'X-Custom-Header': 'Recipient 2',
174+
},
175+
customArgs: {
176+
myArg: 'Recipient 1',
177+
},
178+
sendAt: 1500077141,
179+
}
180+
],
181+
from: 'sender@example.org',
182+
text: 'Hello plain world!',
183+
html: '<p>Hello HTML world!</p>',
184+
});
185+
186+
sgMail.send({
187+
to: 'recipient@example.org',
188+
from: 'sender@example.org',
189+
subject: 'Hello manual content',
190+
content: [
191+
{
192+
type: 'text/html',
193+
value: '<p>Hello HTML world!</p>',
194+
},
195+
{
196+
type: 'text/plain',
197+
value: 'Hello plain world!',
198+
},
199+
],
200+
});
201+
202+
sgMail.send({
203+
to: 'recipient@example.org',
204+
from: 'sender@example.org',
205+
subject: 'Hello custom header',
206+
html: '<p>Some email content</p>',
207+
headers: {
208+
'X-CustomHeader': 'Custom header value',
209+
},
210+
});
211+
212+
sgMail.send({
213+
to: 'recipient@example.org',
214+
from: 'sender@example.org',
215+
subject: 'Hello delayed email',
216+
html: '<p>Some email content</p>',
217+
sendAt: 1500077141,
218+
});
219+
220+
sgMail.send({
221+
to: 'recipient@example.org',
222+
from: 'sender@example.org',
223+
subject: 'Hello email with categories',
224+
html: '<p>Some email content</p>',
225+
categories: [
226+
'transactional', 'customer', 'weekly',
227+
],
228+
});
229+
230+
sgMail.send({
231+
to: 'recipient@example.org',
232+
from: 'sender@example.org',
233+
subject: 'Hello email with categories',
234+
html: '<p>Some email content</p>',
235+
category: 'transactional',
236+
});
237+
238+
sgMail.send({
239+
to: 'recipient@example.org',
240+
cc: 'someone@example.org',
241+
bcc: ['me@example.org', 'you@example.org'],
242+
from: 'sender@example.org',
243+
replyTo: 'othersender@example.org',
244+
subject: 'Hello world',
245+
text: 'Hello plain world!',
246+
html: '<p>Hello HTML world!</p>',
247+
templateId: 'sendgrid-template-id',
248+
substitutionWrappers: ['{{', '}}'],
249+
substitutions: {
250+
name: 'Some One',
251+
id: '123',
252+
},
253+
attachments: [
254+
{
255+
content: 'Some attachment content',
256+
filename: 'some-attachment.txt',
257+
},
258+
],
259+
categories: ['Transactional', 'My category'],
260+
sendAt: 1500077141,
261+
headers: {
262+
'X-CustomHeader': 'Custom header value',
263+
},
264+
sections: {},
265+
customArgs: {
266+
myCustomArg: 123,
267+
},
268+
batchId: 'sendgrid-batch-id',
269+
asm: {
270+
groupId: 1
271+
},
272+
ipPoolName: 'sendgrid-ip-pool-name',
273+
mailSettings: {},
274+
trackingSettings: {},
275+
});

‎tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"noEmit": true,
4+
"module": "commonjs",
5+
"target": "es6",
6+
"baseUrl": ".",
7+
"paths": {
8+
"@sendgrid/*": ["packages/*"]
9+
}
10+
},
11+
"include": [
12+
"test/typescript/*.ts"
13+
]
14+
}

‎yarn.lock

Lines changed: 79 additions & 117 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.