mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem The search in KB currently does not have Sentry ## Solution This PR adds Sentry to the KB search ## Review instructions - I tried to match the style in the docs while keeping everything simple, but does it look alright? - I haven't added the env variables into Vercel yet. I wanted to get this PR in front of eyes of others first. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
105 lines
3.3 KiB
TypeScript
105 lines
3.3 KiB
TypeScript
import * as Sentry from '@sentry/node'
|
|
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
|
|
|
|
import { corsHeaders } from './_cors.js'
|
|
import type { Database, KbSearchResult } from './_types.js'
|
|
|
|
// `vercel dev` sets VERCEL_ENV to 'development'. Elsewhere, Sentry stays disabled without a DSN (tests).
|
|
const IS_DEV = process.env.VERCEL_ENV === 'development'
|
|
|
|
if (!IS_DEV) {
|
|
Sentry.init({
|
|
dsn: process.env.SENTRY_DSN,
|
|
environment: process.env.VERCEL_ENV,
|
|
})
|
|
}
|
|
|
|
let client: SupabaseClient<Database> | undefined
|
|
function supabase() {
|
|
client ??= createClient<Database>(
|
|
process.env.SUPABASE_URL!,
|
|
process.env.SUPABASE_SERVICE_ROLE_KEY!,
|
|
{ auth: { autoRefreshToken: false, persistSession: false, detectSessionInUrl: false } }
|
|
)
|
|
return client
|
|
}
|
|
|
|
/** Fetches the markdown export of a page (`<href>.md`); null if it can't be loaded. */
|
|
async function fetchContent(href: string): Promise<string | null> {
|
|
try {
|
|
const response = await fetch(`${href}.md`)
|
|
if (!response.ok) {
|
|
console.warn(`Failed to fetch kb content from ${href}.md: ${response.status}`)
|
|
return null
|
|
}
|
|
return await response.text()
|
|
} catch (error) {
|
|
console.warn(`Failed to fetch kb content from ${href}.md:`, error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function OPTIONS() {
|
|
return new Response(null, { headers: corsHeaders })
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const { searchParams } = new URL(request.url)
|
|
const query = searchParams.get('q')?.trim()
|
|
const limit = Number(searchParams.get('limit')) || 10
|
|
const includeContent = searchParams.get('content') === 'true'
|
|
if (!query)
|
|
return Response.json({ error: 'Missing q parameter' }, { status: 400, headers: corsHeaders })
|
|
|
|
const { data, error } = await supabase().rpc('search_kb', {
|
|
query_text: query,
|
|
match_limit: limit,
|
|
})
|
|
if (error) {
|
|
console.error('Error running kb search:', error)
|
|
Sentry.captureException(new Error(error.message), {
|
|
tags: { route: 'kb-search' },
|
|
extra: { query, limit, error },
|
|
})
|
|
// the function can be frozen as soon as it responds, so send the event first
|
|
await Sentry.flush(2000)
|
|
return Response.json(
|
|
{ error: 'There was an error processing your request' },
|
|
{ status: 500, headers: corsHeaders }
|
|
)
|
|
}
|
|
|
|
let results: KbSearchResult[] = data.map((row) => ({
|
|
title: row.page_title,
|
|
// slugs keep the case of the content file, same as the pages and their markdown exports
|
|
href: `https://supabase.com/${row.slug}`,
|
|
heading: row.heading,
|
|
headingPath: row.heading_path,
|
|
excerpt: row.excerpt,
|
|
score: row.score,
|
|
}))
|
|
|
|
if (includeContent) {
|
|
results = await Promise.all(
|
|
results.map(async (result) => ({ ...result, content: await fetchContent(result.href) }))
|
|
)
|
|
}
|
|
|
|
return Response.json(results, {
|
|
headers: {
|
|
...corsHeaders,
|
|
'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=300',
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error('Error handling kb search request:', error)
|
|
Sentry.captureException(error, { tags: { route: 'kb-search' } })
|
|
await Sentry.flush(2000)
|
|
return Response.json(
|
|
{ error: 'There was an error processing your request' },
|
|
{ status: 500, headers: corsHeaders }
|
|
)
|
|
}
|
|
}
|