66 */
77import type { APIRoute } from 'astro'
88import { 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'
1012import { rateLimiters , checkRateLimit } from '@pages/api/_utils/rateLimit'
1113import type { ErrorResponse } from '@pages/api/_contracts/gdpr.contracts'
1214import { 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