import { ITargetInfo , ITargetInfoProps } from "../../@types/utils" ; import { ContextInfo } from "../lib" ; import { RequestType } from "." ; /**
 * Target Information
 */ export class TargetInfo implements ITargetInfo { /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ constructor ( props ) { this . props = props || { } ; this . requestData = this . props . data ; this . requestHeaders = this . props . requestHeader ; this . requestMethod = this . props . method ? this . props . method : "GET" ; this . isGraph ? this . setGraphRequestUrl ( ) : this . setRESTRequestUrl ( ) ; } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ props : ITargetInfoProps ; get isBatchRequest ( ) : boolean { return this . props . endpoint == "$batch" ; } get isGraph ( ) : boolean { return this . props . requestType == RequestType . GraphGet || this . props . requestType == RequestType . GraphPost ; } requestData : any ; requestHeaders : object ; requestMethod : string ; requestType : number ; requestUrl : string ; /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ private getDomainUrl ( ) { let url = ContextInfo . document ? ContextInfo . document . location . href : "" ; if ( ContextInfo . isAppWeb ) { url = TargetInfo . getQueryStringValue ( "SPHostUrl" ) + "" ; } url = url . split ( '/' ) ; if ( url && url . length >= 2 ) { url = url [ 0 ] + "//" + url [ 2 ] ; } return url ; } private static getQueryStringValue ( key ) { var queryString = ContextInfo . existsFl && ContextInfo . document ? ContextInfo . document . location . href . split ( '?' ) : [ "" ] ; queryString = queryString . length > 1 ? queryString [ 1 ] : queryString [ 0 ] ; var values = queryString . split ( '&' ) ; for ( var i = 0 ; i < values . length ; i ++ ) { var keyValue = values [ i ] . split ( '=' ) ; if ( keyValue . length == 1 ) { continue ; } if ( decodeURIComponent ( keyValue [ 0 ] ) == key ) { return decodeURIComponent ( keyValue [ 1 ] ) ; } } return null ; } private setGraphRequestUrl ( ) { this . requestUrl = "https://graph.microsoft.com/" + this . props . endpoint ; } private setRESTRequestUrl ( ) { let endpoint = this . props . endpoint ? "/" + this . props . endpoint : "" ; let hostUrl = TargetInfo . getQueryStringValue ( "SPHostUrl" ) ; let qs = ( endpoint . indexOf ( "?" ) === - 1 ? "?" : "&" ) + "@target='{{Target}}'" ; let template = "{{Url}}" + ( this . props . endpoint ? "/_api/{{EndPoint}}{{TargetUrl}}" : "" ) ; if ( ContextInfo . existsFl && ContextInfo . window . $REST && ContextInfo . window . $REST . DefaultRequestToHostFl && ContextInfo . isAppWeb && ! this . props . overrideDefaultRequestToHostFl && this . props . url == null ) { this . props . url = hostUrl ; } if ( this . props . url == null ) { this . props . url = this . props . defaultToWebFl == false ? ContextInfo . siteAbsoluteUrl : ContextInfo . webAbsoluteUrl ; } else if ( /\/_api\// . test ( this . props . url ) ) { var url = this . props . url . toLowerCase ( ) . split ( "/_api/" ) ; if ( ContextInfo . isAppWeb && url [ 0 ] != ContextInfo . webAbsoluteUrl . toLowerCase ( ) ) { this . requestUrl = ContextInfo . webAbsoluteUrl + "/_api/SP.AppContextSite(@target)/" + url [ 1 ] + endpoint + qs . replace ( /{{Target}}/g , url [ 0 ] ) ; } else { this . requestUrl = this . props . url + ( this . props . endpoint ? "/" + this . props . endpoint : "" ) ; } return ; } if ( this . props . url . indexOf ( "http" ) != 0 ) { this . props . url = this . getDomainUrl ( ) + this . props . url ; } if ( ContextInfo . isAppWeb && this . props . url != ContextInfo . webAbsoluteUrl ) { this . requestUrl = template . replace ( /{{Url}}/g , ContextInfo . webAbsoluteUrl ) . replace ( /{{EndPoint}}/g , "SP.AppContextSite(@target)" + endpoint ) . replace ( /{{TargetUrl}}/g , qs . replace ( /{{Target}}/g , this . props . url ) ) ; } else { this . requestUrl = template . replace ( /{{Url}}/g , this . props . url ) . replace ( /{{EndPoint}}/g , this . props . endpoint ) . replace ( /{{TargetUrl}}/g , "" ) ; } } }