diff --git a/docs/examples/creating-a-crud-api.md b/docs/examples/creating-a-crud-api.md new file mode 100644 index 00000000..558949c0 --- /dev/null +++ b/docs/examples/creating-a-crud-api.md @@ -0,0 +1,271 @@ +--- +description: | + Use HTTP CRUD methods to perform operations on resources. Learn how to use HTTP handlers to create a RESTful API. +--- + +The MDN [docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) are a +great resource to learn more about HTTP methods. We'll touch on the four +fundamental methods necessary to create a basic CRUD (create, read, update, +delete) API here. Additionally, we'll briefly mention CORS requests and how +`OPTIONS` comes into play. + +Using HTTP methods is a common way to create a REST API. Fresh supports common +HTTP methods in handlers out of the box. Async HTTP requests are also supported. +Read more about custom handlers [here](/docs/getting-started/custom-handlers). + +In this example we'll be creating a small API that uses +[Deno KV](https://deno.com/kv) to store users in a database. + +Our project structure will look like this (in addition to the rest of the Fresh +code from a new project): + +``` +├── routes +│   └── api +│   └── users +│   ├── [id].ts +│   └── index.ts +``` + +In each section about a method, only the relevant handler will be shown. The +full files are available at the bottom for reference. + +## POST + +`POST` (create) is used to create a resource. + +```tsx +// /routes/api/index.ts +export const handler: Handlers = { + async POST(req, _ctx) { + const user = await req.json() as User; + const userKey = ["user", user.id]; + const ok = await kv.atomic() + .set(userKey, user) + .commit(); + if (!ok) throw new Error("Something went wrong."); + return new Response(JSON.stringify(user)); + }, +}; +``` + +Test this with Postman (or your favorite client) with a URL like +`http://localhost:8000/api/users` and a method of `POST`. Make sure to have a +payload like: + +```json +{ + "id": "2", + "name": "TestUserName" +} +``` + +You should receive the same thing back: + +```json +{ "id": "2", "name": "TestUserName" } +``` + +## GET + +`GET` (read) is used to retrieve a resource and is by far the most common HTTP +method. You can use `GET` to fetch database content, markdown, or static files. + +```tsx +// /routes/api/[id].ts +export const handler: Handlers = { + async GET(_req, ctx) { + const id = ctx.params.id; + const key = ["user", id]; + const user = (await kv.get(key)).value!; + return new Response(JSON.stringify(user)); + }, +}; +``` + +Let's practice retrieving our user! A `GET` request to +`http://localhost:8000/api/users/2` should return: + +```json +{ "id": "2", "name": "TestUserName" } +``` + +## PUT (and PATCH) + +`PUT` (update) and `PATCH` are used to update a resource. While similar, there +are differences and you should use the one that best suits your use case. Read +more about HTTP methods on +[MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods). + +The short version of it: `PUT` requires the entire object to be submitted, while +`PATCH` requires only the properties that are different to be submitted. + +An example of an update endpoint using `PUT`: + +```tsx +// /routes/api/[id].ts +export const handler: Handlers = { + async PUT(req, ctx) { + const id = ctx.params.id; + const user = await req.json() as User; + const userKey = ["user", id]; + const userRes = await kv.get(userKey); + if (!userRes.value) return new Response(`no user with id ${id} found`); + const ok = await kv.atomic() + .check(userRes) + .set(userKey, user) + .commit(); + if (!ok) throw new Error("Something went wrong."); + return new Response(JSON.stringify(user)); + }, +}; +``` + +Time to change their name. We'll now `PUT` a request to +`http://localhost:8000/api/users/2` like: + +```json +{ + "id": "2", + "name": "New Name" +} +``` + +We should receive: + +```json +{ "id": "2", "name": "New Name" } +``` + +If, on the other hand, we chose to implement this as a `PATCH` operation, the +request would just involve the changed property like this: + +```json +{ + "name": "New Name" +} +``` + +No need to send in the id in this case. + +## DELETE + +`DELETE` (delete) is used to delete a resource. + +```tsx +// /routes/api/[id].ts +export const handler: Handlers = { + async DELETE(_req, ctx) { + const id = ctx.params.id; + const userKey = ["user", id]; + const userRes = await kv.get(userKey); + if (!userRes.value) return new Response(`no user with id ${id} found`); + const ok = await kv.atomic() + .check(userRes) + .delete(userKey) + .commit(); + if (!ok) throw new Error("Something went wrong."); + return new Response(`user ${id} deleted`); + }, +}; +``` + +Try sending `DELETE` to `http://localhost:8000/api/users/2` without a body. +We'll get back: + +``` +user 2 deleted +``` + +## OPTIONS + +Options can be used for some advanced cases, including implementing preflight +request checks for complex CORS use cases. See more on the +[CORS documentation](/docs/examples/dealing-with-cors). + +## Full File Reference + +
+[id].ts + +```ts +import { Handlers } from "$fresh/server.ts"; + +type User = { + id: string; + name: string; +}; + +const kv = await Deno.openKv(); + +export const handler: Handlers = { + async GET(_req, ctx) { + const id = ctx.params.id; + const key = ["user", id]; + const user = (await kv.get(key)).value!; + return new Response(JSON.stringify(user)); + }, + async DELETE(_req, ctx) { + const id = ctx.params.id; + const userKey = ["user", id]; + const userRes = await kv.get(userKey); + if (!userRes.value) return new Response(`no user with id ${id} found`); + const ok = await kv.atomic() + .check(userRes) + .delete(userKey) + .commit(); + if (!ok) throw new Error("Something went wrong."); + return new Response(`user ${id} deleted`); + }, + async PUT(req, ctx) { + const id = ctx.params.id; + const user = await req.json() as User; + const userKey = ["user", id]; + const userRes = await kv.get(userKey); + if (!userRes.value) return new Response(`no user with id ${id} found`); + const ok = await kv.atomic() + .check(userRes) + .set(userKey, user) + .commit(); + if (!ok) throw new Error("Something went wrong."); + return new Response(JSON.stringify(user)); + }, +}; +``` + +
+ +
+index.ts + +```ts +import { Handlers } from "$fresh/server.ts"; + +type User = { + id: string; + name: string; +}; + +const kv = await Deno.openKv(); + +export const handler: Handlers = { + async GET(_req, _ctx) { + const users = []; + for await (const res of kv.list({ prefix: ["user"] })) { + users.push(res.value); + } + return new Response(JSON.stringify(users)); + }, + async POST(req, _ctx) { + const user = await req.json() as User; + const userKey = ["user", user.id]; + const ok = await kv.atomic() + .set(userKey, user) + .commit(); + if (!ok) throw new Error("Something went wrong."); + return new Response(JSON.stringify(user)); + }, +}; +``` + +
diff --git a/docs/examples/index.md b/docs/examples/index.md index 84a3c619..ea880b8d 100644 --- a/docs/examples/index.md +++ b/docs/examples/index.md @@ -15,3 +15,4 @@ like to see here, please open - [Initializing the server](./examples/init-the-server) - [Using Fresh canary version](./examples/using-fresh-canary-version) - [Dealing with CORS](./examples/dealing-with-cors) +- [Creating a CRUD API](./examples/creating-a-crud-api) diff --git a/docs/toc.json b/docs/toc.json index 204732e9..621ba22d 100644 --- a/docs/toc.json +++ b/docs/toc.json @@ -48,7 +48,8 @@ ["using-twind-v1", "Using Twind v1"], ["init-the-server", "Initializing the server"], ["using-fresh-canary-version", "Using Fresh canary version"], - ["dealing-with-cors", "Dealing with CORS"] + ["dealing-with-cors", "Dealing with CORS"], + ["creating-a-crud-api", "Creating a CRUD API"] ] } }