Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions apps/web/app/api/docs/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'
import { docSections } from '@/lib/docroutes'
import { checkWebhookCooldown, clientIp } from '@/lib/demo-limits'

export type SearchResult = {
title: string
Expand Down Expand Up @@ -100,6 +101,15 @@ function getCorpus(): IndexedDoc[] {
const MAX_QUERY_LENGTH = 128

export async function GET(request: NextRequest) {
const ip = clientIp(request)
const cooldown = checkWebhookCooldown(ip)
if (!cooldown.ok) {
return NextResponse.json(cooldown.body, {
status: 429,
headers: { 'Retry-After': String(Math.ceil(cooldown.body.retryAfterMs / 1000)) },
})
}

const query = request.nextUrl.searchParams.get('q')?.trim().slice(0, MAX_QUERY_LENGTH) ?? ''

if (query.length < 2) {
Expand Down
5 changes: 5 additions & 0 deletions apps/web/components/docs/SearchDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export default function SearchDialog({ open, onClose }: Props) {
debounceRef.current = setTimeout(async () => {
try {
const res = await fetch(`/api/docs/search?q=${encodeURIComponent(query)}`)
if (!res.ok) {
setResults([])
setSelected(0)
return
}
const data: SearchResult[] = await res.json()
setResults(data)
setSelected(0)
Expand Down