mirror of
https://github.com/koreanbots/core.git
synced 2025-12-16 22:30:23 +00:00
15 lines
484 B
TypeScript
15 lines
484 B
TypeScript
import http from 'http'
|
|
import { NextApiResponse } from 'next'
|
|
import { ResponseProps } from '@types'
|
|
export default function ResponseWrapper<T=unknown>(
|
|
res: NextApiResponse,
|
|
{ code=200, message, version = 2, data, errors }: ResponseProps<T>
|
|
) {
|
|
if (!code) throw new Error('`code` is required.')
|
|
if (!http.STATUS_CODES[code]) throw new Error('Invalid http code.')
|
|
res.statusCode = code
|
|
|
|
res.json({ code, message: message || http.STATUS_CODES[code], data, errors, version })
|
|
}
|
|
|