aws.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. }),
  23. async (input) => {
  24. const res = await createClient().fetch("https://email.us-east-1.amazonaws.com/v2/email/outbound-emails", {
  25. method: "POST",
  26. headers: {
  27. "X-Amz-Target": "SES.SendEmail",
  28. "Content-Type": "application/json",
  29. },
  30. body: JSON.stringify({
  31. FromEmailAddress: `OpenCode Zen <[email protected]>`,
  32. Destination: {
  33. ToAddresses: [input.to],
  34. },
  35. Content: {
  36. Simple: {
  37. Subject: {
  38. Charset: "UTF-8",
  39. Data: input.subject,
  40. },
  41. Body: {
  42. Text: {
  43. Charset: "UTF-8",
  44. Data: input.body,
  45. },
  46. Html: {
  47. Charset: "UTF-8",
  48. Data: input.body,
  49. },
  50. },
  51. },
  52. },
  53. }),
  54. })
  55. if (!res.ok) {
  56. throw new Error(`Failed to send email: ${res.statusText}`)
  57. }
  58. },
  59. )
  60. }