Compare commits
2 Commits
feat/cloud
...
fix/astro6
| Author | SHA1 | Date | |
|---|---|---|---|
| c4adc116d6 | |||
| 44007f99e1 |
@ -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;
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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);
|
||||
|
||||
Reference in New Issue
Block a user