-
-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathsearch.ts
More file actions
53 lines (48 loc) · 1.47 KB
/
Copy pathsearch.ts
File metadata and controls
53 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/api/v1/intent/search')({
server: {
handlers: {
GET: async ({ request }: { request: Request }) => {
const [
{
applyIntentRateLimit,
intentJsonResponse,
intentErrorResponse,
buildSkillContentUrls,
},
dbModule,
] = await Promise.all([
import('~/utils/intent-api.server'),
import('~/utils/intent-db.server'),
])
const decision = await applyIntentRateLimit(request)
if (decision.limited) return decision.response
const url = new URL(request.url)
const q = url.searchParams.get('q')?.trim() ?? ''
const limitParam = url.searchParams.get('limit')
const limit = Math.min(
Math.max(parseInt(limitParam ?? '20', 10) || 20, 1),
100,
)
if (!q) {
return intentErrorResponse(
'Missing required query parameter: q',
'INVALID_REQUEST',
400,
decision.rl,
)
}
const rows = await dbModule.searchSkills(q, limit)
const results = rows.map((row) => ({
...row,
content: buildSkillContentUrls(
row.packageName,
row.version,
row.skillPath,
),
}))
return intentJsonResponse({ query: q, limit, results }, decision.rl)
},
},
},
})