Corregge l'accesso all'env Worker per Astro 6.

Sostituisce locals.runtime.env con import da cloudflare:workers, ripristinando contatti e newsletter.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Javaxman
2026-07-29 14:06:16 +02:00
parent 44007f99e1
commit c4adc116d6
3 changed files with 13 additions and 32 deletions

View File

@ -1,6 +1,7 @@
/** /**
* Outbound email via Cloudflare Email Service (`send_email` binding). * Outbound email via Cloudflare Email Service (`send_email` binding).
*/ */
import { env as workerEnv } from 'cloudflare:workers';
export type RuntimeEnv = Record<string, unknown>; export type RuntimeEnv = Record<string, unknown>;
@ -15,8 +16,9 @@ type EmailBinding = {
}) => Promise<{ messageId: string }>; }) => Promise<{ messageId: string }>;
}; };
export function getEmailBinding(runtimeEnv: RuntimeEnv): EmailBinding | null { export function getEmailBinding(runtimeEnv?: RuntimeEnv): EmailBinding | null {
const binding = runtimeEnv.EMAIL; const env = (runtimeEnv ?? (workerEnv as unknown as RuntimeEnv)) as RuntimeEnv;
const binding = env.EMAIL;
if (binding && typeof binding === 'object' && 'send' in binding) { if (binding && typeof binding === 'object' && 'send' in binding) {
return binding as EmailBinding; return binding as EmailBinding;
} }

View File

@ -1,6 +1,7 @@
/** /**
* Shared helpers for newsletter API routes (Cloudflare Worker / Astro). * Shared helpers for newsletter API routes (Cloudflare Worker / Astro).
*/ */
import { env as workerEnv } from 'cloudflare:workers';
export type RuntimeEnv = Record<string, unknown>; export type RuntimeEnv = Record<string, unknown>;
@ -19,20 +20,9 @@ export type NewsletterSubscriber = {
unsubscribed_at: string | null; unsubscribed_at: string | null;
}; };
export function getRuntimeEnv(locals: unknown): RuntimeEnv { /** Astro v6+: use `cloudflare:workers` env (locals.runtime.env was removed). */
if ( export function getRuntimeEnv(_locals?: unknown): RuntimeEnv {
locals && return workerEnv as unknown as RuntimeEnv;
typeof locals === 'object' &&
'runtime' in locals &&
locals.runtime &&
typeof locals.runtime === 'object' &&
'env' in locals.runtime &&
locals.runtime.env &&
typeof locals.runtime.env === 'object'
) {
return locals.runtime.env as RuntimeEnv;
}
return {};
} }
export function readEnv(name: string, runtimeEnv: RuntimeEnv): string { export function readEnv(name: string, runtimeEnv: RuntimeEnv): string {

View File

@ -1,24 +1,13 @@
import type { APIRoute } from 'astro'; import type { APIRoute } from 'astro';
import { env as workerEnv } from 'cloudflare:workers';
import { getEmailBinding, sendCloudflareEmail } from '../../lib/email'; import { getEmailBinding, sendCloudflareEmail } from '../../lib/email';
export const prerender = false; export const prerender = false;
type RuntimeEnv = Record<string, unknown>; type RuntimeEnv = Record<string, unknown>;
function getRuntimeEnv(locals: unknown): RuntimeEnv { function getRuntimeEnv(): RuntimeEnv {
if ( return workerEnv as unknown as RuntimeEnv;
locals &&
typeof locals === 'object' &&
'runtime' in locals &&
locals.runtime &&
typeof locals.runtime === 'object' &&
'env' in locals.runtime &&
locals.runtime.env &&
typeof locals.runtime.env === 'object'
) {
return locals.runtime.env as RuntimeEnv;
}
return {};
} }
function readEnv(name: string, runtimeEnv: RuntimeEnv): string { function readEnv(name: string, runtimeEnv: RuntimeEnv): string {
@ -69,8 +58,8 @@ function resolveDepartmentRecipient(runtimeEnv: RuntimeEnv, defaultRecipient: st
return map[normalized] || defaultRecipient; return map[normalized] || defaultRecipient;
} }
export const POST: APIRoute = async ({ request, locals, url }) => { export const POST: APIRoute = async ({ request, url }) => {
const runtimeEnv = getRuntimeEnv(locals); const runtimeEnv = getRuntimeEnv();
const mode = readEnv('CONTACT_FORM_MODE', runtimeEnv).toLowerCase() || 'dev'; const mode = readEnv('CONTACT_FORM_MODE', runtimeEnv).toLowerCase() || 'dev';
const fromEmail = readEnv('CONTACT_FORM_FROM_EMAIL', runtimeEnv); const fromEmail = readEnv('CONTACT_FORM_FROM_EMAIL', runtimeEnv);
const toEmail = readEnv('CONTACT_FORM_TO_EMAIL', runtimeEnv); const toEmail = readEnv('CONTACT_FORM_TO_EMAIL', runtimeEnv);