@@ -24,6 +24,16 @@ function isJSONSerializable(value: any) {
2424 ) ;
2525}
2626
27+ export type JSONResponse = {
28+ body : Record < string , any > ;
29+ routerResponse : ResponseInit | undefined ;
30+ _flag : "json" ;
31+ } ;
32+
33+ function isJSONResponse ( value : any ) : value is JSONResponse {
34+ return "_flag" in value && value . _flag === "json" ;
35+ }
36+
2737export function toResponse ( data ?: any , init ?: ResponseInit ) : Response {
2838 if ( data instanceof Response ) {
2939 if ( init ?. headers instanceof Headers ) {
@@ -33,19 +43,22 @@ export function toResponse(data?: any, init?: ResponseInit): Response {
3343 }
3444 return data ;
3545 }
36- if ( data ?. _flag === "json" ) {
46+ if ( isJSONResponse ( data ) ) {
47+ const body = data . body ;
3748 const routerResponse = data . routerResponse ;
3849 if ( routerResponse instanceof Response ) {
3950 return routerResponse ;
4051 }
41- return toResponse ( data . body , {
42- headers : data . headers ,
43- status : data . status ,
52+ return toResponse ( body , {
53+ ...routerResponse ,
54+ headers : init ?. headers ?? routerResponse ?. headers ,
55+ status : init ?. status ?? routerResponse ?. status ,
56+ statusText : init ?. statusText ?? routerResponse ?. statusText ,
4457 } ) ;
4558 }
4659 if ( isAPIError ( data ) ) {
4760 return toResponse ( data . body , {
48- status : data . statusCode ,
61+ status : init ?. status ?? data . statusCode ,
4962 statusText : data . status . toString ( ) ,
5063 headers : init ?. headers || data . headers ,
5164 } ) ;
0 commit comments