@sap-cloud-sdk/core
Version:
SAP Cloud SDK for JavaScript core
49 lines • 2.13 kB
JavaScript
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExpandV2 = exports.getExpand = void 0;
var odata_common_1 = require("../../odata-common");
/**
* Get an object containing the given expand as a query parameter, or an empty object if none was given.
* In this OData v2 expand, selected properties are automatically added to the expand.
* @typeparam EntityT - Type of the entity to expand on
* @param selects - The selects which are expanded if necessary
* @param entityConstructor - Constructor type of the entity to expand on
* @returns An object containing the query parameter or an empty object
*/
function getExpand(selects) {
if (selects === void 0) { selects = []; }
var expand = getExpandsAsString(selects);
return expand.length ? { expand: expand.join(',') } : {};
}
exports.getExpand = getExpand;
exports.getExpandV2 = getExpand;
function getExpandsAsString(selectables, initialExpand, parent) {
if (initialExpand === void 0) { initialExpand = []; }
if (parent === void 0) { parent = ''; }
return selectables.reduce(function (combination, selectable) {
var fullFieldName = getPath(parent, selectable._fieldName);
if (selectable instanceof odata_common_1.Link) {
combination = __spreadArray(__spreadArray([], combination, true), [fullFieldName], false);
if (selectable._selects.length) {
return getExpandsAsString(selectable._selects, combination, fullFieldName);
}
}
return combination;
}, initialExpand);
}
function getPath(parent, fieldName) {
if (parent) {
return "".concat(parent, "/").concat(fieldName);
}
return fieldName;
}
//# sourceMappingURL=get-expand.js.map
;