Skip to content

Commit 6fb73f8

Browse files
committed
Use astro:env for RESEND env vars
1 parent e81ab97 commit 6fb73f8

2 files changed

Lines changed: 12 additions & 36 deletions

File tree

‎src/pages/api/newsletter/_email.ts‎

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,15 @@
44
*/
55

66
import { Resend } from 'resend'
7-
import { ClientScriptError } from '@components/scripts/errors/ClientScriptError'
7+
import { RESEND_API_KEY } from 'astro:env/server'
8+
import { ClientScriptError } from '@components/scripts/errors'
9+
import { getSiteUrl, isDev, isTest } from '@components/scripts/utils'
810

911
/**
1012
* Initialize Resend client
11-
* API key must be set in RESEND_API_KEY environment variable
1213
*/
1314
function getResendClient(): Resend {
14-
const apiKey = import.meta.env.RESEND_API_KEY
15-
if (!apiKey) {
16-
throw new ClientScriptError({
17-
message: `RESEND_API_KEY environment variable is not set`
18-
})
19-
}
20-
return new Resend(apiKey)
21-
}
22-
23-
/**
24-
* Get the site URL from environment or default to localhost
25-
*/
26-
function getSiteUrl(): string {
27-
return import.meta.env.SITE || 'http://localhost:4321'
15+
return new Resend(RESEND_API_KEY)
2816
}
2917

3018
/**
@@ -206,9 +194,7 @@ export async function sendConfirmationEmail(
206194
firstName?: string
207195
): Promise<void> {
208196
// Skip actual email sending in dev/test environments
209-
const isDevOrTest = process.env['NODE_ENV'] === 'development' || process.env['NODE_ENV'] === 'test' || process.env['CI'] === 'true'
210-
211-
if (isDevOrTest) {
197+
if (isDev() || isTest()) {
212198
console.log('[DEV/TEST MODE] Newsletter confirmation email would be sent:', { email, token })
213199
return
214200
}
@@ -261,9 +247,7 @@ export async function sendWelcomeEmail(
261247
firstName?: string
262248
): Promise<void> {
263249
// Skip actual email sending in dev/test environments
264-
const isDevOrTest = process.env['NODE_ENV'] === 'development' || process.env['NODE_ENV'] === 'test' || process.env['CI'] === 'true'
265-
266-
if (isDevOrTest) {
250+
if (isDev() || isTest()) {
267251
console.log('[DEV/TEST MODE] Newsletter welcome email would be sent:', { email })
268252
return
269253
}

‎src/pages/api/newsletter/index.ts‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
*/
77
import type { APIRoute } from 'astro'
88
import { v4 as uuidv4, validate as uuidValidate } from 'uuid'
9-
import { ClientScriptError } from '@components/scripts/errors/ClientScriptError'
9+
import { CONVERTKIT_API_KEY } from 'astro:env/server'
10+
import { ClientScriptError } from '@components/scripts/errors'
11+
import { isDev, isTest } from '@components/scripts/utils'
1012
import { rateLimiters, checkRateLimit } from '@pages/api/_utils/rateLimit'
1113
import type { ErrorResponse } from '@pages/api/_contracts/gdpr.contracts'
1214
import { createPendingSubscription } from './_token'
@@ -78,9 +80,7 @@ export async function subscribeToConvertKit(
7880
data: NewsletterFormData
7981
): Promise<ConvertKitResponse> {
8082
// Skip actual ConvertKit API call in dev/test environments
81-
const isDevOrTest = import.meta.env.DEV || import.meta.env.MODE === 'test' || process.env['NODE_ENV'] === 'test'
82-
83-
if (isDevOrTest) {
83+
if (isDev() || isTest()) {
8484
console.log('[DEV/TEST MODE] Newsletter subscription would be created:', { email: data.email })
8585
// Return mock success response
8686
return {
@@ -97,14 +97,6 @@ export async function subscribeToConvertKit(
9797
}
9898
}
9999

100-
const apiKey = import.meta.env['CONVERTKIT_API_KEY']
101-
102-
if (!apiKey) {
103-
throw new ClientScriptError({
104-
message: 'ConvertKit API key is not configured.'
105-
})
106-
}
107-
108100
/* eslint-disable camelcase */
109101
const subscriberData: ConvertKitSubscriber = {
110102
email_address: data.email,
@@ -121,7 +113,7 @@ export async function subscribeToConvertKit(
121113
method: 'POST',
122114
headers: {
123115
'Content-Type': 'application/json',
124-
'X-Kit-Api-Key': apiKey,
116+
'X-Kit-Api-Key': CONVERTKIT_API_KEY,
125117
},
126118
body: JSON.stringify(subscriberData),
127119
})
@@ -247,7 +239,7 @@ export const POST: APIRoute = async ({ request, clientAddress }) => {
247239
...(firstName && { firstName }),
248240
DataSubjectId: subjectId,
249241
userAgent: request.headers.get('user-agent') || 'unknown',
250-
...(clientAddress !== 'unknown' && { clientAddress: ip }),
242+
...(clientAddress !== 'unknown' && { clientAddress }),
251243
source: 'newsletter_form',
252244
})
253245

0 commit comments

Comments
 (0)