import { Observable, throwError } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; import { HttpClient, HttpResponse } from '@angular/common/http'; import { ODataConfiguration } from './angularODataConfiguration'; import { DeleteOperation, GetOperation, PatchOperation, PostOperation, PutOperation } from './angularODataOperation'; import { ODataQuery } from './angularODataQuery'; import { ODataUtils } from './angularODataUtils'; export class ODataService { private _entitiesUri: string; constructor(private _typeName: string, private _http: HttpClient, private config: ODataConfiguration) { this._entitiesUri = config.getEntitiesUri(_typeName); } public get TypeName(): string { return this._typeName; } public Get(key: any): GetOperation { return new GetOperation(this._typeName, this.config, this._http, key); } public Post(entity: T): PostOperation { return new PostOperation(this._typeName, this.config, this._http, entity); } public Patch(entity: T, key: any): PatchOperation { return new PatchOperation(this._typeName, this.config, this._http, key, entity); } public Put(entity: T, key: any): PutOperation { return new PutOperation(this._typeName, this.config, this._http, key, entity); } public Delete(key: any): DeleteOperation { return new DeleteOperation(this._typeName, this.config, this._http, key); } public CustomAction(key: any, actionName: string, postdata: any): Observable { const body = postdata ? JSON.stringify(postdata) : null; return this._http.post(`${this.getEntityUri(key)}/${actionName}`, body, this.config.customRequestOptions).pipe(map(resp => resp)); } public CustomCollectionAction(actionName: string, postdata: any): Observable { const body = postdata ? JSON.stringify(postdata) : null; return this._http.post(`${this._entitiesUri}/${actionName}`, body, this.config.customRequestOptions).pipe(map(resp => resp)); } public CustomFunction(key: any, functionName: string, parameters?: any): Observable { if (parameters) { const params: string = ODataUtils.convertObjectToString(parameters); functionName = `${functionName}(${params})`; } else if (!functionName.endsWith(')') && !functionName.endsWith('()')) { functionName = `${functionName}()`; } return this._http.get(`${this.getEntityUri(key)}/${functionName}`, this.config.defaultRequestOptions).pipe(map(resp => resp)); } public CustomCollectionFunction(functionName: string, parameters?: any): Observable { if (parameters) { const params: string = ODataUtils.convertObjectToString(parameters); functionName = `${functionName}(${params})`; } else if (!functionName.endsWith(')') && !functionName.endsWith('()')) { functionName = `${functionName}()`; } return this._http.get(`${this._entitiesUri}/${functionName}`, this.config.defaultRequestOptions).pipe(map(resp => resp)); } public ItemProperty(key: string, propertyName: string): Observable { return this._http.get(`${this.getEntityUri(key)}/${propertyName}`, this.config.defaultRequestOptions) .pipe(map(r => r.body)); } public Query(): ODataQuery { return new ODataQuery(this.TypeName, this.config, this._http); } protected getEntityUri(key: any): string { return this.config.getEntityUri(key, this._typeName); } protected handleResponse(entity: Observable>): Observable { return entity .pipe( map(this.extractData), catchError((err: any, caught: Observable) => { if (this.config.handleError) { this.config.handleError(err, caught); } return throwError(err); }) ); } private extractData(res: HttpResponse): TResponse { if (res.status < 200 || res.status >= 300) { throw new Error('Bad response status: ' + res.status); } const body: any = res.body; return body || {}; } }