-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
59 lines (55 loc) · 1.45 KB
/
index.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
import type { Provider, SendEmailInput } from "./types";
import { sendEmail as sendEmailResend } from "./adapters/resend";
import { sendEmail as sendEmailPostmark } from "./adapters/postmark";
export function createClient(providers: Provider[]) {
return {
async sendEmail(email: SendEmailInput) {
for (const provider of providers) {
const providerType = provider.type;
try {
switch (providerType) {
case "custom": {
const res = await provider.sendEmail(email);
if (res.error) {
throw new Error(res.error);
}
return {
type: providerType,
index: providers.indexOf(provider),
custom: res,
};
}
case "resend": {
const res = await sendEmailResend(provider, email);
if (res.error) {
throw res.error;
}
return {
type: providerType,
index: providers.indexOf(provider),
resend: res as any,
};
}
case "postmark": {
const res = await sendEmailPostmark(provider, email);
if (res.ErrorCode) {
throw new Error(res.Message);
}
return {
type: providerType,
index: providers.indexOf(provider),
postmark: res,
};
}
default:
providerType satisfies never;
throw new Error("Invalid provider type");
}
} catch (error) {
console.error(error);
}
}
throw new Error("No email provider available");
},
};
}