-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.ts
33 lines (29 loc) · 875 Bytes
/
kv.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @see {@link https://developers.cloudflare.com/pages/platform/functions/bindings/#kv-namespaces}
*/
interface Env {
KV_BINDING: KVNamespace;
}
const html = (key, value) => `
<form method="post">
<label>${key}:
<input name="${key}" value="${value || ''}">
</label>
<input type="submit" value="Set">
</form>
`;
const KEY = 'KEY';
export const onRequestGet: PagesFunction<Env> = async (context) => {
const value = await context.env.KV_BINDING.get(KEY);
return new Response(html(KEY, value), {
headers: { 'content-type': 'text/html' },
});
};
export const onRequestPost: PagesFunction<Env> = async (context) => {
const formData = await context.request.formData();
const value = formData.get(KEY);
await context.env.KV_BINDING.put(KEY, value);
return new Response(html(KEY, value), {
headers: { 'content-type': 'text/html' },
});
};