UNPKG

hal-4-angular

Version:

This Angular module offers a HAL/JSON http-client to easily interact with a Spring Data Rest API or any API that implements the Spring Data Rest resource model

197 lines (196 loc) 8.45 kB
import { of as observableOf, throwError as observableThrowError } from 'rxjs'; import { map, mergeMap } from 'rxjs/operators'; import { ResourceService } from './resource.service'; import { isNullOrUndefined } from 'util'; var RestService = /** @class */ (function () { function RestService(type, resource, injector, _embedded) { this.injector = injector; this._embedded = '_embedded'; this.type = type; this.resource = resource; this.resourceService = injector.get(ResourceService); if (!isNullOrUndefined(_embedded)) this._embedded = _embedded; } RestService.prototype.handleError = function (error) { return RestService.handleError(error); }; RestService.handleError = function (error) { return observableThrowError(error); }; RestService.prototype.getAll = function (options, subType) { var _this = this; return this.resourceService.getAll(this.type, this.resource, this._embedded, options, subType).pipe(mergeMap(function (resourceArray) { if (options && options.notPaged && !isNullOrUndefined(resourceArray.first_uri)) { options.notPaged = false; options.size = resourceArray.totalElements; return _this.getAll(options); } else { _this.resourceArray = resourceArray; return observableOf(resourceArray.result); } })); }; RestService.prototype.get = function (id, params) { return this.resourceService.get(this.type, this.resource, id, params); }; RestService.prototype.selfURI = function (id) { return this.resourceService.selfURI(this.type, this.resource, id); }; RestService.prototype.getBySelfLink = function (selfLink) { return this.resourceService.getBySelfLink(this.type, selfLink); }; RestService.prototype.search = function (query, options, subType) { var _this = this; return this.resourceService.search(this.type, query, this.resource, this._embedded, options, subType).pipe(mergeMap(function (resourceArray) { if (options && options.notPaged && !isNullOrUndefined(resourceArray.first_uri)) { options.notPaged = false; options.size = resourceArray.totalElements; return _this.search(query, options, subType); } else { _this.resourceArray = resourceArray; return observableOf(resourceArray.result); } })); }; RestService.prototype.query = function (query, options) { return this.resourceService.query(this.resource, query, options); }; RestService.prototype.searchSingle = function (query, options) { return this.resourceService.searchSingle(this.type, query, this.resource, options); }; RestService.prototype.customQuery = function (query, options) { var _this = this; return this.resourceService.customQuery(this.type, query, this.resource, this._embedded, options).pipe(mergeMap(function (resourceArray) { if (options && options.notPaged && !isNullOrUndefined(resourceArray.first_uri)) { options.notPaged = false; options.size = resourceArray.totalElements; return _this.customQuery(query, options); } else { _this.resourceArray = resourceArray; return observableOf(resourceArray.result); } })); }; RestService.prototype.customQueryPost = function (query, options, body) { var _this = this; return this.resourceService.customQueryPost(this.type, query, this.resource, this._embedded, options, body).pipe(mergeMap(function (resourceArray) { if (options && options.notPaged && !isNullOrUndefined(resourceArray.first_uri)) { options.notPaged = false; options.size = resourceArray.totalElements; return _this.customQueryPost(query, options, body); } else { _this.resourceArray = resourceArray; return observableOf(resourceArray.result); } })); }; RestService.prototype.getByRelationArray = function (relation, builder) { var _this = this; return this.resourceService.getByRelationArray(this.type, relation, this._embedded, builder).pipe(map(function (resourceArray) { _this.resourceArray = resourceArray; return resourceArray.result; })); }; RestService.prototype.getByRelation = function (relation) { return this.resourceService.getByRelation(this.type, relation); }; RestService.prototype.count = function () { return this.resourceService.count(this.resource); }; RestService.prototype.create = function (entity) { return this.resourceService.create(this.resource, entity); }; RestService.prototype.update = function (entity) { return this.resourceService.update(entity); }; RestService.prototype.patch = function (entity) { return this.resourceService.patch(entity); }; RestService.prototype.delete = function (entity) { return this.resourceService.delete(entity); }; RestService.prototype.totalElement = function () { if (this.resourceArray && this.resourceArray.totalElements) return this.resourceArray.totalElements; return 0; }; RestService.prototype.hasFirst = function () { if (this.resourceArray) return this.resourceService.hasFirst(this.resourceArray); return false; }; RestService.prototype.hasNext = function () { if (this.resourceArray) return this.resourceService.hasNext(this.resourceArray); return false; }; RestService.prototype.hasPrev = function () { if (this.resourceArray) return this.resourceService.hasPrev(this.resourceArray); return false; }; RestService.prototype.hasLast = function () { if (this.resourceArray) return this.resourceService.hasLast(this.resourceArray); return false; }; RestService.prototype.next = function () { var _this = this; if (this.resourceArray) return this.resourceService.next(this.resourceArray, this.type).pipe(map(function (resourceArray) { _this.resourceArray = resourceArray; return resourceArray.result; })); else observableThrowError('no resourceArray found'); }; RestService.prototype.prev = function () { var _this = this; if (this.resourceArray) return this.resourceService.prev(this.resourceArray, this.type).pipe(map(function (resourceArray) { _this.resourceArray = resourceArray; return resourceArray.result; })); else observableThrowError('no resourceArray found'); }; RestService.prototype.first = function () { var _this = this; if (this.resourceArray) return this.resourceService.first(this.resourceArray, this.type) .pipe(map(function (resourceArray) { _this.resourceArray = resourceArray; return resourceArray.result; })); else observableThrowError('no resourceArray found'); }; RestService.prototype.last = function () { var _this = this; if (this.resourceArray) return this.resourceService.last(this.resourceArray, this.type) .pipe(map(function (resourceArray) { _this.resourceArray = resourceArray; return resourceArray.result; })); else observableThrowError('no resourceArray found'); }; RestService.prototype.page = function (pageNumber) { var _this = this; if (this.resourceArray) return this.resourceService.page(this.resourceArray, this.type, pageNumber).pipe(map(function (resourceArray) { _this.resourceArray = resourceArray; return resourceArray.result; })); else observableThrowError('no resourceArray found'); }; return RestService; }()); export { RestService };