From cc173b66bd76566baf1056515dcb193b6760c735 Mon Sep 17 00:00:00 2001 From: ld3zian <42660576+ld3z@users.noreply.github.com> Date: Sun, 26 Jan 2025 11:55:50 -0700 Subject: [PATCH] forgo abt cors --- src/components/SlugInput.astro | 30 ++++++++++++++++++++++++++++-- src/pages/api/search.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/pages/api/search.ts diff --git a/src/components/SlugInput.astro b/src/components/SlugInput.astro index 94db311..c295713 100644 --- a/src/components/SlugInput.astro +++ b/src/components/SlugInput.astro @@ -145,11 +145,37 @@ const languages = [ } try { - const response = await fetch(`https://api.comick.io/v1.0/search/?page=1&limit=5&q=${encodeURIComponent(query)}`); + searchResults!.innerHTML = ` +
+ Searching... +
+ `; + searchResults!.classList.remove('hidden'); + + const response = await fetch('/api/search', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ query }) + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + if (!data || !Array.isArray(data) || data.length === 0) { + searchResults!.innerHTML = ` +
+ No comics found +
+ `; + return; + } + searchResults!.innerHTML = ''; - searchResults!.classList.remove('hidden'); data.forEach((comic: any) => { const div = document.createElement('div'); diff --git a/src/pages/api/search.ts b/src/pages/api/search.ts new file mode 100644 index 0000000..bccfc41 --- /dev/null +++ b/src/pages/api/search.ts @@ -0,0 +1,31 @@ +import type { APIRoute } from "astro"; + +export const POST: APIRoute = async ({ request }) => { + const { query } = await request.json(); + + try { + const response = await fetch( + `https://api.comick.io/v1.0/search/?page=1&limit=5&q=${encodeURIComponent(query)}` + ); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const data = await response.json(); + return new Response(JSON.stringify(data), { + status: 200, + headers: { + 'Content-Type': 'application/json' + } + }); + } catch (error) { + console.error('Search error:', error); + return new Response(JSON.stringify({ error: 'Failed to search comics' }), { + status: 500, + headers: { + 'Content-Type': 'application/json' + } + }); + } +}; \ No newline at end of file