import { IMethodInfo , IRequestInfo } from "gd-sprest-def/base" ; import { IBase , IBaseHelper , ITargetInfoProps } from "../../@types/utils" ; import { ContextInfo } from "../lib" ; import { Base , MethodInfo , Request , RequestType , TargetInfo } from "." ; import { XHRRequest } from "./xhrRequest" ; /**
 * Request Helper
 */ export const Helper = { addBaseMethods : ( base , obj ) => { obj [ "addMethods" ] = base . addMethods ; obj [ "base" ] = base . base ; obj [ "done" ] = base . done ; obj [ "execute" ] = base . execute ; obj [ "executeAndWait" ] = base . executeAndWait ; obj [ "executeMethod" ] = base . executeMethod ; obj [ "existsFl" ] = true ; obj [ "getCollection" ] = base . getCollection ; obj [ "getProperty" ] = base . getProperty ; obj [ "parent" ] = base ; obj [ "targetInfo" ] = base . targetInfo ; obj [ "updateMetadataUri" ] = base . updateMetadataUri ; obj [ "waitForRequestsToComplete" ] = base . waitForRequestsToComplete ; } , done : ( base , resolve ) => { base . base = base . base ? base . base : base ; base . responseIndex = base . responseIndex >= 0 ? base . responseIndex : 0 ; Request . waitForRequestsToComplete ( base , ( ) => { let responses = base . base . responses ; base . base . responses = [ ] ; base . base . waitFlags = [ ] ; resolve ? resolve . apply ( base , responses ) : null ; } ) ; } , executeMethod : ( base , methodName , methodConfig , args ? ) => { let targetInfo = null ; let metadata = base [ "d" ] ? base [ "d" ] . __metadata : base [ "__metadata" ] ; if ( metadata && metadata . uri ) { targetInfo = { url : metadata . uri } ; if ( methodConfig . inheritMetadataType && metadata . type ) { methodConfig . metadataType = metadata . type ; } Helper . updateMetadataUri ( base , metadata , targetInfo ) ; } else { targetInfo = Object . create ( base . targetInfo ) ; } var methodInfo = new MethodInfo ( methodName , methodConfig , args ) ; targetInfo . bufferFl = methodConfig . requestType == RequestType . GetBuffer ; targetInfo . data = methodInfo . body ; targetInfo . defaultToWebFl = typeof ( targetInfo . defaultToWebFl ) === "undefined" && base . base ? base . base . targetInfo . defaultToWebFl : targetInfo . defaultToWebFl ; targetInfo . method = methodInfo . requestMethod ; targetInfo . requestDigest = typeof ( targetInfo . requestDigest ) === "undefined" && base . base && base . base . targetInfo . requestDigest ? base . base . targetInfo . requestDigest : targetInfo . requestDigest ; targetInfo . requestType = methodConfig . requestType ; if ( methodInfo . appendEndpointFl ) { targetInfo . endpoint += "." + methodInfo . url ; } else if ( methodInfo . replaceEndpointFl ) { targetInfo . endpoint = methodInfo . url ; } else if ( methodInfo . url && methodInfo . url . length > 0 ) { targetInfo . endpoint = targetInfo . endpoint ? targetInfo . endpoint : "" ; if ( targetInfo . endpoint && methodInfo . url && methodInfo . url . indexOf ( "?" ) != 0 ) { targetInfo . endpoint += "/" ; } targetInfo . endpoint += methodInfo . url ; } let obj = new Base ( targetInfo ) ; obj . base = base . base ? base . base : base ; obj . getAllItemsFl = methodInfo . getAllItemsFl ; obj . parent = base ; obj . requestType = methodConfig . requestType ; if ( methodConfig . returnType ) { Request . addMethods ( obj , { __metadata : { type : methodConfig . returnType } } ) ; } return obj ; } , getCollection : ( base , method , args ? ) => { let targetInfo = Object . create ( base . targetInfo ) ; targetInfo . data = null ; targetInfo . method = null ; let metadata = base [ "d" ] ? base [ "d" ] . __metadata : base [ "__metadata" ] ; if ( metadata && metadata . uri ) { targetInfo . url = metadata . uri ; Helper . updateMetadataUri ( base , metadata , targetInfo ) ; targetInfo . endpoint = method ; } else { targetInfo . endpoint += "/" + method ; } targetInfo . callback = args && typeof ( args [ 0 ] ) === "function" ? args [ 0 ] : null ; let obj = new Base ( targetInfo ) ; obj . base = base . base ? base . base : base ; obj . parent = base ; return obj ; } , getNextSetOfResults : ( base ) => { let targetInfo = Object . create ( base . targetInfo ) ; targetInfo . endpoint = "" ; targetInfo . url = base [ "d" ] . __next ; let obj = new Base ( targetInfo ) ; obj . base = base . base ? base . base : base ; obj . parent = base ; return obj ; } , getProperty : ( base , propertyName , requestType ? ) => { let targetInfo = Object . create ( base . targetInfo ) ; if ( requestType . indexOf ( "graph" ) == 0 ) { targetInfo . requestType = RequestType . GraphGet ; } targetInfo . data = null ; targetInfo . method = null ; let metadata = base [ "d" ] ? base [ "d" ] . __metadata : base [ "__metadata" ] ; if ( metadata && metadata . uri ) { targetInfo . url = metadata . uri ; Helper . updateMetadataUri ( base , metadata , targetInfo ) ; targetInfo . endpoint = propertyName ; } else { targetInfo . endpoint += "/" + propertyName ; } let obj = new Base ( targetInfo ) ; obj . base = base . base ? base . base : base ; obj . parent = base ; requestType ? Request . addMethods ( obj , { __metadata : { type : requestType } } ) : null ; return obj ; } , getRequestInfo : ( base ) : IRequestInfo => { let xhr = new XHRRequest ( true , new TargetInfo ( base . targetInfo ) , null , false ) ; return xhr . requestInfo ; } , stringify : ( base ) : string => { return JSON . stringify ( { response : base . response , status : base . status , targetInfo : { accessToken : base . targetInfo . accessToken , bufferFl : base . targetInfo . bufferFl , defaultToWebFl : base . targetInfo . defaultToWebFl , endpoint : base . targetInfo . endpoint , method : base . targetInfo . method , overrideDefaultRequestToHostFl : base . targetInfo . overrideDefaultRequestToHostFl , requestDigest : base . targetInfo . requestDigest , requestHeader : base . targetInfo . requestHeader , requestInfo : base . targetInfo . requestInfo , requestType : base . targetInfo . requestType , url : base . targetInfo . url } } ) ; } , updateDataCollection : ( obj , results ) => { if ( results ) { obj [ "results" ] = obj [ "results" ] ? obj [ "results" ] . concat ( results ) : results ; if ( obj [ "results" ] . length > 0 ) { let results = obj [ "results" ] ; for ( let result of results ) { Helper . addBaseMethods ( obj , result ) ; Helper . updateMetadata ( obj , result ) ; Request . addMethods ( result , result ) ; } } } } , updateExpandedCollection : ( base , results ) => { for ( let i = 0 ; i < results . length ; i ++ ) { let result = results [ i ] ; if ( result [ "__metadata" ] ) { Helper . addBaseMethods ( base , result ) ; Helper . updateMetadata ( result , result ) ; Request . addMethods ( result , result ) ; } } } , updateExpandedProperties : ( base ) => { if ( base [ "results" ] == null || base . requestType != RequestType . OData ) { return ; } for ( let i = 0 ; i < base [ "results" ] . length ; i ++ ) { let result = base [ "results" ] [ i ] ; for ( let key in result ) { if ( key == "parent" ) { continue ; } let prop = result [ key ] ; if ( prop ) { if ( prop [ "results" ] && prop [ "results" ] . length > 0 ) { Helper . updateExpandedCollection ( base , prop . results ) ; } else if ( prop [ "__metadata" ] ) { Helper . addBaseMethods ( result , prop ) ; Helper . updateMetadata ( result , prop ) ; Request . addMethods ( prop , prop ) ; } } } } } , updateMetadata : ( base , data ) => { if ( ContextInfo . isAppWeb ) { let hostUrl = ContextInfo . webAbsoluteUrl . toLowerCase ( ) ; let requestUrl = data && data . __metadata && data . __metadata . uri ? data . __metadata . uri . toLowerCase ( ) : null ; let targetUrl = base . targetInfo && base . targetInfo . url ? base . targetInfo . url . toLowerCase ( ) : null ; if ( hostUrl == null || requestUrl == null || targetUrl == null ) { return ; } if ( targetUrl . indexOf ( hostUrl ) == 0 ) { return ; } data . __metadata . uri = requestUrl . replace ( hostUrl , targetUrl ) ; } if ( data . __metadata && /SP.WebParts.WebPartDefinition/ . test ( data . __metadata . type ) ) { data . __metadata . uri = data . __metadata . uri . replace ( /SP.WebParts.WebPartDefinition/ , base . targetInfo . endpoint + "/getById('" ) + "')" ; } } , updateMetadataUri : ( base , metadata , targetInfo ) => { if ( /^SP.Field/ . test ( metadata . type ) || /^SP\..*Field$/ . test ( metadata . type ) ) { targetInfo . url = targetInfo . url . replace ( /AvailableFields/ , "fields" ) ; } else if ( /SP.EventReceiverDefinition/ . test ( metadata . type ) ) { targetInfo . url = targetInfo . url . replace ( /\/EventReceiver\// , "/EventReceivers/" ) ; } else if ( /Microsoft.SharePoint.Marketplace.CorporateCuratedGallery.CorporateCatalogAppMetadata/ . test ( targetInfo . url ) ) { targetInfo . url = targetInfo . url . split ( "Microsoft.SharePoint.Marketplace.CorporateCuratedGallery.CorporateCatalogAppMetadata" ) [ 0 ] + "web/tenantappcatalog/availableapps/getbyid('" + base [ "ID" ] + "')" ; } } } ;