import { Base } from "gd-sprest-def" ; import { ContextInfo } from "../lib" ; import { TargetInfo } from "." ; declare var ActiveXObject ; /**
 * XML HTTP Request Class
 */ export class XHRRequest { /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ constructor ( asyncFl , targetInfo , callback ? , executeFl = true ) { this . asyncFl = asyncFl ; this . executeFl = executeFl ; this . headers = { } ; this . onRequestCompleted = callback || targetInfo . props . callback ; this . targetInfo = targetInfo ; this . xhr = this . createXHR ( ) ; if ( this . xhr ) { this . execute ( ) ; } else { this . defaultHeaders ( ) ; } } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ get completedFl ( ) : boolean { return this . xhr ? this . xhr . readyState == 4 : false ; } get response ( ) { return this . xhr ? this . xhr . response : null ; } get request ( ) { return this . xhr ? this . xhr : null ; } get requestData ( ) { return this . targetInfo . requestData ; } get requestHeaders ( ) { return this . headers ; } get requestInfo ( ) : Base . IRequestInfo { return { data : this . targetInfo . requestData , headers : this . headers , method : this . targetInfo . requestMethod , url : this . targetInfo . requestUrl } ; } get requestUrl ( ) : string { return this . xhr ? this . xhr . responseURL : null ; } get status ( ) : number { return this . xhr ? this . xhr . status : null ; } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ private asyncFl : boolean ; private executeFl : boolean ; private headers : { [ key ] : string ; } ; private targetInfo : TargetInfo ; private onRequestCompleted : ( xhr : XHRRequest ) => any ; private xhr : XMLHttpRequest ; /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ private createXHR ( ) { if ( typeof ( XMLHttpRequest ) !== "undefined" ) { return new XMLHttpRequest ( ) ; } try { return new ActiveXObject ( "Msxml2.XMLHTTP.6.0" ) ; } catch ( e ) { } try { return new ActiveXObject ( "Msxml2.XMLHTTP.3.0" ) ; } catch ( e ) { } try { return new ActiveXObject ( "Microsoft.XMLHTTP" ) ; } catch ( e ) { } console . error ( "This browser does not support xml http requests." ) ; } private defaultHeaders ( requestDigest ? ) { let ifMatchExists = false ; if ( this . targetInfo . requestHeaders ) { for ( var header in this . targetInfo . requestHeaders ) { this . xhr ? this . xhr . setRequestHeader ( header , this . targetInfo . requestHeaders [ header ] ) : null ; this . headers [ header ] = this . targetInfo . requestHeaders [ header ] ; ifMatchExists = ifMatchExists || header . toUpperCase ( ) == "IF-MATCH" ; } } else { if ( this . targetInfo . isGraph ) { this . xhr ? this . xhr . setRequestHeader ( "Accept" , "application/json" ) : null ; this . xhr ? this . xhr . setRequestHeader ( "Content-Type" , "application/json" ) : null ; this . headers [ "Accept" ] = "application/json" ; this . headers [ "Content-Type" ] = "application/json" ; } else { this . xhr ? this . xhr . setRequestHeader ( "Accept" , "application/json;odata=verbose" ) : null ; this . xhr ? this . xhr . setRequestHeader ( "Content-Type" , "application/json;odata=verbose" ) : null ; this . headers [ "Accept" ] = "application/json;odata=verbose" ; this . headers [ "Content-Type" ] = "application/json;odata=verbose" ; } } if ( this . targetInfo . props . disableCache ) { this . xhr ? this . xhr . setRequestHeader ( "Cache-Control" , "no-cache" ) : null ; this . headers [ "Cache-Control" ] = "no-cache" ; } if ( this . targetInfo . isGraph ) { this . xhr ? this . xhr . setRequestHeader ( "Authorization" , "Bearer " + this . targetInfo . props . accessToken ) : null ; this . headers [ "Authorization" ] = "Bearer " + this . targetInfo . props . accessToken ; } else { if ( this . targetInfo . requestHeaders == null ) { this . xhr ? this . xhr . setRequestHeader ( "X-HTTP-Method" , this . targetInfo . requestMethod ) : null ; this . headers [ "X-HTTP-Method" ] = this . targetInfo . requestMethod ; } this . xhr ? this . xhr . setRequestHeader ( "X-RequestDigest" , requestDigest ) : null ; requestDigest ? this . headers [ "X-RequestDigest" ] = requestDigest : null ; if ( this . targetInfo . requestMethod == "DELETE" || this . targetInfo . requestMethod == "MERGE" && ! ifMatchExists ) { this . xhr ? this . xhr . setRequestHeader ( "IF-MATCH" , "*" ) : null ; this . headers [ "IF-MATCH" ] = "*" ; } } } private execute ( ) { let requestDigest = this . targetInfo . props . requestDigest || "" ; if ( requestDigest == "" ) { requestDigest = ContextInfo . document ? ContextInfo . document . querySelector ( "#__REQUESTDIGEST" ) : "" ; requestDigest = requestDigest ? requestDigest . value : ContextInfo . formDigestValue ; } if ( this . targetInfo . props . endpoint == "contextinfo" ) { this . executeRequest ( requestDigest ) ; } else if ( this . targetInfo . requestMethod != "GET" && requestDigest == "" ) { if ( ! this . asyncFl ) { console . info ( "[gd-sprest] POST requests must include the request digest information for synchronous requests. This is due to the modern page not including this information on the page." ) ; } else { ContextInfo . getWeb ( this . targetInfo . props . url || document . location . pathname . substr ( 0 , document . location . pathname . lastIndexOf ( '/' ) ) ) . execute ( contextInfo => { this . executeRequest ( contextInfo . GetContextWebInformation . FormDigestValue ) ; } ) ; } } else { this . executeRequest ( requestDigest ) ; } } private executeRequest ( requestDigest ) { if ( this . xhr == null ) { return null ; } this . xhr . open ( this . targetInfo . requestMethod == "GET" ? "GET" : "POST" , this . targetInfo . requestUrl , this . asyncFl ) ; if ( this . asyncFl ) { this . xhr . onreadystatechange = ( ) => { if ( this . xhr . readyState == 4 ) { this . onRequestCompleted ? this . onRequestCompleted ( this ) : null ; } } ; } if ( this . targetInfo . props . bufferFl && this . asyncFl ) { this . xhr . responseType = "arraybuffer" ; } else { this . defaultHeaders ( requestDigest ) ; if ( this . targetInfo . requestData && typeof ( this . targetInfo . requestData ) !== "string" ) { this . targetInfo . requestData = this . targetInfo . requestData . byteLength ? this . targetInfo . requestData : JSON . stringify ( this . targetInfo . requestData ) ; } } if ( this . executeFl ) { this . targetInfo . props . bufferFl || this . targetInfo . requestData == null ? this . xhr . send ( ) : this . xhr . send ( this . targetInfo . requestData ) ; } } }