-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsearchPages.ts
More file actions
47 lines (39 loc) · 1.25 KB
/
searchPages.ts
File metadata and controls
47 lines (39 loc) · 1.25 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
import { z } from 'zod'
import { client } from '@/api/utils/api.utils'
import { logger } from '@/lib/logger'
export const searchResultSchema = z.object({
title: z.string(),
sub_title: z.string().optional().nullable(),
meta: z.object({
type: z.string(),
detail_url: z.string(),
html_url: z.string().nullable(),
slug: z.string(),
search_description: z.string().optional(),
}),
})
export const searchResultsSchema = z.object({
items: z.array(searchResultSchema),
meta: z.object({
total_count: z.number(),
}),
})
export type SearchResponse = z.infer<typeof searchResultsSchema>
export const searchPages = async ({ limit, search }: { limit: string; search: string }) => {
try {
const searchParams = new URLSearchParams()
searchParams.set('fields', 'title')
searchParams.set('limit', limit)
searchParams.set('offset', '0')
searchParams.set('search', search)
const { data } = await client<SearchResponse>(`proxy/pages/search`, { searchParams })
const result = searchResultsSchema.safeParse(data)
if (!result.success) {
logger.error('Search Zod Validation error: ', result, data)
}
return result
} catch (error) {
logger.error(error)
return searchResultsSchema.safeParse(error)
}
}