UNPKG

2.42 kBJavaScriptView Raw
1"use strict";
2var __spreadArray = (this && this.__spreadArray) || function (to, from) {
3 for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
4 to[j] = from[i];
5 return to;
6};
7Object.defineProperty(exports, "__esModule", { value: true });
8exports.getSelectV2 = exports.getSelect = void 0;
9var odata_common_1 = require("../../odata-common");
10/**
11 * Get an object containing the given Selectables as query parameter, or an empty object if none were given.
12 * This retrieves where in addition to the selection (`select`) there is also an expansion (`expand`) needed.
13 * @typeparam EntityT - Type of the entity to get the selection for
14 * @param selects - The list of selectables to be transformed to query parameters
15 * @returns An object containing the query parameters or an empty object
16 */
17function getSelect(selects) {
18 if (selects === void 0) { selects = []; }
19 var select = getSelectsAsStrings(selects);
20 return select.length ? { select: filterSelects(select).join(',') } : {};
21}
22exports.getSelect = getSelect;
23exports.getSelectV2 = getSelect;
24function selectionLevel(select) {
25 return select.split('/').slice(0, -1).join('/');
26}
27function filterSelects(selects) {
28 var allFieldSelects = selects.filter(function (select) { return select.endsWith('*'); });
29 var selectionLevels = allFieldSelects.map(function (select) { return selectionLevel(select); });
30 return __spreadArray(__spreadArray([], allFieldSelects), selects.filter(function (select) { return !selectionLevels.includes(selectionLevel(select)); }));
31}
32function getSelectsAsStrings(selectables, initialSelect, parent) {
33 if (initialSelect === void 0) { initialSelect = []; }
34 if (parent === void 0) { parent = ''; }
35 return selectables.reduce(function (select, selectable) {
36 var fullFieldName = getPath(parent, selectable._fieldName);
37 if (selectable instanceof odata_common_1.Link) {
38 if (selectable._selects.length) {
39 return getSelectsAsStrings(selectable._selects, select, fullFieldName);
40 }
41 return __spreadArray(__spreadArray([], select), [fullFieldName + "/*"]);
42 }
43 return __spreadArray(__spreadArray([], select), [fullFieldName]);
44 }, initialSelect);
45}
46function getPath(parent, fieldName) {
47 if (parent) {
48 return parent + "/" + fieldName;
49 }
50 return fieldName;
51}
52//# sourceMappingURL=get-select.js.map
\No newline at end of file