mirror of
https://github.com/koreanbots/core.git
synced 2025-12-16 14:30:22 +00:00
feat: added response wrapper util
This commit is contained in:
parent
be9fc5a918
commit
dcc7a90515
21
utils/ResponseWrapper.ts
Normal file
21
utils/ResponseWrapper.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import http from 'http'
|
||||||
|
import { NextApiResponse } from 'next'
|
||||||
|
export default function ResponseWrapper(res: NextApiResponse, { code, message, version=2, data, errors }:ResponseProps) {
|
||||||
|
if(!code) throw new Error('`code` is required.')
|
||||||
|
if(!http.STATUS_CODES[code]) throw new Error('Invalid http code.')
|
||||||
|
res.statusCode = code
|
||||||
|
|
||||||
|
return { code, message: message || http.STATUS_CODES[code], data, errors, version }
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ResponseProps {
|
||||||
|
code: number
|
||||||
|
message?: string
|
||||||
|
version?: number
|
||||||
|
data?: Data
|
||||||
|
errors?: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Data {
|
||||||
|
[key: string]: unknown
|
||||||
|
}
|
||||||
3
utils/index.ts
Normal file
3
utils/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import ResponseWrapper from './ResponseWrapper'
|
||||||
|
|
||||||
|
export { ResponseWrapper }
|
||||||
Loading…
x
Reference in New Issue
Block a user