|
| 1 | +import assert from "node:assert"; |
| 2 | +import { serve } from "@hono/node-server"; |
| 3 | +import { connect, type Framer } from "framer-api"; |
| 4 | +import { Hono } from "hono"; |
| 5 | + |
| 6 | +const projectUrl = process.env["EXAMPLE_PROJECT_URL"]; |
| 7 | +assert(projectUrl, "EXAMPLE_PROJECT_URL environment variable is required"); |
| 8 | + |
| 9 | +interface Variables { |
| 10 | + framer: Framer; |
| 11 | +} |
| 12 | + |
| 13 | +const app = new Hono<{ Variables: Variables }>(); |
| 14 | + |
| 15 | +// Middleware to connect to Framer and set the framer client in the context. |
| 16 | +// Will not share the instance between requests |
| 17 | +app.use(async (c, next) => { |
| 18 | + // The `using` keyword is used to ensure that the Framer client is closed after the block is executed. |
| 19 | + // This will automatically end the connection when the request is complete |
| 20 | + // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using |
| 21 | + using framer = await connect(projectUrl); |
| 22 | + c.set("framer", framer); |
| 23 | + await next(); |
| 24 | +}); |
| 25 | + |
| 26 | +app.get("/collections", async (c) => { |
| 27 | + const allCollections = await c.var.framer.getCollections(); |
| 28 | + |
| 29 | + const collections = allCollections.map((col) => ({ |
| 30 | + id: col.id, |
| 31 | + name: col.name, |
| 32 | + })); |
| 33 | + |
| 34 | + return c.json({ collections }); |
| 35 | +}); |
| 36 | + |
| 37 | +app.get("/collections/:collectionId", async (c) => { |
| 38 | + const collectionId = c.req.param("collectionId"); |
| 39 | + const allCollections = await c.var.framer.getCollections(); |
| 40 | + const collection = allCollections.find((col) => col.id === collectionId); |
| 41 | + |
| 42 | + if (!collection) { |
| 43 | + return c.json({ error: "Collection not found" }, 404); |
| 44 | + } |
| 45 | + |
| 46 | + const allItems = await collection.getItems(); |
| 47 | + |
| 48 | + const items = allItems.map((item) => ({ |
| 49 | + id: item.id, |
| 50 | + slug: item.slug, |
| 51 | + })); |
| 52 | + |
| 53 | + return c.json({ items }); |
| 54 | +}); |
| 55 | + |
| 56 | +app.get("/collections/:collectionId/:itemId", async (c) => { |
| 57 | + const collectionId = c.req.param("collectionId"); |
| 58 | + const itemId = c.req.param("itemId"); |
| 59 | + const allCollections = await c.var.framer.getCollections(); |
| 60 | + const collection = allCollections.find((col) => col.id === collectionId); |
| 61 | + |
| 62 | + if (!collection) { |
| 63 | + return c.json({ error: "Collection not found" }, 404); |
| 64 | + } |
| 65 | + |
| 66 | + const allItems = await collection.getItems(); |
| 67 | + |
| 68 | + const item = allItems.find((i) => i.id === itemId); |
| 69 | + |
| 70 | + if (!item) { |
| 71 | + return c.json({ error: "Item not found" }, 404); |
| 72 | + } |
| 73 | + |
| 74 | + return c.json(item); |
| 75 | +}); |
| 76 | + |
| 77 | +serve(app, (info) => { |
| 78 | + console.log(`Server running at http://localhost:${info.port}`); |
| 79 | +}); |
0 commit comments