import { unixEOL } from '@sap-cloud-sdk/util' ; import voca from 'voca' ; import { ODataRequest , ODataRequestConfig } from '../../request' ; import { MethodRequestBuilder } from '../request-builder-base' ; import { DeSerializers } from '../../de-serializers' ; import { BatchChangeSet } from './batch-change-set' ; import { BatchRequestSerializationOptions , BatchSubRequestPathType } from './batch-request-options' ; import type { BatchRequestBuilder } from './batch-request-builder' ; param changeSet param options returns internal export function serializeChangeSet < DeSerializersT extends DeSerializers > ( changeSet , options = { } ) { if ( changeSet . requests . length ) { return [ `Content-Type: multipart/mixed; boundary=${ changeSet . boundary }` , '' , `--${ changeSet . boundary }` , changeSet . requests . map ( request => serializeRequest ( request , options ) ) . join ( `${ unixEOL }--${ changeSet . boundary }${ unixEOL }` ) , `--${ changeSet . boundary }--` ] . join ( unixEOL ) ; } } param request param options returns internal export function serializeRequest ( request , options = { } ) { const odataRequest = new ODataRequest ( request . requestConfig , options . destination ) ; const headers = { ... odataRequest . defaultHeaders ( ) , ... odataRequest . eTagHeaders ( ) , ... odataRequest . customHeaders ( ) } ; const requestHeaders = Object . entries ( headers ) . map ( ( [ key , value ] ) => `${ voca . titleCase ( key ) }: ${ value }` ) ; return [ 'Content-Type: application/http' , 'Content-Transfer-Encoding: binary' , '' , `${ request . requestConfig . method . toUpperCase ( ) } ${ getUrl ( odataRequest , options . subRequestPathType ) } HTTP/1.1` , ... ( requestHeaders . length ? requestHeaders : [ '' ] ) , '' , ... getPayload ( request ) , '' ] . join ( unixEOL ) ; } function getUrl < ConfigT extends ODataRequestConfig > ( request , subRequestPathType ? ) { switch ( subRequestPathType ) { case 'absolute' : return request . url ( ) ; case 'relativeToEntity' : return `/${ request . relativeUrl ( false ) }` ; default : return `/${ request . relativeUrl ( ) }` ; } } function getPayload ( request ) { return request . requestConfig . method !== 'get' ? [ JSON . stringify ( request . requestConfig . payload ) ] : [ ] ; } function validateOptions ( options ) { if ( options . subRequestPathType === 'absolute' && ! options . destination ?. url ) { throw new Error ( "Cannot serialize batch request. Invalid destination provided for sub request path type 'absolute'" ) ; } } param request param options returns internal export function serializeBatchRequest < DeSerializersT extends DeSerializers > ( request , options = { } ) { validateOptions ( options ) ; const serializedSubRequests = request . requests . map ( subRequest => subRequest instanceof MethodRequestBuilder ? serializeRequest ( subRequest , options ) : serializeChangeSet ( subRequest , options ) ) . filter ( validRequest => ! ! validRequest ) . join ( `${ unixEOL }--${ request . requestConfig . boundary }${ unixEOL }` ) ; const serializedBatchRequest = serializedSubRequests ? [ `--${ request . requestConfig . boundary }` , serializedSubRequests , `--${ request . requestConfig . boundary }--` , '' ] . join ( unixEOL ) : serializedSubRequests ; return serializedBatchRequest . replace ( /\n/g , '\r\n' ) ; }