From d42e2039fc6db77d3319f71cec92f2f198aa40fc Mon Sep 17 00:00:00 2001 From: Rizky Date: Thu, 18 Jul 2024 23:05:23 +0700 Subject: [PATCH] fix kv --- pkgs/api/_kv.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pkgs/api/_kv.ts b/pkgs/api/_kv.ts index e500a79..1bcdbc6 100644 --- a/pkgs/api/_kv.ts +++ b/pkgs/api/_kv.ts @@ -4,7 +4,7 @@ import { dir } from "utils/dir"; import { g } from "utils/global"; export const _ = { - url: "/_kv/**", + url: "/_kv", raw: true, async api() { const { req } = apiContext(this); @@ -14,12 +14,11 @@ export const _ = { } try { - const parts = req.params._.split("/"); + const parts = (await req.json()) as [string, string, any]; switch (parts[0]) { case "set": { - const body = await req.json(); - if (typeof parts[1] === "string" && typeof body !== "undefined") { - g.kv.set(parts[1], body); + if (typeof parts[1] === "string") { + g.kv.set(parts[1], parts[2]); return new Response(JSON.stringify({ status: "ok" }), { headers: { "content-type": "application/json" }, @@ -34,10 +33,23 @@ export const _ = { ); } case "get": { - return new Response(JSON.stringify(g.kv.get(parts[1])), { + if (parts[2]) { + g.kv.set(parts[1], parts[2]); + } + + return new Response(JSON.stringify(g.kv.get(parts[1]) || null), { headers: { "content-type": "application/json" }, }); } + case "del": { + if (parts[1]) { + g.kv.delete(parts[1]); + + return new Response(JSON.stringify({ status: "ok" }), { + headers: { "content-type": "application/json" }, + }); + } + } } } catch (e) {}