mirror of
https://github.com/koreanbots/core.git
synced 2025-12-16 06:20:24 +00:00
feat: added bot search endpoint
This commit is contained in:
parent
4b062fac93
commit
e732a1d240
30
pages/api/v2/search/bots.ts
Normal file
30
pages/api/v2/search/bots.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { BotList } from '@types'
|
||||
import { get } from '@utils/Query'
|
||||
import ResponseWrapper from '@utils/ResponseWrapper'
|
||||
import { SearchQuerySchema } from '@utils/Yup'
|
||||
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
const SearchBots: NextApiHandler = async (req: ApiRequest, res: NextApiResponse) => {
|
||||
const validated = await SearchQuerySchema.validate({ q: req.query.q, page: req.query.page }).then(el => el).catch(e => {
|
||||
ResponseWrapper(res, { code: 400, errors: e.errors })
|
||||
})
|
||||
if(!validated) return
|
||||
|
||||
let result: BotList
|
||||
try {
|
||||
result = await get.list.search.load(JSON.stringify({ page: validated.page, query: validated.q }))
|
||||
} catch {
|
||||
return ResponseWrapper(res, { code: 400, message: '검색 문법이 잘못되었습니다.' })
|
||||
}
|
||||
if(result.totalPage < validated.page || result.currentPage !== validated.page) return ResponseWrapper(res, { code: 404, message: '검색 결과가 없습니다.' })
|
||||
else ResponseWrapper<BotList>(res, { code: 200, data: result })
|
||||
}
|
||||
|
||||
interface ApiRequest extends NextApiRequest {
|
||||
query: {
|
||||
q: string
|
||||
page: string
|
||||
}
|
||||
}
|
||||
|
||||
export default SearchBots
|
||||
Loading…
x
Reference in New Issue
Block a user