chore: not sending message at no data

This commit is contained in:
Junseo Park 2021-02-17 15:17:14 +09:00
parent c6606f0e9b
commit 17b965547e

View File

@ -1,6 +1,7 @@
import http from 'http'
import { NextApiResponse } from 'next'
import { ResponseProps } from '@types'
import { ErrorText } from './Constants'
export default function ResponseWrapper<T=unknown>(
res: NextApiResponse,
{ code=200, message, version = 2, data, errors }: ResponseProps<T>
@ -9,6 +10,6 @@ export default function ResponseWrapper<T=unknown>(
if (!http.STATUS_CODES[code]) throw new Error('Invalid http code.')
res.statusCode = code
res.setHeader('Access-Control-Allow-Origin', process.env.KOREANBOTS_URL)
res.json({ code, message: message || http.STATUS_CODES[code], data, errors, version })
res.json({ code, data, errors, version, ...(message || !data ? { message: message || ErrorText[code] || http.STATUS_CODES[code] } : {}) })
}