@@ -24,7 +24,7 @@ import { FirebaseApp, initializeApp } from '@firebase/app';
2424
2525import { Firestore , initializeFirestore , setLogLevel } from '../../../src' ;
2626import { PrivateSettings } from '../../../src/lite-api/settings' ;
27- import { logDebug } from '../../../src/util/log' ;
27+ import { logDebug , logError } from '../../../src/util/log' ;
2828import { generateUniqueDebugId } from '../../../src/util/debug_uid' ;
2929
3030// TODO(dimond): Right now we create a new app and Firestore instance for
@@ -92,12 +92,12 @@ class ReadableStreamSpy<Uint8Array> {
9292 }
9393}
9494
95- class RequestInfo { }
96-
9795globalThis . fetch = async function ( requestOrUrl , options ) {
9896 let url = '' ;
9997 let verb = 'GET' ;
10098 let request = requestOrUrl ;
99+ let bodyPromise : Promise < string > = Promise . resolve ( 'none' ) ;
100+ let contentType : string = '' ;
101101 const id = generateUniqueDebugId ( ) ;
102102
103103 if ( typeof requestOrUrl === 'string' ) {
@@ -107,31 +107,9 @@ globalThis.fetch = async function (requestOrUrl, options) {
107107 } else {
108108 url = requestOrUrl . url ;
109109 verb = requestOrUrl . method ;
110- if (
111- url . startsWith (
112- 'https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel'
113- )
114- ) {
115- try {
116- if ( requestOrUrl . body instanceof ReadableStream ) {
117- const requestSpy = new ReadableStreamSpy (
118- 'request' ,
119- requestOrUrl . body ,
120- id
121- ) ;
122- const requestInit : RequestInit = {
123- body : requestSpy . readableStream ,
124- // @ts -ignore
125- duplex : 'half'
126- } ;
127- request = new Request ( requestOrUrl , requestInit ) ;
128- } else {
129- logDebug ( JSON . stringify ( requestOrUrl . body ) ) ;
130- }
131- } catch ( e ) {
132- console . log ( e ) ;
133- }
134- }
110+ bodyPromise = requestOrUrl . clone ( ) . text ( ) ;
111+ contentType = requestOrUrl . headers . get ( 'Content-Type' ) ?? 'empty' ;
112+ request = requestOrUrl ;
135113 }
136114
137115 if (
@@ -143,6 +121,20 @@ globalThis.fetch = async function (requestOrUrl, options) {
143121
144122 const response = await originalFetch ( request , options ) ;
145123
124+ bodyPromise . then ( bodyText => {
125+ if ( contentType === 'application/x-www-form-urlencoded' ) {
126+ const decodedData : Record < string , string > = { } ;
127+
128+ bodyText . split ( '&' ) . forEach ( pair => {
129+ const [ key , value ] = pair . split ( '=' ) ;
130+ decodedData [ key ] = decodeURIComponent ( value ) ;
131+ } ) ;
132+ logDebug ( `(fetch: ${ id } ) BODY: ${ JSON . stringify ( decodedData ) } ` ) ;
133+ } else {
134+ logDebug ( `(fetch: ${ id } ) BODY: ${ bodyText } ` ) ;
135+ }
136+ } ) ;
137+
146138 if ( response . body ) {
147139 const spy = new ReadableStreamSpy ( 'response' , response . body , id ) ;
148140
0 commit comments