Skip to content

Commit 75d9ce5

Browse files
authored
Merge pull request #14 from nyaomaru/deno-study-10
study: kv POST
2 parents dbaf249 + affd9c4 commit 75d9ce5

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

deno.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44
"check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
55
"cli": "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run --unstable -A -",
66
"manifest": "deno task cli manifest $(pwd)",
7-
"start": "deno run -A --watch=src/static/,src/routes/ src/dev.ts",
7+
"start": "deno run -A --watch=src/static/,src/routes/ --unstable src/dev.ts",
88
"build": "deno run -A src/dev.ts build",
99
"preview": "deno run -A src/main.ts",
1010
"update": "deno run -A -r https://fresh.deno.dev/update ."
1111
},
12-
"lint": {
13-
"rules": {
14-
"tags": ["fresh", "recommended"]
15-
}
16-
},
12+
"lint": { "rules": { "tags": ["fresh", "recommended"] } },
1713
"exclude": ["**/_fresh/*"],
1814
"imports": {
1915
"$fresh/": "https://deno.land/x/fresh@1.6.8/",
@@ -27,9 +23,6 @@
2723
"$std/": "https://deno.land/std@0.216.0/",
2824
"$fresh_charts/": "https://deno.land/x/fresh_charts@0.3.1/"
2925
},
30-
"compilerOptions": {
31-
"jsx": "react-jsx",
32-
"jsxImportSource": "preact"
33-
},
26+
"compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
3427
"nodeModulesDir": true
3528
}

src/fresh.gen.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import * as $_middleware from "./routes/_middleware.ts";
1010
import * as $about from "./routes/about.tsx";
1111
import * as $api_joke from "./routes/api/joke.ts";
1212
import * as $api_random_uuid from "./routes/api/random-uuid.ts";
13+
import * as $api_users_id_ from "./routes/api/users/[id].ts";
14+
import * as $api_users_index from "./routes/api/users/index.ts";
1315
import * as $chart from "./routes/chart.tsx";
1416
import * as $countdown from "./routes/countdown.tsx";
1517
import * as $greet_name_ from "./routes/greet/[name].tsx";
@@ -35,6 +37,8 @@ const manifest = {
3537
"./routes/about.tsx": $about,
3638
"./routes/api/joke.ts": $api_joke,
3739
"./routes/api/random-uuid.ts": $api_random_uuid,
40+
"./routes/api/users/[id].ts": $api_users_id_,
41+
"./routes/api/users/index.ts": $api_users_index,
3842
"./routes/chart.tsx": $chart,
3943
"./routes/countdown.tsx": $countdown,
4044
"./routes/greet/[name].tsx": $greet_name_,

src/routes/api/users/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Handlers } from "$fresh/server.ts";
2+
3+
export const kv = await Deno.openKv();
4+
5+
interface User {
6+
id: string;
7+
name: string;
8+
}
9+
10+
export const handler: Handlers<User | null> = {
11+
async POST(req, _ctx) {
12+
const user = (await req.json()) as User;
13+
const userKey = ["user", user.id];
14+
const ok = await kv.atomic().set(userKey, user).commit();
15+
if (!ok) throw new Error("Something went wrong.");
16+
return new Response(JSON.stringify(user));
17+
},
18+
};

0 commit comments

Comments
 (0)