-
-
Notifications
You must be signed in to change notification settings - Fork 482
bug fix: set content-type header based on response type in mapResponse and mapEarlyResponse #1766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
71aa86a
c146d2a
3549daf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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' | ||||||||||||||||||||
|
Comment on lines
+188
to
+189
|
||||||||||||||||||||
| ) | |
| set.headers['content-type'] = 'application/json' | |
| ) { | |
| if (set.headers instanceof Headers) { | |
| set.headers.set('content-type', 'application/json') | |
| } else { | |
| ;(set.headers as any)['content-type'] = 'application/json' | |
| } | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| } | ||
|
Comment on lines
+57
to
+65
|
||
|
|
||
| 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 | ||
| } | ||
|
Comment on lines
+218
to
+226
|
||
|
|
||
| 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( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new pre-
handleSetassignment uses index-based writes (set.headers['content-type'] = ...).set.headerscan be aHeadersinstance in some call paths (for example whenhandleSetalready ran before callingmapResponse, or after recursivemapResponsecalls using the sameset), in which case this write is ignored and JSON responses won’t getContent-Type. Update to set the header viaHeaders.set(...)whenset.headers instanceof Headers(or use a helper that supports both shapes).