import { blob } from "https://esm.town/v/std/blob";
const KEY = "Ben Wheeler globalIncrement test key";
export function getValue(): Promise<number> {
return blob.getJSON(KEY)
.then(value => typeof value === "number" ? value : 0)
.catch(() => 0);
}
export function increment(): Promise<number> {
return getValue()
.then(currentValue => {
const newValue = currentValue + 1;
return blob.setJSON(KEY, newValue)
.then(() => {
console.log(`Previous value: ${currentValue} ; Updated value: ${newValue}`);
return newValue;
});
});
}
export default async function(req: Request): Promise<Response> {
if (req.method === "POST") {
return increment()
.then(newValue => Response.json({ value: newValue }));
} else {
return getValue()
.then(currentValue => Response.json({ value: currentValue }));
}
}
export function checkIfWorking(req: Request): Promise<Response> {
console.log("working");
}