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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This val creates a RESTful API for storing JSON blobs using Val Town's blob storage.
// It supports GET, POST, PUT, and DELETE operations on /items and /items/:id endpoints.
// CORS is enabled to allow requests from any origin.
// The root endpoint (/) returns instructions on how to use the API via curl.
import { blob } from "https://esm.town/v/std/blob";
const BLOB_PREFIX = "items_";
export default async function server(request: Request): Promise<Response> {
const url = new URL(request.url);
const path = url.pathname;
const method = request.method;
// CORS headers
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
};
// Handle CORS preflight requests
if (method === "OPTIONS") {
return new Response(null, { headers });
}
if (path === "/") {
return new Response(getInstructions(), {
headers: { ...headers, "Content-Type": "text/plain" },
});
}
if (path === "/items") {
if (method === "GET") {
const items = await getAllItems();
return jsonResponse(items, headers);
} else if (method === "POST") {
const item = await request.json();
const id = await createItem(item);
return jsonResponse({ id }, headers, 201);
}
} else if (path.startsWith("/items/")) {
const id = path.split("/")[2];
if (method === "GET") {
const item = await getItem(id);
return item ? jsonResponse(item, headers) : notFoundResponse(headers);
} else if (method === "PUT") {
const item = await request.json();
await updateItem(id, item);
return new Response(null, { headers, status: 204 });
} else if (method === "DELETE") {
await deleteItem(id);
return new Response(null, { headers, status: 204 });
}
}
return new Response("Not Found", { headers, status: 404 });
}
async function getAllItems() {
const keys = await blob.list(BLOB_PREFIX);
const items = await Promise.all(
keys.map(async (key) => ({
id: key.key.slice(BLOB_PREFIX.length),
...await blob.getJSON(key.key),
}))
);
return items;
}
async function getItem(id: string) {
return await blob.getJSON(`${BLOB_PREFIX}${id}`);
}
async function createItem(item: any) {
const id = crypto.randomUUID();
await blob.setJSON(`${BLOB_PREFIX}${id}`, item);
return id;
}
async function updateItem(id: string, item: any) {
await blob.setJSON(`${BLOB_PREFIX}${id}`, item);
}
async function deleteItem(id: string) {
await blob.delete(`${BLOB_PREFIX}${id}`);
}
function jsonResponse(data: any, headers: HeadersInit, status = 200) {
return new Response(JSON.stringify(data), {
headers: { ...headers, "Content-Type": "application/json" },
status,
});
}
function notFoundResponse(headers: HeadersInit) {
return new Response("Not Found", { headers, status: 404 });
}
function getInstructions() {