@@ -117,30 +117,45 @@ export async function fetch(
117117
118118 const headers = init ?. headers
119119 ? init . headers instanceof Headers
120- ? Array . from ( init . headers . entries ( ) )
121- : Array . isArray ( init . headers )
122- ? init . headers
123- : Object . entries ( init . headers )
124- : [ ] ;
125-
126- const mappedHeaders : Array < [ string , string ] > = headers . map ( ( [ name , val ] ) => [
127- name ,
128- // we need to ensure we have all values as strings
129- // eslint-disable-next-line
130- typeof val === "string" ? val : ( val as any ) . toString ( ) ,
131- ] ) ;
120+ ? init . headers
121+ : new Headers ( init . headers )
122+ : new Headers ( ) ;
132123
133124 const req = new Request ( input , init ) ;
134125 const buffer = await req . arrayBuffer ( ) ;
135- const reqData =
126+ const data =
136127 buffer . byteLength !== 0 ? Array . from ( new Uint8Array ( buffer ) ) : null ;
137128
129+ // append new headers created by the browser `Request` implementation,
130+ // if not already declared by the caller of this function
131+ for ( const [ key , value ] of req . headers ) {
132+ if ( ! headers . get ( key ) ) {
133+ headers . set ( key , value ) ;
134+ }
135+ }
136+
137+ const headersArray =
138+ headers instanceof Headers
139+ ? Array . from ( headers . entries ( ) )
140+ : Array . isArray ( headers )
141+ ? headers
142+ : Object . entries ( headers ) ;
143+
144+ const mappedHeaders : Array < [ string , string ] > = headersArray . map (
145+ ( [ name , val ] ) => [
146+ name ,
147+ // we need to ensure we have all header values as strings
148+ // eslint-disable-next-line
149+ typeof val === "string" ? val : ( val as any ) . toString ( ) ,
150+ ] ,
151+ ) ;
152+
138153 const rid = await invoke < number > ( "plugin:http|fetch" , {
139154 clientConfig : {
140155 method : req . method ,
141156 url : req . url ,
142157 headers : mappedHeaders ,
143- data : reqData ,
158+ data,
144159 maxRedirections,
145160 connectTimeout,
146161 proxy,
0 commit comments