@sap-cloud-sdk/odata-v4
Version:
SAP Cloud SDK for JavaScript common functions of OData client generator and OpenAPI clint generator.
79 lines • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.responseDataAccessor = void 0;
exports.getCollectionResult = getCollectionResult;
exports.isCollectionResult = isCollectionResult;
exports.getLinkedCollectionResult = getLinkedCollectionResult;
exports.getSingleResult = getSingleResult;
const util_1 = require("@sap-cloud-sdk/util");
const logger = (0, util_1.createLogger)({
package: 'odata-v4',
messageContext: 'response-data-accessor'
});
/**
* Methods to extract the data from OData v4 responses.
*/
/**
* Extract the collection data from the response.
* If the data does not contain a collection an empty array is returned.
* @param data - Response of the OData v4 service.
* @returns Collection extracted from the response.
* @internal
*/
function getCollectionResult(data) {
validateCollectionResult(data);
return isCollectionResult(data) ? data.value : [];
}
/**
* Checks if the data contains a collection result.
* @param data - Response of the OData v4 service
* @returns `true`, if the data is a collection result
* @internal
*/
function isCollectionResult(data) {
return Array.isArray(data.value);
}
function validateCollectionResult(data) {
if (!isCollectionResult(data)) {
logger.warn('The given response data does not have the standard OData v4 format for collections.');
}
}
/**
* Extract the collection data from the one to many link response.
* If the data does not contain a collection an empty array is returned.
* @param data - Response of the one to many link.
* @returns Collection extracted from the response.
* @internal
*/
function getLinkedCollectionResult(data) {
return Array.isArray(data) ? data : [];
}
/**
* Extract the single entry data from the response.
* If the data does not contain a single object an empty object is returned.
* @param data - Response of the OData v4 service.
* @returns A single object extracted from the response.
* @internal
*/
function getSingleResult(data) {
validateSingleResult(data);
return isSingleResult(data) ? data : {};
}
function isSingleResult(data) {
return typeof data === 'object' && !Array.isArray(data);
}
function validateSingleResult(data) {
if (!isSingleResult(data)) {
logger.warn('The given response data does not have the standard OData v4 format for single results.');
}
}
/**
* @internal
*/
exports.responseDataAccessor = {
getCollectionResult,
getLinkedCollectionResult,
getSingleResult,
isCollectionResult
};
//# sourceMappingURL=response-data-accessor.js.map