import { IBase , ITargetInfo } from "../../@types/utils" ; import { ContextInfo } from "../lib" ; import { TargetInfo } from "." ; /**
 * Batch Requests
 */ export class Batch { static execute ( base , args ) { let appendFl = false ; let callback = null ; for ( let i = 0 ; i < args . length ; i ++ ) { let arg = args [ i ] ; switch ( typeof ( arg ) ) { case "boolean" : appendFl = arg ; break ; case "function" : callback = arg ; break ; } } base . base = base . base ? base . base : base ; if ( appendFl && base . base . batchRequests ) { base . base . batchRequests [ base . base . batchRequests . length - 1 ] . push ( { callback , targetInfo : new TargetInfo ( base . targetInfo ) } ) ; } else { base . base . batchRequests = base . base . batchRequests || [ ] ; base . base . batchRequests . push ( [ { callback , targetInfo : new TargetInfo ( base . targetInfo ) } ] ) ; } return base ; } static getTargetInfo ( requests ) { let batchId = "batch_" + ContextInfo . generateGUID ( ) ; let batchRequests = [ ] ; for ( let i = 0 ; i < requests . length ; i ++ ) { batchRequests . push ( Batch . createBatch ( batchId , requests [ i ] ) ) ; } batchRequests . push ( "--" + batchId + "--" ) ; return new TargetInfo ( { endpoint : "$batch" , method : "POST" , data : batchRequests . join ( "\r\n" ) , requestHeader : { "Content-Type" : 'multipart/mixed; boundary="' + batchId + '"' } } ) ; } private static createBatch ( batchId , requests ) { let batch = [ ] ; for ( let i = 0 ; i < requests . length ; i ++ ) { let request = requests [ i ] ; batch . push ( "--" + batchId ) ; let requiresChangeset = request && request . targetInfo . requestMethod != "GET" ; if ( requiresChangeset ) { let changesetId = "changeset_" + ContextInfo . generateGUID ( ) ; batch . push ( "Content-Type: multipart/mixed; boundary=" + changesetId ) ; batch . push ( "" ) ; batch . push ( "--" + changesetId ) ; batch . push ( "Content-Type: application/http" ) ; batch . push ( "Content-Transfer-Encoding: binary" ) ; batch . push ( "" ) ; batch . push ( "POST " + request . targetInfo . requestUrl + " HTTP/1.1" ) ; batch . push ( "Content-Type: application/json;odata=verbose" ) ; batch . push ( "" ) ; request . targetInfo . requestData ? batch . push ( request . targetInfo . requestData ) : null ; batch . push ( "" ) ; batch . push ( "--" + changesetId + "--" ) ; } else { batch . push ( "Content-Type: application/http" ) ; batch . push ( "Content-Transfer-Encoding: binary" ) ; batch . push ( "" ) ; batch . push ( "GET " + request . targetInfo . requestUrl + " HTTP/1.1" ) ; batch . push ( "Accept: application/json;odata=verbose" ) ; batch . push ( "" ) ; request . targetInfo . requestData ? batch . push ( request . targetInfo . requestData ) : null ; batch . push ( "" ) ; } } let batchRequest = batch . join ( "\r\n" ) ; let request = [ ] ; request . push ( "Content-Type: multipart/mixed; boundary=" + batchId ) ; request . push ( "Content-Length: " + batchRequest . length ) ; request . push ( "" ) ; request . push ( batchRequest ) ; request . push ( "" ) ; return request . join ( "\r\n" ) ; } static processCallbacks ( requests = [ ] ) { for ( let i = 0 ; i < requests . length ; i ++ ) { let request = requests [ i ] ; for ( let j = 0 ; j < request . length ; j ++ ) { let batchRequest = request [ j ] ; if ( batchRequest . callback ) { batchRequest . callback ( batchRequest . response , batchRequest . targetInfo ) ; } } } } }