import { utils } from "./utils"; /** * The base class for dealing with queries. */ export class QueryBase { selectProperties: string[]; expandProperties: string[]; constructor() { this.selectProperties = null; this.expandProperties = null; } /** * Selects which fields will be returned when querying the service. */ select(...properties: string[]): QueryBase { properties = properties ?? []; this.validatePropertyPaths(properties); return this.clone((newObj: QueryBase) => { newObj.selectProperties = properties.filter((prop, index, props) => props.indexOf(prop) === index); }); } /** * Selects which navigation properties will be returned when querying the service. */ expand(...properties: string[]): QueryBase { properties = properties ?? []; this.validatePropertyPaths(properties); return this.clone((newObj: QueryBase) => { newObj.expandProperties = properties.filter((prop, index, props) => props.indexOf(prop) === index); }); } protected validatePropertyPaths(propPaths: string[], allowNested = false) { for (let i = 0; i < propPaths.length; i++) { const member = propPaths[i]; if (!utils.isString(member)) { throw new Error("Invalid argument in clause"); } if (!allowNested && member.indexOf(".") > -1) { throw new Error("Invalid argument in clause"); } } } protected clone(setter: Function = null) { const newObj = utils.clone(this, new QueryBase(), setter); return newObj; } }