@@ -178,28 +178,43 @@ module.exports = (config = {}, global = window) => {
178178 // Extract request body
179179 let requestBody = ''
180180 let requestBodyLength = 0
181- if ( startContext && startContext . body ) {
182- const bodyStr = typeof startContext . body === 'string' ? startContext . body : String ( startContext . body )
183- requestBodyLength = bodyStr . length
181+ const initialBody = startContext . init ?. body || startContext . body
182+ if ( initialBody ) {
183+ const bodyStr = String ( initialBody )
184184 requestBody = truncate ( bodyStr , maxRequestSize )
185+ requestBodyLength = bodyStr . length
186+ }
187+
188+ // Extract response body
189+ let responseBody
190+ let responseBodyLength
191+ if ( endContext . xhr && endContext . xhr . responseText ) {
192+ responseBody = truncate ( endContext . xhr . responseText , maxRequestSize )
193+ responseBodyLength = endContext . xhr . responseText . length
194+ }
195+
196+ // Extract response headers
197+ let responseHeaders = { }
198+ if ( endContext . response && endContext . response . headers ) {
199+ responseHeaders = headersToObject ( endContext . response . headers )
200+ } else if ( endContext . xhr && typeof endContext . xhr . getAllResponseHeaders === 'function' ) {
201+ responseHeaders = responseHeadersToObject ( endContext . xhr . getAllResponseHeaders ( ) )
185202 }
186203
187204 // Create request and response objects for callback
188205 const requestObj = {
189206 url,
190207 httpMethod : method ,
191- headers : headersToObject ( startContext && startContext . xhr && startContext . xhr . _requestHeaders ) ,
208+ headers : headersToObject ( startContext . xhr ?. _requestHeaders || startContext . init ?. headers ) ,
192209 params : parseQueryParams ( url ) ,
193210 body : requestBody ,
194211 bodyLength : requestBodyLength
195212 }
196-
197- const responseHeaders = endContext . xhr . getAllResponseHeaders ( )
198213 const responseObj = {
199214 statusCode : endContext . status ,
200- headers : responseHeadersToObject ( responseHeaders ) ,
201- body : endContext . xhr . responseText ,
202- bodyLength : endContext . xhr . responseText ? endContext . xhr . responseText . length : 0
215+ headers : responseHeaders ,
216+ body : responseBody ,
217+ bodyLength : responseBodyLength
203218 }
204219
205220 // Call onHttpError callback if provided
0 commit comments