forked from triggerdotdev/trigger.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
141 lines (124 loc) · 3.49 KB
/
Copy pathindex.ts
File metadata and controls
141 lines (124 loc) · 3.49 KB
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
import {
ConnectionAuth,
IO,
IOTask,
IntegrationTaskKey,
Json,
RunTaskErrorCallback,
RunTaskOptions,
TriggerIntegration,
retry,
} from "@trigger.dev/sdk";
import { MailService } from "@sendgrid/mail";
type SendEmailData = Parameters<InstanceType<typeof MailService>["send"]>[0];
export type SendGridIntegrationOptions = {
id: string;
apiKey?: string;
};
export class SendGrid implements TriggerIntegration {
// @internal
private _options: SendGridIntegrationOptions;
// @internal
private _client?: MailService;
// @internal
private _io?: IO;
// @internal
private _connectionKey?: string;
constructor(private options: SendGridIntegrationOptions) {
this._options = options;
}
get authSource() {
return this._options.apiKey ? ("LOCAL" as const) : ("HOSTED" as const);
}
cloneForRun(io: IO, connectionKey: string, auth?: ConnectionAuth) {
const apiKey = this._options.apiKey ?? auth?.accessToken;
if (!apiKey) {
throw new Error(
`Can't initialize SendGrid integration (${this._options.id}) as apiKey was undefined`
);
}
const sendgrid = new SendGrid(this._options);
sendgrid._io = io;
sendgrid._connectionKey = connectionKey;
sendgrid._client = new MailService();
sendgrid._client.setApiKey(apiKey);
return sendgrid;
}
get id() {
return this.options.id;
}
get metadata() {
return { id: "sendgrid", name: "SendGrid" };
}
runTask<T, TResult extends Json<T> | void>(
key: IntegrationTaskKey,
callback: (client: MailService, task: IOTask, io: IO) => Promise<TResult>,
options?: RunTaskOptions,
errorCallback?: RunTaskErrorCallback
): Promise<TResult> {
if (!this._io) throw new Error("No IO");
if (!this._connectionKey) throw new Error("No connection key");
return this._io.runTask(
key,
(task, io) => {
if (!this._client) throw new Error("No client");
return callback(this._client, task, io);
},
{
icon: "sendgrid",
...(options ?? {}),
connectionKey: this._connectionKey,
retry: retry.standardBackoff,
},
errorCallback
);
}
sendEmail(key: IntegrationTaskKey, params: SendEmailData) {
const subjectProperty = Array.isArray(params)
? []
: params.subject
? [{ label: "Subject", text: params.subject }]
: [];
return this.runTask(
key,
async (client) => {
await client.send(params);
//we intentional don't return anything, as there's nothing useful in the response
},
{
name: "Send Email",
params,
icon: "sendgrid",
properties: [
{
label: "From",
text: Array.isArray(params)
? getEmailFromEmailData(params[0].from)
: getEmailFromEmailData(params.from),
},
{
label: "To",
text: Array.isArray(params)
? getEmailFromEmailData(params[0]?.to)
: getEmailFromEmailData(params?.to),
},
...subjectProperty,
],
}
);
}
}
type EmailData = string | { name?: string; email: string };
type EmailDataArray = EmailData | EmailData[];
function getEmailFromEmailData(emailData: EmailDataArray | undefined): string {
if (emailData === undefined) {
return "";
}
if (Array.isArray(emailData)) {
return emailData.map(getEmailFromEmailData).join(", ");
}
if (typeof emailData === "string") {
return emailData;
}
return emailData.email;
}