Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 5 additions & 30 deletions netlify/edge-functions/hello.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
Expand Down
10 changes: 10 additions & 0 deletions routes/getSnapshotList.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
23 changes: 23 additions & 0 deletions routes/postUploadSnapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getStore } from '@netlify/blobs';

interface SnapshotTemp {
id: string;
assets: Record<string, any>;
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 });
}