@@ -446,21 +446,35 @@ export class GraphRequest {
446446 return "" ;
447447 }
448448
449+ private parseDocumentResponse ( response , type ) : Promise < any > {
450+ return new Promise ( ( resolve , reject ) => {
451+ response . text ( ) . then ( ( xmlString ) => {
452+ try {
453+ let parser = new DOMParser ( ) ,
454+ xmlDoc = parser . parseFromString ( xmlString , type ) ;
455+ resolve ( xmlDoc ) ;
456+ } catch ( error ) {
457+ reject ( error ) ;
458+ }
459+ } ) ;
460+ } ) ;
461+ }
462+
449463 private convertResponseType ( response : Response ) : Promise < any > {
450- let responseValue : any ;
451- if ( ! this . _responseType ) {
452- this . _responseType = '' ;
464+ let self = this ,
465+ responseValue : any ;
466+ if ( ! self . _responseType ) {
467+ self . _responseType = '' ;
453468 }
454- switch ( this . _responseType . toLowerCase ( ) ) {
469+ switch ( self . _responseType . toLowerCase ( ) ) {
455470 case ResponseType . ARRAYBUFFER :
456471 responseValue = response . arrayBuffer ( ) ;
457472 break ;
458473 case ResponseType . BLOB :
459474 responseValue = response . blob ( ) ;
460475 break ;
461476 case ResponseType . DOCUMENT :
462- // XMLHTTPRequest only :(
463- responseValue = response . json ( ) ;
477+ responseValue = self . parseDocumentResponse ( response , "text/html" ) ;
464478 break ;
465479 case ResponseType . JSON :
466480 responseValue = response . json ( ) ;
@@ -472,7 +486,29 @@ export class GraphRequest {
472486 responseValue = response . text ( ) ;
473487 break ;
474488 default :
475- responseValue = response . json ( ) ;
489+ let contentType = response . headers . get ( "Content-type" ) ;
490+ if ( contentType !== null ) {
491+ let mimeType = contentType . split ( ";" ) [ 0 ] ,
492+ documentContentTypes = [ "text/html" , "text/xml" , "application/xml" , "application/xhtml+xml" ] ;
493+ if ( documentContentTypes . includes ( mimeType ) ) {
494+ responseValue = self . parseDocumentResponse ( response , mimeType ) ;
495+ } else {
496+ responseValue = response . json ( ) ;
497+ }
498+ } else {
499+ /**
500+ * RFC specification {@link https://tools.ietf.org/html/rfc7231#section-3.1.1.5} says:
501+ * A sender that generates a message containing a payload body SHOULD
502+ * generate a Content-Type header field in that message unless the
503+ * intended media type of the enclosed representation is unknown to the
504+ * sender. If a Content-Type header field is not present, the recipient
505+ * MAY either assume a media type of "application/octet-stream"
506+ * ([RFC2046], Section 4.5.1) or examine the data to determine its type.
507+ *
508+ * So assuming it as a stream type so returning the body.
509+ */
510+ responseValue = Promise . resolve ( response . body ) ;
511+ }
476512 break ;
477513 }
478514 return responseValue ;
0 commit comments