From d518b752eafa8d22dca1bc7689643ee6e21decef Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Sat, 19 Oct 2024 15:31:23 -0700 Subject: [PATCH] chore: search cleanup --- package-lock.json | 4 +-- package.json | 2 +- workers/search/index.js | 58 ++++++++++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index dc0c652d..7462149c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "1.5.4", + "version": "1.5.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "1.5.4", + "version": "1.5.5", "license": "MIT", "dependencies": { "@cdransf/api-text": "^1.5.0", diff --git a/package.json b/package.json index f4dece98..229db553 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "1.5.4", + "version": "1.5.5", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "engines": { diff --git a/workers/search/index.js b/workers/search/index.js index b63baecd..20909175 100644 --- a/workers/search/index.js +++ b/workers/search/index.js @@ -2,49 +2,65 @@ import { createClient } from '@supabase/supabase-js' export default { async fetch(request, env) { - const allowedOrigin = 'https://coryd.dev' - const origin = request.headers.get('Origin') || '' - const referer = request.headers.get('Referer') || '' - - if (!origin.startsWith(allowedOrigin) && !referer.startsWith(allowedOrigin)) return new Response('Forbidden', { status: 403 }) - - const supabaseUrl = env.SUPABASE_URL || process.env.SUPABASE_URL - const supabaseKey = env.SUPABASE_KEY || process.env.SUPABASE_KEY - const supabase = createClient(supabaseUrl, supabaseKey) + const supabase = createClient(env.SUPABASE_URL, env.SUPABASE_KEY) const { searchParams } = new URL(request.url) const query = searchParams.get('q') || '' const types = searchParams.get('type')?.split(',') || [] - const page = parseInt(searchParams.get('page')) || 1 - const pageSize = parseInt(searchParams.get('page_size')) || 10 + const page = parseInt(searchParams.get('page') || '1', 10) + const pageSize = parseInt(searchParams.get('pageSize') || '10', 10) const offset = (page - 1) * pageSize try { let supabaseQuery = supabase .from('optimized_search_index') - .select('*', { count: 'exact' }) + .select( + 'id, title, description, url, tags, type, total_plays, genre_name, genre_url', + { count: 'exact' } + ) + .range(offset, offset + pageSize - 1) if (types.length > 0) supabaseQuery = supabaseQuery.in('type', types) - if (query) supabaseQuery = supabaseQuery.or(`title.ilike.%${query}%,description.ilike.%${query}%`) - const { data, error, count } = await supabaseQuery.range(offset, offset + pageSize - 1) + if (query) { + const queryLower = `%${query.toLowerCase()}%` + supabaseQuery = supabaseQuery.or( + `title.ilike.${queryLower},description.ilike.${queryLower}` + ) + } + + const { data, error, count } = await supabaseQuery if (error) { - console.error('Query error:', error) - return new Response('Error fetching data from Supabase', { status: 500 }) + console.error('Supabase query error:', error) + return new Response(JSON.stringify({ error: 'Error fetching data' }), { status: 500 }) + } + + if (!data || data.length === 0) { + console.warn('No results found.') + return new Response( + JSON.stringify({ results: [], total: 0, page, pageSize }), + { headers: { 'Content-Type': 'application/json' } } + ) } return new Response( - JSON.stringify({ results: data, total: count, page, pageSize }), + JSON.stringify({ + results: data, + total: count || 0, + page, + pageSize, + }), { headers: { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', }, } ) } catch (error) { console.error('Unexpected error:', error) - return new Response('Internal Server Error', { status: 500 }) + return new Response(JSON.stringify({ error: 'Internal Server Error' }), { status: 500 }) } - } -} \ No newline at end of file + }, +}