docs: add "Creating a CRUD API" example (#1361)

closes https://github.com/denoland/fresh/issues/768
supersedes https://github.com/denoland/fresh/pull/853

I'm not sure if this is what Ryan had in mind when opened 768. But this
PR includes some working code, references Deno KV, and briefly touches
on `OPTIONS` as a bonus.

Should I link to https://deno.com/blog/build-crud-api-oak-denokv where I
borrowed some code from? I must admit I'm a bit confused about why Andy
chose to write about Oak instead of Fresh in this post...
This commit is contained in:
Reed von Redwitz
2023-06-27 09:18:49 +02:00
committed by GitHub
parent 2b92b1c526
commit ee3e4a1145
3 changed files with 274 additions and 1 deletions
+271
View File
@@ -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<User | null> = {
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<User | null> = {
async GET(_req, ctx) {
const id = ctx.params.id;
const key = ["user", id];
const user = (await kv.get<User>(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<User | null> = {
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<User | null> = {
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
<details>
<summary>[id].ts</summary>
```ts
import { Handlers } from "$fresh/server.ts";
type User = {
id: string;
name: string;
};
const kv = await Deno.openKv();
export const handler: Handlers<User | null> = {
async GET(_req, ctx) {
const id = ctx.params.id;
const key = ["user", id];
const user = (await kv.get<User>(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));
},
};
```
</details>
<details>
<summary>index.ts</summary>
```ts
import { Handlers } from "$fresh/server.ts";
type User = {
id: string;
name: string;
};
const kv = await Deno.openKv();
export const handler: Handlers<User | null> = {
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));
},
};
```
</details>
+1
View File
@@ -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)