UNPKG

@sap-cloud-sdk/odata-v4

Version:

SAP Cloud SDK for JavaScript common functions of OData client generator and OpenAPI clint generator.

55 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getExpand = getExpand; const internal_1 = require("@sap-cloud-sdk/odata-common/internal"); const get_select_1 = require("./get-select"); const uri_value_converter_1 = require("./uri-value-converter"); function prependDollar(param) { return `$${param}`; } /** * @internal * Get an object containing the given expand as a query parameter, or an empty object if none was given. * @typeParam EntityT - Type of the entity to expand on. * @param expands - The expands to transform to a query parameter. * @param entityApi - Entity API of the entity to build the expand for. * @returns An object containing the query parameter or an empty object/ */ function getExpand(expands = [], entityApi) { return expands.length ? { expand: expands .map(expand => getExpandAsString(expand, entityApi)) .join(',') } : {}; } function getExpandAsString(expand, entityApi) { if (expand instanceof internal_1.AllFields) { return '*'; } let params = {}; if (expand instanceof internal_1.Link) { params = { ...params, ...(0, get_select_1.getSelect)(expand._selects), ...getExpand(expand._expand, expand._linkedEntityApi) }; if (expand instanceof internal_1.OneToManyLink) { params = { ...params, ...(0, internal_1.createGetFilter)(uri_value_converter_1.uriConverter).getFilter((0, internal_1.and)(expand._filters?.filters || []), entityApi), ...(expand._skip && { skip: expand._skip }), ...(expand._top && { top: expand._top }), ...(expand._orderBy && (0, internal_1.getOrderBy)(expand._orderBy)) }; } const subQuery = Object.entries(params) .map(([key, value]) => `${prependDollar(key)}=${value}`) .join(';'); const subQueryWithBrackets = subQuery ? `(${subQuery})` : ''; return `${expand._fieldName}${subQueryWithBrackets}`; } return ''; } //# sourceMappingURL=get-expand.js.map