@sap-ux/fe-mockserver-core
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -36,2 +36,3 @@ import type { ODataMetadata } from './metadata'; | ||
getMetadata(): ODataMetadata; | ||
private apply$Select; | ||
getData(odataRequest: ODataRequest, dontClone?: boolean): Promise<any>; | ||
@@ -38,0 +39,0 @@ updateData(odataRequest: ODataRequest, patchData: any): Promise<any>; |
@@ -291,2 +291,56 @@ "use strict"; | ||
} | ||
apply$Select(expandDefinition, expandData, entityType) { | ||
// preprocess the selected property names | ||
let selectedPropertyNames = Object.keys(expandDefinition.properties); | ||
if (selectedPropertyNames.includes('*') || selectedPropertyNames.length === 0) { | ||
// select all | ||
selectedPropertyNames = ['*']; | ||
} | ||
else { | ||
// select the specified properties plus all key properties | ||
selectedPropertyNames = selectedPropertyNames | ||
.map((property) => property.split('/', 1)[0]) // reduce paths to their first segment ($select=foo/bar ~> $select=foo), accepting too much data in the response for simplicity | ||
.concat(...entityType.keys.map((key) => key.name)); // always include key properties | ||
} | ||
const expandedNavProps = Object.keys(expandDefinition.expand).reduce((navigationProperties, navPropName) => { | ||
const navigationProperty = entityType.navigationProperties.find((navProp) => navProp.name === navPropName); | ||
if (navigationProperty) { | ||
navigationProperties.push(navigationProperty); | ||
} | ||
return navigationProperties; | ||
}, []); | ||
const processedNavProps = []; | ||
for (const element of expandData) { | ||
// the element might be null (if it is a 1:1 navigation property) | ||
if (element === null || element === undefined) { | ||
continue; | ||
} | ||
// if all properties are requested ("*") keep everything else delete unwanted properties | ||
if (!selectedPropertyNames.includes('*')) { | ||
Object.keys(element) | ||
.filter((propertyName) => !selectedPropertyNames.includes(propertyName)) | ||
.forEach((propertyName) => { | ||
delete element[propertyName]; | ||
}); | ||
} | ||
for (const navProp of expandedNavProps) { | ||
processedNavProps.push(navProp); | ||
const subElement = element[navProp.name]; | ||
const subExpand = expandDefinition.expand[navProp.name]; | ||
if (Array.isArray(subElement)) { | ||
this.apply$Select(subExpand, subElement, navProp.targetType); | ||
} | ||
else { | ||
this.apply$Select(subExpand, [subElement], navProp.targetType); | ||
} | ||
} | ||
// we may still want to remove navProp that were inline and not requested | ||
for (const navigationProperty of entityType.navigationProperties) { | ||
if (!processedNavProps.includes(navigationProperty) && | ||
navigationProperty.name !== 'DraftAdministrativeData') { | ||
delete element[navigationProperty.name]; | ||
} | ||
} | ||
} | ||
} | ||
async getData(odataRequest, dontClone = false) { | ||
@@ -557,41 +611,2 @@ var _a, _b, _c, _d, _e, _f, _g, _h; | ||
if (odataRequest.selectedProperties && Object.keys(odataRequest.selectedProperties).length > 0) { | ||
const apply$Select = (expandDefinition, expandData) => { | ||
const selectedPropertyNames = Object.keys(expandDefinition.properties); | ||
const expandedNavProps = Object.keys(expandDefinition.expand); | ||
let allNavProps = (currentEntityType === null || currentEntityType === void 0 ? void 0 : currentEntityType.navigationProperties.map((navProp) => navProp.name)) || []; | ||
allNavProps = allNavProps.filter((navPropName) => navPropName !== 'DraftAdministrativeData'); | ||
expandData.forEach((element) => { | ||
// the element might be null (if it is a 1:1 navigation property) | ||
if (element === null || element === undefined) { | ||
return; | ||
} | ||
// if all properties are requested ("*") keep everything else delete unwanted properties | ||
if (!selectedPropertyNames.includes('*')) { | ||
const propertyNames = Object.keys(element); | ||
const differenceKeys = propertyNames.filter((propertyName) => !selectedPropertyNames.includes(propertyName)); | ||
differenceKeys.forEach((k) => { | ||
delete element[k]; | ||
}); | ||
} | ||
expandedNavProps.forEach((navProp) => { | ||
const expandedIndex = allNavProps.indexOf(navProp); | ||
if (expandedIndex !== -1) { | ||
allNavProps.splice(expandedIndex, 1); | ||
} | ||
const subElement = element[navProp]; | ||
if (Array.isArray(subElement)) { | ||
apply$Select(expandDefinition.expand[navProp], subElement); | ||
} | ||
else { | ||
apply$Select(expandDefinition.expand[navProp], [subElement]); | ||
} | ||
}); | ||
if (selectedPropertyNames.includes('*') && currentEntitySet && allNavProps.length > 0) { | ||
// we may still want to remove navProp that were inline and not requested | ||
allNavProps.forEach((k) => { | ||
delete element[k]; | ||
}); | ||
} | ||
}); | ||
}; | ||
const select = {}; | ||
@@ -604,6 +619,6 @@ Object.keys(odataRequest.selectedProperties).forEach((prop) => (select[prop] = true)); | ||
if (Array.isArray(data)) { | ||
apply$Select(expand, data); | ||
this.apply$Select(expand, data, currentEntityType); | ||
} | ||
else if (data != null && data.constructor.name === 'Object') { | ||
apply$Select(expand, [data]); | ||
this.apply$Select(expand, [data], currentEntityType); | ||
} | ||
@@ -610,0 +625,0 @@ } |
{ | ||
"name": "@sap-ux/fe-mockserver-core", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "SAP Fiori OData - Fiori elements mock server core", | ||
@@ -34,2 +34,4 @@ "repository": { | ||
"@types/body-parser": "1.17.1", | ||
"@types/graceful-fs": "^4.1.5", | ||
"@types/connect": "^3.4.35", | ||
"@types/node-fetch": "2", | ||
@@ -36,0 +38,0 @@ "@types/express": "4.17.13", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
399794
4779
15
4