Commit 6b50b132 authored by Joe Haddad's avatar Joe Haddad Committed by JJ Kasper

Allow Boolean API Response (#9842)

parent f89d55f1
...@@ -204,7 +204,11 @@ export function sendData(res: NextApiResponse, body: any) { ...@@ -204,7 +204,11 @@ export function sendData(res: NextApiResponse, body: any) {
let str = body let str = body
// Stringify JSON body // Stringify JSON body
if (typeof body === 'object' || typeof body === 'number') { if (
typeof body === 'object' ||
typeof body === 'number' ||
typeof body === 'boolean'
) {
str = JSON.stringify(body) str = JSON.stringify(body)
res.setHeader('Content-Type', 'application/json; charset=utf-8') res.setHeader('Content-Type', 'application/json; charset=utf-8')
} }
......
export default (req, res) => {
res.json(true)
}
...@@ -86,6 +86,16 @@ function runTests(dev = false) { ...@@ -86,6 +86,16 @@ function runTests(dev = false) {
expect(data).toEqual({}) expect(data).toEqual({})
}) })
it('should support boolean for JSON in api page', async () => {
const res = await fetchViaHTTP(appPort, '/api/bool', null, {})
const body = res.ok ? await res.json() : null
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe(
'application/json; charset=utf-8'
)
expect(body).toBe(true)
})
it('should return error with invalid JSON', async () => { it('should return error with invalid JSON', async () => {
const data = await fetchViaHTTP(appPort, '/api/parse', null, { const data = await fetchViaHTTP(appPort, '/api/parse', null, {
method: 'POST', method: 'POST',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment