Zum Inhalt springen

Hono

Terminal-Fenster
npm install hono @spacepost/node
src/index.ts
import { Hono } from "hono";
import { SpacePost } from "@spacepost/node";
type Env = { Bindings: { SPACEPOST_API_KEY: string; SPACEPOST_WEBHOOK_SECRET: string } };
const app = new Hono<Env>();
app.post("/send", async (c) => {
const spacepost = new SpacePost(c.env.SPACEPOST_API_KEY);
const { to } = await c.req.json<{ to: string }>();
const { data, error } = await spacepost.emails.send({
from: "Acme <hello@acme.com>",
to,
subject: "Hello from Hono",
html: "<p>Sent from the edge.</p>",
});
if (error) return c.json(error, (error.statusCode ?? 500) as 400);
return c.json(data);
});
app.post("/webhooks/spacepost", async (c) => {
const spacepost = new SpacePost(c.env.SPACEPOST_API_KEY);
try {
const event = await spacepost.webhooks.verify({
payload: await c.req.text(), // raw body
headers: c.req.raw.headers,
secret: c.env.SPACEPOST_WEBHOOK_SECRET,
});
console.log(event.type);
return c.body(null, 200);
} catch {
return c.body(null, 400);
}
});
export default app;