@sap-ai-sdk/core
Version:
**This package is designed for internal usage and should not be consumed directly.**
75 lines • 3.19 kB
JavaScript
import { Readable } from 'node:stream';
import { json } from 'node:stream/consumers';
import { ErrorWithCause, mergeIgnoreCase, removeLeadingSlashes, removeTrailingSlashes } from '@sap-cloud-sdk/util';
import { executeHttpRequest } from '@sap-cloud-sdk/http-client';
import { getAiCoreDestination } from './context.js';
/**
* Executes a request to the AI Core service.
* @param endpointOptions - The options to call an endpoint.
* @param data - The input parameters for the request.
* @param requestConfig - The request configuration.
* @param destination - The destination to use for the request.
* @returns The {@link HttpResponse} from the AI Core service.
*/
export async function executeRequest(endpointOptions, data, requestConfig, destination) {
const aiCoreDestination = await getAiCoreDestination(destination);
const { url, apiVersion, resourceGroup = 'default' } = endpointOptions;
const mergedRequestConfig = {
...mergeWithDefaultRequestConfig(apiVersion, resourceGroup, requestConfig),
data: JSON.stringify(data)
};
try {
const response = await executeHttpRequest({ ...aiCoreDestination, url: getTargetUrl(aiCoreDestination.url, url) }, mergedRequestConfig, {
fetchCsrfToken: false
});
return response;
}
catch (error) {
// TODO: remove this after the axios issue (https://github.com/axios/axios/issues/6468) has been fixed.
await handleStreamError(error);
throw new ErrorWithCause(`Request failed with status code ${error.status}.`, error);
}
}
async function handleStreamError(error) {
if (error.response?.data && error.config.responseType === 'stream') {
const readable = Readable.from(error.response.data);
error.response.data = await json(readable);
}
}
function mergeWithDefaultRequestConfig(apiVersion, resourceGroup, requestConfig) {
const defaultConfig = {
method: 'post',
headers: {
'content-type': 'application/json',
'ai-resource-group': resourceGroup,
'ai-client-type': 'AI SDK JavaScript'
},
params: apiVersion ? { 'api-version': apiVersion } : {}
};
return {
...defaultConfig,
...requestConfig,
headers: mergeIgnoreCase(defaultConfig.headers, requestConfig?.headers),
params: mergeIgnoreCase(defaultConfig.params, requestConfig?.params)
};
}
/**
* Get target url with endpoint path appended.
* Append path `v2` if the url contains empty pathname `/`.
* @param url - The url, e.g., `http://example.com` or `http://example.com:8000/abc`.
* @param endpointPath - The path to the endpoint, e.g., `/some/endpoint`.
* @returns Target url combining the url and endpoint path.
* @internal
*/
export function getTargetUrl(url, endpointPath) {
// Remove the last trailing slash
url = removeTrailingSlashes(url);
// Remove the first leading slashes
endpointPath = removeLeadingSlashes(endpointPath);
const urlObj = new URL(url);
if (urlObj.pathname === '/') {
return url + '/v2/' + endpointPath;
}
return url + '/' + endpointPath;
}
//# sourceMappingURL=http-client.js.map