summaryrefslogtreecommitdiffhomepage
path: root/packages/console/core/src/aws.ts
blob: ce4a20f447c8a0bf8eea2073a9ced1f27b889f1c (plain)
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
import { z } from "zod"
import { Resource } from "@opencode-ai/console-resource"
import { AwsClient } from "aws4fetch"
import { fn } from "./util/fn"

export namespace AWS {
  let client: AwsClient

  const createClient = () => {
    if (!client) {
      client = new AwsClient({
        accessKeyId: Resource.AWS_SES_ACCESS_KEY_ID.value,
        secretAccessKey: Resource.AWS_SES_SECRET_ACCESS_KEY.value,
        region: "us-east-1",
      })
    }
    return client
  }

  export const sendEmail = fn(
    z.object({
      to: z.string(),
      subject: z.string(),
      body: z.string(),
    }),
    async (input) => {
      const res = await createClient().fetch(
        "https://email.us-east-1.amazonaws.com/v2/email/outbound-emails",
        {
          method: "POST",
          headers: {
            "X-Amz-Target": "SES.SendEmail",
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            FromEmailAddress: `OpenCode Zen <[email protected]>`,
            Destination: {
              ToAddresses: [input.to],
            },
            Content: {
              Simple: {
                Subject: {
                  Charset: "UTF-8",
                  Data: input.subject,
                },
                Body: {
                  Text: {
                    Charset: "UTF-8",
                    Data: input.body,
                  },
                  Html: {
                    Charset: "UTF-8",
                    Data: input.body,
                  },
                },
              },
            },
          }),
        },
      )
      if (!res.ok) {
        throw new Error(`Failed to send email: ${res.statusText}`)
      }
    },
  )
}