diff --git a/netlify/edge-functions/hello.ts b/netlify/edge-functions/hello.ts index 5cb3c8b..f222e82 100644 --- a/netlify/edge-functions/hello.ts +++ b/netlify/edge-functions/hello.ts @@ -1,41 +1,16 @@ import type { Context, Config } from '@netlify/edge-functions'; import { getStore, type Store } from '@netlify/blobs'; +import { postUploadSnapshot } from '../../routes/postUploadSnapshot.ts'; +import { getSnapshotList } from '../../routes/getSnapshotList.ts'; + export default async (request: Request, context: Context) => { try { - const store: Store = await getStore('my-store'); - - const url = new URL(request.url); - const key = url.searchParams.get('key'); - switch (request.method) { case 'POST': - if (!key) { - return new Response('No key provided', { status: 400 }); - } - - const body = await request.json(); - await store.setJSON(key, body); - return new Response('Blob successfully stored', { status: 200 }); + return postUploadSnapshot(request); case 'GET': - if (!key) { - const list = await store.list(); - - return new Response(JSON.stringify(list), { - status: 200, - headers: { - 'Content-Type': 'application/json', - }, - }); - } - - const value = await store.get(key); - return new Response(value, { - status: 200, - headers: { - 'Content-Type': 'application/json', - }, - }); + return getSnapshotList(); default: return new Response('Method not allowed', { status: 405 }); } diff --git a/routes/getSnapshotList.ts b/routes/getSnapshotList.ts new file mode 100644 index 0000000..99a0ddd --- /dev/null +++ b/routes/getSnapshotList.ts @@ -0,0 +1,10 @@ +import { getStore } from '@netlify/blobs'; + +export async function getSnapshotList() { + const zeSnapshotStore = getStore('ze_snapshots'); + const list = await zeSnapshotStore.list(); + const response = { + keys: list?.blobs?.map((blob) => ({ name: blob })), + }; + return new Response(JSON.stringify(response), { status: 200 }); +} diff --git a/routes/postUploadSnapshot.ts b/routes/postUploadSnapshot.ts new file mode 100644 index 0000000..bd09166 --- /dev/null +++ b/routes/postUploadSnapshot.ts @@ -0,0 +1,23 @@ +import { getStore } from '@netlify/blobs'; + +interface SnapshotTemp { + id: string; + assets: Record; + message: string; +} + +export async function postUploadSnapshot(request: Request) { + const newSnapshot = (await request.json()) as SnapshotTemp; + + console.log('snapshot', newSnapshot); + + const zeSnapshotsStore = getStore('ze_snapshots'); + + console.log('before setJSON'); + + await zeSnapshotsStore.setJSON(newSnapshot.id, newSnapshot); + + console.log('after setJSON'); + + return new Response('Blob successfully stored', { status: 200 }); +}