@sap-cloud-sdk/core
Version:
SAP Cloud SDK for JavaScript core
83 lines • 3.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.responseDataAccessorV2 = exports.responseDataAccessor = exports.getSingleResult = exports.getLinkedCollectionResult = exports.isCollectionResult = exports.getCollectionResult = void 0;
var util_1 = require("@sap-cloud-sdk/util");
var logger = (0, util_1.createLogger)({
package: 'core',
messageContext: 'response-data-accessor'
});
/**
* Methods to extract the data from OData v2 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 v2 service
* @returns any[] - Collection extracted from the response
*/
function getCollectionResult(data) {
var _a;
validateCollectionResult(data);
return isCollectionResult(data) ? (_a = data === null || data === void 0 ? void 0 : data.d) === null || _a === void 0 ? void 0 : _a.results : [];
}
exports.getCollectionResult = getCollectionResult;
/**
* Checks if the data contains a collection result.
* @param data - Response of the OData service.
* @returns `true`, if the data is a collection result.
*/
function isCollectionResult(data) {
var _a;
return Array.isArray((_a = data === null || data === void 0 ? void 0 : data.d) === null || _a === void 0 ? void 0 : _a.results);
}
exports.isCollectionResult = isCollectionResult;
function validateCollectionResult(data) {
if (!isCollectionResult(data)) {
logger.warn('The given response data does not have the standard OData v2 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 any[] - Collection extracted from the response
*/
function getLinkedCollectionResult(data) {
if (Array.isArray(data === null || data === void 0 ? void 0 : data.results)) {
return data.results;
}
return Array.isArray(data) ? data : [];
}
exports.getLinkedCollectionResult = getLinkedCollectionResult;
/**
* Parses the data of a single result.
* @param data - Response of the OData service.
* @returns The single result object if existent, an empty object otherwise.
*/
function getSingleResult(data) {
var _a;
validateSingleResult(data);
return isSingleResultAsCollection(data) ? (_a = data === null || data === void 0 ? void 0 : data.d) === null || _a === void 0 ? void 0 : _a.results : (data === null || data === void 0 ? void 0 : data.d) || {};
}
exports.getSingleResult = getSingleResult;
// Workaround to be compatible with services that wrongly implement the OData v2 protocol and serve single responses in the same format as collections
function isSingleResultAsCollection(data) {
var _a;
return !!((_a = data === null || data === void 0 ? void 0 : data.d) === null || _a === void 0 ? void 0 : _a.results) && !isCollectionResult(data);
}
function validateSingleResult(data) {
if (isSingleResultAsCollection(data)) {
logger.warn('The given response data has the format for collections instead of the standard OData v2 format for single results.');
}
if (!(data === null || data === void 0 ? void 0 : data.d)) {
logger.warn('The given response data does not have the standard OData v2 format for single results.');
}
}
exports.responseDataAccessor = {
getCollectionResult: getCollectionResult,
getLinkedCollectionResult: getLinkedCollectionResult,
getSingleResult: getSingleResult,
isCollectionResult: isCollectionResult
};
exports.responseDataAccessorV2 = exports.responseDataAccessor;
//# sourceMappingURL=response-data-accessor.js.map
;