import { ODataServerError } from './errors'; import { ODataFilter } from './filter'; import { ODataQueryParam } from './params'; import { OData } from './request'; import { PlainODataResponse } from './types'; export class EntitySet { private _collection: string; private _client: OData; constructor(collection: string, client: OData) { this._collection = collection; this._client = client; } private _checkError(res: any): void { if (res.error) { switch (this._client.getVersion()) { case 'v2': throw new ODataServerError(res.error?.message?.value); case 'v4': throw new ODataServerError(res.error?.message); default: break; } } } private _getResult(res: PlainODataResponse) { switch (this._client.getVersion()) { case 'v2': // @ts-ignore return res.d?.results || res.d; case 'v4': // @ts-ignore return res?.value || res; default: break; } } async retrieve(id: any, params?: ODataQueryParam): Promise { const res = await this._client.newRequest({ collection: this._collection, method: 'GET', id, params }); this._checkError(res); return this._getResult(res); } async find(base: Partial): Promise { const filter = OData.newFilter(); Object.entries(base).forEach(([key, value]) => { if (typeof value == 'string') { filter.field(key).eqString(value); } else { filter.field(key).eq(value); } }); return this.query(OData.newParam().filter(filter)); } async query(param?: ODataFilter): Promise; async query(param?: ODataQueryParam): Promise; async query(param?: any): Promise { if (param instanceof ODataFilter) { param = ODataQueryParam.newParam().filter(param); } if (param == undefined) { param = OData.newParam(); } const res = await this._client.newRequest({ collection: this._collection, method: 'GET', params: param }); this._checkError(res); return this._getResult(res); } async count(filter?: ODataFilter): Promise { const params = OData.newParam().inlinecount(true).count(true); // set count flag if (filter) { params.filter(filter); } const res = await this._client.newRequest({ collection: this._collection, method: 'GET', params }); this._checkError(res); switch (this._client.getVersion()) { case 'v2': return parseInt(res?.d?.__count); case 'v4': return res['@odata.count']; default: break; } } async create(body: Partial): Promise { const res = await this._client.newRequest({ collection: this._collection, method: 'POST', entity: body }); this._checkError(res); return this._getResult(res); } async update(id: any, body: Partial): Promise { const res = await this._client.newRequest({ collection: this._collection, method: 'PATCH', id, entity: body }); this._checkError(res); return this._getResult(res); } async delete(id: any): Promise { const res = await this._client.newRequest({ collection: this._collection, method: 'DELETE', id }); this._checkError(res); } }