aws.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { z } from "zod"
  2. import { Resource } from "@opencode-ai/console-resource"
  3. import { AwsClient } from "aws4fetch"
  4. import { fn } from "./util/fn"
  5. export namespace AWS {
  6. let client: AwsClient
  7. const createClient = () => {
  8. if (!client) {
  9. client = new AwsClient({
  10. accessKeyId: Resource.AWS_SES_ACCESS_KEY_ID.value,
  11. secretAccessKey: Resource.AWS_SES_SECRET_ACCESS_KEY.value,
  12. region: "us-east-1",
  13. })
  14. }
  15. return client
  16. }
  17. export const sendEmail = fn(
  18. z.object({
  19. to: z.string(),
  20. subject: z.string(),
  21. body: z.string(),
  22. replyTo: z.string().optional(),
  23. }),
  24. async (input) => {
  25. const res = await createClient().fetch("https://email.us-east-1.amazonaws.com/v2/email/outbound-emails", {
  26. method: "POST",
  27. headers: {
  28. "X-Amz-Target": "SES.SendEmail",
  29. "Content-Type": "application/json",
  30. },
  31. body: JSON.stringify({
  32. FromEmailAddress: `OpenCode Zen <[email protected]>`,
  33. Destination: {
  34. ToAddresses: [input.to],
  35. },
  36. ...(input.replyTo && { ReplyToAddresses: [input.replyTo] }),
  37. Content: {
  38. Simple: {
  39. Subject: {
  40. Charset: "UTF-8",
  41. Data: input.subject,
  42. },
  43. Body: {
  44. Text: {
  45. Charset: "UTF-8",
  46. Data: input.body,
  47. },
  48. Html: {
  49. Charset: "UTF-8",
  50. Data: input.body,
  51. },
  52. },
  53. },
  54. },
  55. }),
  56. })
  57. if (!res.ok) {
  58. throw new Error(`Failed to send email: ${res.statusText}`)
  59. }
  60. },
  61. )
  62. }