|
| 1 | +import { ContactFormSchema, type ContactFormValues } from "./schema"; |
| 2 | + |
| 3 | +interface Env { |
| 4 | + GITHUB_ISSUE_TOKEN: string; |
| 5 | + GITHUB_ISSUE_REPO: string; |
| 6 | +} |
| 7 | + |
| 8 | +interface GithubIssuePayload { |
| 9 | + title: string; |
| 10 | + body: string; |
| 11 | + labels?: string[]; |
| 12 | +} |
| 13 | + |
| 14 | +function jsonResponse(data: unknown, status: number): Response { |
| 15 | + return new Response(JSON.stringify(data), { |
| 16 | + status, |
| 17 | + headers: { "Content-Type": "application/json" }, |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function buildIssueTitle(message: string, email: string): string { |
| 22 | + const oneLine = message.replace(/\s+/g, " ").trim(); |
| 23 | + const snippet = oneLine.slice(0, 60); |
| 24 | + const truncated = oneLine.length > 60 ? `${snippet}…` : snippet; |
| 25 | + return `[Contact] ${truncated || email}`; |
| 26 | +} |
| 27 | + |
| 28 | +function buildIssueBody( |
| 29 | + data: Pick<ContactFormValues, "email" | "message">, |
| 30 | +): string { |
| 31 | + return [ |
| 32 | + `**From:** ${data.email}`, |
| 33 | + `**Received:** ${new Date().toISOString()}`, |
| 34 | + "", |
| 35 | + "---", |
| 36 | + "", |
| 37 | + data.message, |
| 38 | + ].join("\n"); |
| 39 | +} |
| 40 | + |
| 41 | +async function createGithubIssue( |
| 42 | + env: Env, |
| 43 | + payload: GithubIssuePayload, |
| 44 | +): Promise<{ ok: true } | { ok: false; status: number; error: string }> { |
| 45 | + const res = await fetch( |
| 46 | + `https://api.github.com/repos/${env.GITHUB_ISSUE_REPO}/issues`, |
| 47 | + { |
| 48 | + method: "POST", |
| 49 | + headers: { |
| 50 | + Authorization: `Bearer ${env.GITHUB_ISSUE_TOKEN}`, |
| 51 | + Accept: "application/vnd.github+json", |
| 52 | + "X-GitHub-Api-Version": "2022-11-28", |
| 53 | + "User-Agent": "firstfluke-contact-worker", |
| 54 | + }, |
| 55 | + body: JSON.stringify(payload), |
| 56 | + }, |
| 57 | + ); |
| 58 | + if (!res.ok) { |
| 59 | + const error = await res.text(); |
| 60 | + return { ok: false, status: res.status, error }; |
| 61 | + } |
| 62 | + return { ok: true }; |
| 63 | +} |
| 64 | + |
| 65 | +export default { |
| 66 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 67 | + if (request.method !== "POST") { |
| 68 | + return new Response(null, { status: 405 }); |
| 69 | + } |
| 70 | + |
| 71 | + let payload: unknown; |
| 72 | + try { |
| 73 | + payload = await request.json(); |
| 74 | + } catch { |
| 75 | + return jsonResponse({ ok: false, error: "validation" }, 400); |
| 76 | + } |
| 77 | + |
| 78 | + const parsed = ContactFormSchema.safeParse(payload); |
| 79 | + if (!parsed.success) { |
| 80 | + return jsonResponse( |
| 81 | + { |
| 82 | + ok: false, |
| 83 | + error: "validation", |
| 84 | + fields: parsed.error.flatten().fieldErrors, |
| 85 | + }, |
| 86 | + 400, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + if (parsed.data._hp && parsed.data._hp.length > 0) { |
| 91 | + return jsonResponse({ ok: true }, 200); |
| 92 | + } |
| 93 | + |
| 94 | + if (!env.GITHUB_ISSUE_TOKEN || !env.GITHUB_ISSUE_REPO) { |
| 95 | + // TODO(oma-deferred): set GITHUB_ISSUE_TOKEN and GITHUB_ISSUE_REPO via `wrangler secret put`. |
| 96 | + console.info( |
| 97 | + "[contact] GitHub credentials missing. Payload acknowledged:", |
| 98 | + { |
| 99 | + email: parsed.data.email, |
| 100 | + messageLength: parsed.data.message.length, |
| 101 | + }, |
| 102 | + ); |
| 103 | + return jsonResponse({ ok: true }, 200); |
| 104 | + } |
| 105 | + |
| 106 | + const result = await createGithubIssue(env, { |
| 107 | + title: buildIssueTitle(parsed.data.message, parsed.data.email), |
| 108 | + body: buildIssueBody(parsed.data), |
| 109 | + labels: ["contact"], |
| 110 | + }); |
| 111 | + |
| 112 | + if (!result.ok) { |
| 113 | + console.error( |
| 114 | + "[contact] github issue create failed:", |
| 115 | + result.status, |
| 116 | + result.error, |
| 117 | + ); |
| 118 | + return jsonResponse({ ok: false, error: "send_failed" }, 502); |
| 119 | + } |
| 120 | + |
| 121 | + return jsonResponse({ ok: true }, 200); |
| 122 | + }, |
| 123 | +} satisfies ExportedHandler<Env>; |
0 commit comments