chore: search cleanup

This commit is contained in:
Cory Dransfeldt 2024-10-19 15:31:23 -07:00
parent 3da23da73d
commit d518b752ea
No known key found for this signature in database
3 changed files with 40 additions and 24 deletions

4
package-lock.json generated
View file

@ -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",

View file

@ -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": {

View file

@ -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 })
}
}
}
},
}