diff --git a/src/adapter/bun/handler.ts b/src/adapter/bun/handler.ts old mode 100644 new mode 100755 index b7c1a7e4..fa2e111e --- a/src/adapter/bun/handler.ts +++ b/src/adapter/bun/handler.ts @@ -25,6 +25,12 @@ export const mapResponse = ( request?: Request ): Response => { if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { + if ( + response?.constructor?.name === 'Object' || + response?.constructor?.name === 'Array' + ) + set.headers['content-type'] = 'application/json' + handleSet(set) switch (response?.constructor?.name) { @@ -33,7 +39,6 @@ export const mapResponse = ( case 'Array': case 'Object': - set.headers['content-type'] = 'application/json' return new Response(JSON.stringify(response), set as any) case 'ElysiaFile': @@ -141,7 +146,10 @@ export const mapResponse = ( const code = (response as any).charCodeAt(0) if (code === 123 || code === 91) { - if (!set.headers['Content-Type']) + if (set.headers instanceof Headers) { + if (!set.headers.has('Content-Type')) + set.headers.set('Content-Type', 'application/json') + } else if (!set.headers['Content-Type']) set.headers['Content-Type'] = 'application/json' return new Response( @@ -174,6 +182,12 @@ export const mapEarlyResponse = ( if (response === undefined || response === null) return if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { + if ( + response?.constructor?.name === 'Object' || + response?.constructor?.name === 'Array' + ) + set.headers['content-type'] = 'application/json' + handleSet(set) switch (response?.constructor?.name) { @@ -182,7 +196,6 @@ export const mapEarlyResponse = ( case 'Array': case 'Object': - set.headers['content-type'] = 'application/json' return new Response(JSON.stringify(response), set as any) case 'ElysiaFile': @@ -289,7 +302,10 @@ export const mapEarlyResponse = ( const code = (response as any).charCodeAt(0) if (code === 123 || code === 91) { - if (!set.headers['Content-Type']) + if (set.headers instanceof Headers) { + if (!set.headers.has('Content-Type')) + set.headers.set('Content-Type', 'application/json') + } else if (!set.headers['Content-Type']) set.headers['Content-Type'] = 'application/json' return new Response( diff --git a/src/adapter/web-standard/handler.ts b/src/adapter/web-standard/handler.ts old mode 100644 new mode 100755 index a679eb34..ce97ac68 --- a/src/adapter/web-standard/handler.ts +++ b/src/adapter/web-standard/handler.ts @@ -54,16 +54,24 @@ export const mapResponse = ( request?: Request ): Response => { if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { + switch (response?.constructor?.name) { + case 'String': + set.headers['content-type'] = 'text/plain' + break + case 'Array': + case 'Object': + set.headers['content-type'] = 'application/json' + break + } + handleSet(set) switch (response?.constructor?.name) { case 'String': - set.headers['content-type'] = 'text/plain' return new Response(response as string, set as any) case 'Array': case 'Object': - set.headers['content-type'] = 'application/json' return new Response(JSON.stringify(response), set as any) case 'ElysiaFile': @@ -171,7 +179,10 @@ export const mapResponse = ( const code = (response as any).charCodeAt(0) if (code === 123 || code === 91) { - if (!set.headers['Content-Type']) + if (set.headers instanceof Headers) { + if (!set.headers.has('Content-Type')) + set.headers.set('Content-Type', 'application/json') + } else if (!set.headers['Content-Type']) set.headers['Content-Type'] = 'application/json' return new Response( @@ -204,16 +215,24 @@ export const mapEarlyResponse = ( if (response === undefined || response === null) return if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { + switch (response?.constructor?.name) { + case 'String': + set.headers['content-type'] = 'text/plain' + break + case 'Array': + case 'Object': + set.headers['content-type'] = 'application/json' + break + } + handleSet(set) switch (response?.constructor?.name) { case 'String': - set.headers['content-type'] = 'text/plain' return new Response(response as string, set as any) case 'Array': case 'Object': - set.headers['content-type'] = 'application/json' return new Response(JSON.stringify(response), set as any) case 'ElysiaFile': @@ -320,7 +339,10 @@ export const mapEarlyResponse = ( const code = (response as any).charCodeAt(0) if (code === 123 || code === 91) { - if (!set.headers['Content-Type']) + if (set.headers instanceof Headers) { + if (!set.headers.has('Content-Type')) + set.headers.set('Content-Type', 'application/json') + } else if (!set.headers['Content-Type']) set.headers['Content-Type'] = 'application/json' return new Response(