-
-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathpackages.ts
More file actions
41 lines (36 loc) · 1.43 KB
/
Copy pathpackages.ts
File metadata and controls
41 lines (36 loc) · 1.43 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
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/api/v1/intent/packages')({
server: {
handlers: {
GET: async ({ request }: { request: Request }) => {
const [{ applyIntentRateLimit, intentJsonResponse }, fns] =
await Promise.all([
import('~/utils/intent-api.server'),
import('~/utils/intent.functions'),
])
const decision = await applyIntentRateLimit(request)
if (decision.limited) return decision.response
const url = new URL(request.url)
const search = url.searchParams.get('q') ?? undefined
const framework = url.searchParams.get('framework') ?? undefined
const sortParam = url.searchParams.get('sort')
const sort: 'downloads' | 'name' | 'skills' | 'newest' | undefined =
sortParam === 'downloads' ||
sortParam === 'name' ||
sortParam === 'skills' ||
sortParam === 'newest'
? sortParam
: undefined
const page = parseInt(url.searchParams.get('page') ?? '0', 10) || 0
const pageSize = Math.min(
Math.max(parseInt(url.searchParams.get('pageSize') ?? '24', 10) || 24, 1),
100,
)
const result = await fns.getIntentDirectory({
data: { search, framework, sort, page, pageSize },
})
return intentJsonResponse(result, decision.rl)
},
},
},
})