From d592137f360022bd2df2b1ce249fc0e7d1e2f757 Mon Sep 17 00:00:00 2001 From: kirinnee Date: Sun, 29 Dec 2024 10:41:52 +0800 Subject: [PATCH] fix: email submission --- astro.config.mjs | 7 ++++++- src/actions/index.ts | 7 ++++--- src/env.d.ts | 3 --- 3 files changed, 10 insertions(+), 7 deletions(-) delete mode 100644 src/env.d.ts diff --git a/astro.config.mjs b/astro.config.mjs index 3f6a588..e9b4ef9 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,5 +1,5 @@ // @ts-check -import { defineConfig } from 'astro/config'; +import { defineConfig, envField } from 'astro/config'; import tailwind from '@astrojs/tailwind'; import react from '@astrojs/react'; @@ -7,6 +7,11 @@ import react from '@astrojs/react'; import cloudflare from '@astrojs/cloudflare'; export default defineConfig({ + env: { + schema: { + BREVO_API_KEY: envField.string({ context: 'server', access: 'secret', optional: false }), + }, + }, output: 'server', integrations: [ tailwind({ diff --git a/src/actions/index.ts b/src/actions/index.ts index abfc225..09dcd72 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -1,6 +1,6 @@ import { defineAction, ActionError } from 'astro:actions'; import { z } from 'astro:schema'; -const { env } = Astro.locals.runtime; +import { getSecret } from 'astro:env/server'; export const server = { submitWaitlistEmail: defineAction({ @@ -8,12 +8,14 @@ export const server = { email: z.string().email(), }), handler: async ({ email }) => { + const apiKey = getSecret('BREVO_API_KEY'); + const options = { method: 'POST', headers: { accept: 'application/json', 'content-type': 'application/json', - 'api-key': env.BREVO_API_KEY, + 'api-key': apiKey, }, body: JSON.stringify({ email, @@ -24,7 +26,6 @@ export const server = { const r = await fetch('https://api.brevo.com/v3/contacts', options); if (!r.ok) { - console.log(import.meta.env.BREVO_API_KEY); console.error(r.statusText); console.error(r.status); console.error(r.url); diff --git a/src/env.d.ts b/src/env.d.ts deleted file mode 100644 index 7e01c63..0000000 --- a/src/env.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -interface ImportMetaEnv { - readonly BREVO_API_KEY: string; -}