(null);
-// LeafletProvider component manages Leaflet loading and context
export function LeafletProvider(props: { children: ComponentChildren }) {
if (!IS_BROWSER) {
return Leaflet must be loaded on the client. No children will render
;
diff --git a/src/routes/api/users/[id].ts b/src/routes/api/users/[id].ts
index 333d0e8..8ff9dbf 100644
--- a/src/routes/api/users/[id].ts
+++ b/src/routes/api/users/[id].ts
@@ -1,39 +1,39 @@
import { Handlers } from "$fresh/server.ts";
import { kv } from "#src/services/database.ts";
import type { User } from "#src/shared/api.ts";
+import { getUserKey, handleUserNotFound, statusCheck } from "#src/utils/api.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));
+ const user = (await kv.get(getUserKey(id))).value;
+ return user ? new Response(JSON.stringify(user)) : handleUserNotFound(id);
},
async PUT(req, ctx) {
const id = ctx.params.id;
const user = (await req.json()) as User;
- const userKey = ["user", id];
+ const userKey = getUserKey(id);
const userRes = await kv.get(userKey);
- if (!userRes.value) return new Response(`no user with id ${id} found`);
+ if (!userRes.value) return handleUserNotFound(id);
const ok = await kv.atomic().check(userRes).set(userKey, user).commit();
- if (!ok) throw new Error("Something went wrong.");
+ statusCheck(ok);
return new Response(JSON.stringify(user));
},
async DELETE(_req, ctx) {
const id = ctx.params.id;
- const userKey = ["user", id];
+ const userKey = getUserKey(id);
const userRes = await kv.get(userKey);
- if (!userRes.value) return new Response(`no user with id ${id} found`);
+ if (!userRes.value) return handleUserNotFound(id);
const ok = await kv.atomic().check(userRes).delete(userKey).commit();
- if (!ok) throw new Error("Something went wrong.");
+ statusCheck(ok);
- return new Response(`user ${id} deleted`);
+ return new Response(`User ${id} deleted`);
},
};
diff --git a/src/routes/api/users/index.ts b/src/routes/api/users/index.ts
index eaa7203..614fd28 100644
--- a/src/routes/api/users/index.ts
+++ b/src/routes/api/users/index.ts
@@ -1,14 +1,15 @@
import { Handlers } from "$fresh/server.ts";
import { kv } from "#src/services/database.ts";
import type { User } from "#src/shared/api.ts";
+import { getUserKey, statusCheck } from "#src/utils/api.ts";
export const handler: Handlers = {
async POST(req, _ctx) {
const user = (await req.json()) as User;
- const userKey = ["user", user.id];
+ const userKey = getUserKey(user.id);
const ok = await kv.atomic().set(userKey, user).commit();
- if (!ok) throw new Error("Something went wrong.");
+ statusCheck(ok);
return new Response(JSON.stringify(user));
},
diff --git a/src/utils/api.ts b/src/utils/api.ts
new file mode 100644
index 0000000..7615fc0
--- /dev/null
+++ b/src/utils/api.ts
@@ -0,0 +1,10 @@
+export const statusCheck = (
+ status: Deno.KvCommitResult | Deno.KvCommitError,
+) => {
+ if (!status) throw new Error("Something went wrong.");
+};
+
+export const getUserKey = (id: string) => ["user", id];
+
+export const handleUserNotFound = (id: string) =>
+ new Response(`No user with id ${id} found`);