fix kv
This commit is contained in:
parent
b7ff711617
commit
d42e2039fc
|
|
@ -4,7 +4,7 @@ import { dir } from "utils/dir";
|
||||||
import { g } from "utils/global";
|
import { g } from "utils/global";
|
||||||
|
|
||||||
export const _ = {
|
export const _ = {
|
||||||
url: "/_kv/**",
|
url: "/_kv",
|
||||||
raw: true,
|
raw: true,
|
||||||
async api() {
|
async api() {
|
||||||
const { req } = apiContext(this);
|
const { req } = apiContext(this);
|
||||||
|
|
@ -14,12 +14,11 @@ export const _ = {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parts = req.params._.split("/");
|
const parts = (await req.json()) as [string, string, any];
|
||||||
switch (parts[0]) {
|
switch (parts[0]) {
|
||||||
case "set": {
|
case "set": {
|
||||||
const body = await req.json();
|
if (typeof parts[1] === "string") {
|
||||||
if (typeof parts[1] === "string" && typeof body !== "undefined") {
|
g.kv.set(parts[1], parts[2]);
|
||||||
g.kv.set(parts[1], body);
|
|
||||||
|
|
||||||
return new Response(JSON.stringify({ status: "ok" }), {
|
return new Response(JSON.stringify({ status: "ok" }), {
|
||||||
headers: { "content-type": "application/json" },
|
headers: { "content-type": "application/json" },
|
||||||
|
|
@ -34,10 +33,23 @@ export const _ = {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
case "get": {
|
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" },
|
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) {}
|
} catch (e) {}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue