chore: search cleanup
This commit is contained in:
parent
3da23da73d
commit
d518b752ea
3 changed files with 40 additions and 24 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.5.4",
|
"version": "1.5.5",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.5.4",
|
"version": "1.5.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cdransf/api-text": "^1.5.0",
|
"@cdransf/api-text": "^1.5.0",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.5.4",
|
"version": "1.5.5",
|
||||||
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
@ -2,49 +2,65 @@ import { createClient } from '@supabase/supabase-js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
async fetch(request, env) {
|
async fetch(request, env) {
|
||||||
const allowedOrigin = 'https://coryd.dev'
|
const supabase = createClient(env.SUPABASE_URL, env.SUPABASE_KEY)
|
||||||
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 { searchParams } = new URL(request.url)
|
const { searchParams } = new URL(request.url)
|
||||||
const query = searchParams.get('q') || ''
|
const query = searchParams.get('q') || ''
|
||||||
const types = searchParams.get('type')?.split(',') || []
|
const types = searchParams.get('type')?.split(',') || []
|
||||||
const page = parseInt(searchParams.get('page')) || 1
|
const page = parseInt(searchParams.get('page') || '1', 10)
|
||||||
const pageSize = parseInt(searchParams.get('page_size')) || 10
|
const pageSize = parseInt(searchParams.get('pageSize') || '10', 10)
|
||||||
const offset = (page - 1) * pageSize
|
const offset = (page - 1) * pageSize
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let supabaseQuery = supabase
|
let supabaseQuery = supabase
|
||||||
.from('optimized_search_index')
|
.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 (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) {
|
if (error) {
|
||||||
console.error('Query error:', error)
|
console.error('Supabase query error:', error)
|
||||||
return new Response('Error fetching data from Supabase', { status: 500 })
|
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(
|
return new Response(
|
||||||
JSON.stringify({ results: data, total: count, page, pageSize }),
|
JSON.stringify({
|
||||||
|
results: data,
|
||||||
|
total: count || 0,
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
}),
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Unexpected error:', 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 })
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
}
|
}
|
Reference in a new issue