-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathsend-email.ts
80 lines (75 loc) · 2.26 KB
/
send-email.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
import { createRoute, z } from "@hono/zod-openapi";
import { PublicAPIApp } from "~/server/public-api/hono";
import { getTeamFromToken } from "~/server/public-api/auth";
import { sendEmail } from "~/server/service/email-service";
const route = createRoute({
method: "post",
path: "/v1/emails",
request: {
body: {
required: true,
content: {
"application/json": {
schema: z
.object({
to: z.string().or(z.array(z.string())),
from: z.string(),
subject: z
.string()
.optional()
.openapi({
description: "Optional when templateId is provided",
}),
templateId: z
.string()
.optional()
.openapi({
description: "ID of a template from the dashboard",
}),
variables: z.record(z.string()).optional(),
replyTo: z.string().or(z.array(z.string())).optional(),
cc: z.string().or(z.array(z.string())).optional(),
bcc: z.string().or(z.array(z.string())).optional(),
text: z.string().optional(),
html: z.string().optional(),
attachments: z
.array(
z.object({
filename: z.string(),
content: z.string(),
})
)
.optional(),
scheduledAt: z.string().datetime().optional(),
})
.refine(
(data) => !!data.subject || !!data.templateId,
"Either subject or templateId should be passed."
),
},
},
},
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({ emailId: z.string().optional() }),
},
},
description: "Retrieve the user",
},
},
});
function send(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = await getTeamFromToken(c);
const email = await sendEmail({
...c.req.valid("json"),
teamId: team.id,
apiKeyId: team.apiKeyId,
});
return c.json({ emailId: email.withinLimitEmail?.id });
});
}
export default send;