2 Commits

Author SHA1 Message Date
c4adc116d6 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>
2026-07-29 14:06:16 +02:00
44007f99e1 Merge pull request #14 from javaxman/feat/cloudflare-email-sending
Sostituisce Resend con Cloudflare Email Service
2026-07-29 09:55:05 +02:00
3 changed files with 13 additions and 32 deletions

View File

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

View File

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

View File

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