-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
executable file
·216 lines (195 loc) · 4.42 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const {
args,
} = Deno;
const sendgridUri = "https://api.sendgrid.com/v3/mail/send";
export interface IOptions {
apiKey: string;
}
export interface IResult {
success: boolean;
errors?: string[];
}
export interface IAddress {
email: string;
name?: string;
}
export interface IContent {
type: string;
value: string;
}
export interface IAttachment {
content: string;
filename: string;
type?: string;
disposition?: string;
contentId?: string;
}
export interface IPersonalization {
to: IAddress[];
cc?: IAddress[];
bcc?: IAddress[];
subject: string;
substitutions?: any;
customArgs?: any;
dynamicTemplateData?: any;
headers?: any;
}
export interface IRequestBody {
personalizations: IPersonalization[];
from: IAddress;
replyTo?: IAddress;
content: IContent[];
attachments?: IAttachment[];
templateId?: string;
headers?: any;
sections?: any;
categories?: string[];
customArgs?: any;
sendAt?: number;
batchId?: string;
ipPoolName?: string;
asm?: {
groupId: number;
groupsToDisplay: number[];
};
mailSettings?: {
bcc?: {
enable?: boolean;
email?: string;
};
bypassListManagement?: {
enable?: boolean;
};
footer?: {
enable?: boolean;
text?: string;
html?: string;
};
sandboxMode?: {
enable?: boolean;
};
spamCheck?: {
enable?: boolean;
threshold?: number;
postToUrl?: string;
};
};
//trackingSettings?
}
export interface ISimpleRequestBody {
to: IAddress[];
cc?: IAddress[];
bcc?: IAddress[];
subject: string;
from: IAddress;
replyTo?: IAddress;
content: IContent[];
attachments?: IAttachment[];
templateId?: string;
dynamicTemplateData?: any;
}
//
// Some keys shouldn't be snake cased. These are those.
//
function isImmune(key: string): boolean {
switch (key) {
case "dynamicTemplateData":
case "customArgs":
case "headers":
case "sections":
return true;
default:
return false;
}
}
function snakeCaseString(str: string): string {
let result = str;
let matches = str.match(
/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g,
);
if (matches && matches.length) {
matches = matches.map((x: string) => {
return x.toLowerCase();
});
result = matches.join("_");
}
return result;
}
// function isAny(toBeDetermined: PersonOrAnimal): toBeDetermined is Animal {
// if((toBeDetermined as Animal).type){
// return true
// }
// return false
// }
// view raw
function snakeCaseObject(obj: any): any {
if (Array.isArray(obj)) {
return obj.map(function (val, key) {
return (typeof val === "object") ? snakeCaseObject(val) : val;
});
} else if (typeof obj === "object") {
var res: any = {};
for (var key in obj) {
var val = obj[key];
if (typeof val === "object") {
res[snakeCaseString(key)] = isImmune(key) ? val : snakeCaseObject(val);
} else {
res[snakeCaseString(key)] = val;
}
}
return res;
} else {
return obj;
}
}
export const sendSimpleMail = async (
requestBody: ISimpleRequestBody,
options: IOptions,
): Promise<IResult> => {
let mail: IRequestBody = {
personalizations: [{
to: requestBody.to,
cc: requestBody.cc,
bcc: requestBody.bcc,
subject: requestBody.subject,
dynamicTemplateData: requestBody.dynamicTemplateData,
}],
from: requestBody.from,
replyTo: requestBody.replyTo,
content: requestBody.content,
attachments: requestBody.attachments,
templateId: requestBody.templateId,
};
return sendMail(mail, options);
};
export const sendMail = async (
requestBody: IRequestBody,
options: IOptions,
): Promise<IResult> => {
let mail = snakeCaseObject(requestBody);
let json = JSON.stringify(mail);
const response = await fetch(sendgridUri, {
method: "POST",
headers: {
"authorization": `Bearer ${options.apiKey}`,
"content-type": "application/json",
},
body: json,
});
let result: IResult = {
success: true,
};
//
// This will cause an exception from SendGrid, because SendGrid returns an
// empty JSON body. However, if you don't call it, you'll leak a resource
// that doesn't appear to have a close().
//
try {
let responseBody = await response.json();
if (responseBody) {
result.success = false;
result.errors = responseBody.errors;
}
} catch (ex) {}
return result;
};