1313 * License for the specific language governing permissions and limitations
1414 * under the License.
1515 */
16+ import JSONbig from 'json-bigint' ;
17+ import { jsonPrettify } from '../json-util' ;
1618import { docServiceDebug , providers } from '../header-provider' ;
1719import { Endpoint , Method } from '../specification' ;
1820import { ResponseData } from '../types' ;
1921
20- export default abstract class Transport {
22+ export abstract class Transport {
2123 public abstract supportsMimeType ( mimeType : string ) : boolean ;
2224
2325 public abstract getDebugMimeType ( ) : string ;
2426
27+ private toHeaderLines ( headers : Headers ) : string [ ] {
28+ const headerLines : string [ ] = [ ] ;
29+ headers . forEach ( ( value , name ) => {
30+ headerLines . push ( `${ name } : ${ value } ` ) ;
31+ } ) ;
32+ return headerLines ;
33+ }
34+
2535 public async send (
2636 method : Method ,
2737 headers : { [ name : string ] : string } ,
@@ -59,13 +69,28 @@ export default abstract class Transport {
5969 endpointPath ,
6070 queries ,
6171 ) ;
62- const responseHeaders = httpResponse . headers ;
63- const responseText = httpResponse . body ;
72+
73+ const responseHeaders = this . toHeaderLines ( httpResponse . headers ) ;
74+ const responseText = await httpResponse . text ( ) ;
75+ const applicationType = httpResponse . headers . get ( 'content-type' ) || '' ;
76+ let responseBody : string = responseText ;
77+ if ( applicationType . indexOf ( 'json' ) >= 0 ) {
78+ try {
79+ const json = JSONbig . parse ( responseText ) ;
80+ const prettified = jsonPrettify ( JSONbig . stringify ( json ) ) ;
81+ if ( prettified . length > 0 ) {
82+ responseBody = prettified ;
83+ }
84+ } catch ( ignored ) {
85+ /* empty */
86+ }
87+ }
6488 const duration = Math . round ( performance . now ( ) - start ) ;
6589 const timestamp = new Date ( ) . toLocaleString ( ) ;
90+
6691 if ( responseText . length > 0 ) {
6792 return {
68- body : responseText ,
93+ body : responseBody ,
6994 headers : responseHeaders ,
7095 status : httpResponse . status ,
7196 executionTime : duration ,
@@ -78,7 +103,7 @@ export default abstract class Transport {
78103 headers : responseHeaders ,
79104 status : httpResponse . status ,
80105 executionTime : duration ,
81- size : responseText . length ,
106+ size : 0 ,
82107 timestamp,
83108 } ;
84109 }
@@ -159,10 +184,10 @@ export default abstract class Transport {
159184
160185 protected abstract doSend (
161186 method : Method ,
162- headers : { [ name : string ] : string } ,
187+ headers : { [ p : string ] : string } ,
163188 pathPrefix : string ,
164189 bodyJson ?: string ,
165190 endpointPath ?: string ,
166191 queries ?: string ,
167- ) : Promise < ResponseData > ;
192+ ) : Promise < Response > ;
168193}
0 commit comments