import { AxiosResponse } from 'axios' ; import { isNullish } from '@sap-cloud-sdk/util' ; import { Destination , noDestinationErrorMessage , useOrFetchDestination , DestinationFetchOptions } from '@sap-cloud-sdk/connectivity' ; import { Method , HttpResponse , executeHttpRequestWithOrigin } from '@sap-cloud-sdk/http-client' ; import { filterCustomRequestConfig , OriginOptions } from '@sap-cloud-sdk/http-client/internal' ; typeParam export class OpenApiRequestBuilder < ResponseT = any > { private static isPlaceholder ( pathPart ) { return /^\{.+\}$/ . test ( pathPart ) ; } private customHeaders : Record < string , string > = { } ; private customRequestConfiguration : Record < string , string > = { } ; private _fetchCsrfToken = true ; param method param pathPattern param parameters constructor ( public method , private pathPattern , private parameters ? ) { } param headers returns addCustomHeaders ( headers ) { Object . entries ( headers ) . forEach ( ( [ key , value ] ) => { this . customHeaders [ key . toLowerCase ( ) ] = value ; } ) ; return this ; } param requestConfiguration returns addCustomRequestConfiguration ( requestConfiguration ) { Object . entries ( requestConfiguration ) . forEach ( ( [ key , value ] ) => { this . customRequestConfiguration [ key ] = value ; } ) ; return this ; } returns skipCsrfTokenFetching ( ) { this . _fetchCsrfToken = false ; return this ; } param destination returns async executeRaw ( destination ) { const fetchCsrfToken = this . _fetchCsrfToken && [ 'post' , 'put' , 'patch' , 'delete' ] . includes ( this . method . toLowerCase ( ) ) ; const resolvedDestination = await useOrFetchDestination ( destination ) ; if ( isNullish ( destination ) ) { throw Error ( noDestinationErrorMessage ( destination ) ) ; } return executeHttpRequestWithOrigin ( resolvedDestination as Destination , { ... filterCustomRequestConfig ( this . customRequestConfiguration ) , method : this . method , url : this . getPath ( ) , headers : this . getHeaders ( ) , params : this . getParameters ( ) , data : this . parameters ?. body } , { fetchCsrfToken } ) ; } param destination returns async execute ( destination ) { const response = await this . executeRaw ( destination ) ; if ( isAxiosResponse ( response ) ) { return response . data ; } throw new Error ( 'Could not access response data. Response was not an axios response.' ) ; } private getHeaders ( ) { return { custom : this . customHeaders } ; } private getParameters ( ) { return { requestConfig : this . parameters ?. queryParameters } ; } private getPath ( ) { const pathParameters = this . parameters ?. pathParameters || { } ; const pathParts = this . pathPattern . split ( '/' ) ; return pathParts . map ( part => { if ( OpenApiRequestBuilder . isPlaceholder ( part ) ) { const paramName = part . slice ( 1 , - 1 ) ; const paramValue = pathParameters [ paramName ] ; if ( ! paramValue ) { throw new Error ( `Cannot execute request, no path parameter provided for '${ paramName }'.` ) ; } return encodeURIComponent ( paramValue ) ; } return part ; } ) . join ( '/' ) ; } } export interface OpenApiRequestParameters { pathParameters ? : Record < string , any > ; queryParameters ? : Record < string , any > ; body ? : any ; } function isAxiosResponse ( val ) { return 'data' in val ; }