diff options
Diffstat (limited to 'src/pages/api')
| -rw-r--r-- | src/pages/api/blob.ts | 20 | ||||
| -rw-r--r-- | src/pages/api/blobs.ts | 27 | ||||
| -rw-r--r-- | src/pages/api/revalidate.ts | 19 |
3 files changed, 66 insertions, 0 deletions
diff --git a/src/pages/api/blob.ts b/src/pages/api/blob.ts new file mode 100644 index 0000000..4ff37c4 --- /dev/null +++ b/src/pages/api/blob.ts @@ -0,0 +1,20 @@ +import type { APIRoute } from 'astro'; +import { getStore } from '@netlify/blobs'; + +export const prerender = false; + +export const GET: APIRoute = async (context) => { + const urlParams = new URL(context.url); + const key = urlParams.searchParams.get('key'); + if (!key) { + return new Response('Bad Request', { status: 400 }); + } + + const blobStore = getStore('shapes'); + const blob = await blobStore.get(key, { type: 'json' }); + return new Response( + JSON.stringify({ + blob + }) + ); +}; diff --git a/src/pages/api/blobs.ts b/src/pages/api/blobs.ts new file mode 100644 index 0000000..d11aa58 --- /dev/null +++ b/src/pages/api/blobs.ts @@ -0,0 +1,27 @@ +import type { APIRoute } from 'astro'; +import { getStore } from '@netlify/blobs'; + +export const prerender = false; + +export const POST: APIRoute = async ({ request }) => { + const parameters = await request.json(); + const blobStore = getStore('shapes'); + const key = parameters.name; + await blobStore.setJSON(key, parameters); + return new Response( + JSON.stringify({ + message: `Stored shape "${key}"` + }) + ); +}; + +export const GET: APIRoute = async ({ request }) => { + const blobStore = getStore({ name: 'shapes', consistency: 'strong' }); + const data = await blobStore.list(); + const keys = data.blobs.map(({ key }) => key); + return new Response( + JSON.stringify({ + keys + }) + ); +}; diff --git a/src/pages/api/revalidate.ts b/src/pages/api/revalidate.ts new file mode 100644 index 0000000..0d953a3 --- /dev/null +++ b/src/pages/api/revalidate.ts @@ -0,0 +1,19 @@ +import type { APIRoute } from 'astro'; +import { purgeCache } from '@netlify/functions'; + +export const prerender = false; + +export const POST: APIRoute = async ({ request }) => { + const { tags } = await request.json(); + + if (!Array.isArray(tags)) { + return new Response(`Bad Request: expected tags attribute with array of strings in the body, got ${typeof tags}`, { status: 400 }); + } + + await purgeCache({ tags }); + return new Response( + JSON.stringify({ + invalidated: tags + }) + ); +}; |
