From 46347f19743ee3907752166c1d6b77300457f694 Mon Sep 17 00:00:00 2001 From: Misode Date: Wed, 2 Nov 2022 20:00:10 +0100 Subject: [PATCH] Switch to cloudflare worker for share links --- src/app/services/Sharing.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/app/services/Sharing.ts b/src/app/services/Sharing.ts index 7787e697..6a92680d 100644 --- a/src/app/services/Sharing.ts +++ b/src/app/services/Sharing.ts @@ -1,7 +1,7 @@ import lz from 'lz-string' import type { VersionId } from './Schemas.js' -const API_PREFIX = 'https://z15g7can.directus.app/items' +const API_PREFIX = 'https://snippets.misode.workers.dev' const ShareCache = new Map() @@ -13,7 +13,7 @@ export async function shareSnippet(type: string, version: VersionId, jsonData: a const body = JSON.stringify({ data, type, version, show_preview }) let id = ShareCache.get(body) if (!id) { - const snippet = await fetchApi('/snippets', body) + const snippet = await fetchApi('/', body) ShareCache.set(body, snippet.id) id = snippet.id as string } @@ -28,7 +28,7 @@ export async function shareSnippet(type: string, version: VersionId, jsonData: a export async function getSnippet(id: string) { try { - const snippet = await fetchApi(`/snippets/${id}`) + const snippet = await fetchApi(`/${id}`) return { ...snippet, data: JSON.parse(lz.decompressFromBase64(snippet.data) ?? '{}'), @@ -47,9 +47,9 @@ async function fetchApi(url: string, body?: string) { headers: { 'Content-Type': 'application/json' }, body, } : undefined) - const data = await res.json() - if (data.data) { - return data.data + if (!res.ok) { + const message = await res.text() + throw new Error(message) } - throw new Error(data.errors?.[0]?.message ?? 'Unknown error') + return await res.json() }