-
Notifications
You must be signed in to change notification settings - Fork 0
Introducing JSON server example #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # JSON API | ||
|
|
||
| A simple HTTP/JSON server that exposes Framer collections via REST endpoints using [Hono](https://hono.dev). | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| EXAMPLE_PROJECT_URL="https://framer.com/projects/..." npm run dev | ||
| ``` | ||
|
|
||
| The server runs on port 3000 by default. | ||
|
|
||
| ## Endpoints | ||
|
|
||
| ### List Collections | ||
|
|
||
| ``` | ||
| GET /collections | ||
| ``` | ||
|
|
||
| Returns all collections in the project. | ||
|
|
||
| ### List Items | ||
|
|
||
| ``` | ||
| GET /collections/:collectionId | ||
| ``` | ||
|
|
||
| Returns all items in a collection by ID. | ||
|
|
||
| ### Get Item | ||
|
|
||
| ``` | ||
| GET /collections/:collectionId/:itemId | ||
| ``` | ||
|
|
||
| Returns a single item with its field data. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "name": "json-api", | ||
| "version": "0.0.1", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "node --watch src/index.ts", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "dependencies": { | ||
| "@hono/node-server": "^1.14.1", | ||
| "framer-api": "^0.0.1-beta.0", | ||
| "hono": "^4.7.10", | ||
| "typescript": "^5.9.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^22.10.2" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import assert from "node:assert"; | ||
| import { serve } from "@hono/node-server"; | ||
| import { connect, type Framer } from "framer-api"; | ||
| import { Hono } from "hono"; | ||
|
|
||
| const projectUrl = process.env["EXAMPLE_PROJECT_URL"]; | ||
| assert(projectUrl, "EXAMPLE_PROJECT_URL environment variable is required"); | ||
|
|
||
| interface Variables { | ||
| framer: Framer; | ||
| } | ||
|
|
||
| const app = new Hono<{ Variables: Variables }>(); | ||
|
|
||
| // Middleware to connect to Framer and set the framer client in the context. | ||
| // Will not share the instance between requests | ||
| app.use(async (c, next) => { | ||
| // The `using` keyword is used to ensure that the Framer client is closed after the block is executed. | ||
| // This will automatically end the connection when the request is complete | ||
| // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using | ||
| using framer = await connect(projectUrl); | ||
| c.set("framer", framer); | ||
| await next(); | ||
| }); | ||
|
|
||
| app.get("/collections", async (c) => { | ||
| const allCollections = await c.var.framer.getCollections(); | ||
|
|
||
| const collections = allCollections.map((col) => ({ | ||
| id: col.id, | ||
| name: col.name, | ||
| })); | ||
|
|
||
| return c.json({ collections }); | ||
| }); | ||
|
|
||
| app.get("/collections/:collectionId", async (c) => { | ||
| const collectionId = c.req.param("collectionId"); | ||
| const allCollections = await c.var.framer.getCollections(); | ||
| const collection = allCollections.find((col) => col.id === collectionId); | ||
|
|
||
| if (!collection) { | ||
| return c.json({ error: "Collection not found" }, 404); | ||
| } | ||
|
|
||
| const allItems = await collection.getItems(); | ||
|
|
||
| const items = allItems.map((item) => ({ | ||
| id: item.id, | ||
| slug: item.slug, | ||
| })); | ||
|
|
||
| return c.json({ items }); | ||
| }); | ||
|
|
||
| app.get("/collections/:collectionId/:itemId", async (c) => { | ||
| const collectionId = c.req.param("collectionId"); | ||
| const itemId = c.req.param("itemId"); | ||
| const allCollections = await c.var.framer.getCollections(); | ||
| const collection = allCollections.find((col) => col.id === collectionId); | ||
|
|
||
| if (!collection) { | ||
| return c.json({ error: "Collection not found" }, 404); | ||
| } | ||
|
|
||
| const allItems = await collection.getItems(); | ||
|
|
||
| const item = allItems.find((i) => i.id === itemId); | ||
|
|
||
| if (!item) { | ||
| return c.json({ error: "Item not found" }, 404); | ||
| } | ||
|
|
||
| return c.json(item); | ||
| }); | ||
|
|
||
| serve(app, (info) => { | ||
| console.log(`Server running at http://localhost:${info.port}`); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "include": ["**/*.ts"] | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Dev script runs TypeScript without TypeScript runtime
The
devscript usesnode --watch src/index.tsto run a TypeScript file directly, but Node.js cannot execute TypeScript files without either a TypeScript runtime liketsxor experimental flags. Thecsv-importerexample correctly addstsxto devDependencies for this purpose, butjson-apilacks this dependency. Runningnpm run devwill fail with a syntax error on most Node.js versions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
works fine with modern node versions. Welcome to the future, LLM.