angular-odata-es5
Version:
OData service for Angular (es5 version)
245 lines • 9.96 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { HttpParams } from '@angular/common/http';
import { Dictionary, List } from 'linq-collections';
import { throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
var ODataOperation = /** @class */ (function () {
function ODataOperation(typeName, config, http) {
this.typeName = typeName;
this.config = config;
this.http = http;
this._expand = [];
this._select = [];
}
ODataOperation.prototype.Expand = function (expand) {
if (expand) {
this._expand = this.toStringArray(expand);
}
return this;
};
ODataOperation.prototype.Select = function (select) {
if (select) {
this._select = this.toStringArray(select);
}
return this;
};
ODataOperation.prototype.getParams = function () {
var _this = this;
var expandData = new Dictionary();
var normalSelects = new List();
this._expand.forEach(function (name) { return expandData.set(name, new List()); });
this._select.forEach(function (select) {
var items = select.split('/');
// Expand contains string like: `Boss/Name`
if (items.length > 1) {
var expandName = items[0];
var propertyName = items[1];
if (!expandData.containsKey(expandName)) {
expandData.set(expandName, new List());
}
expandData.get(expandName).push(propertyName);
}
else {
// Expand is just a simple string like: `Boss`
normalSelects.push(select);
}
});
var params = new HttpParams();
var expands = expandData.distinct().select(function (element) {
if (element.value.any()) {
return element.key + "(" + _this.config.keys.select + "=" + _this.toCommaString(element.value) + ")";
}
return element.key;
});
if (expands.any()) {
params = params.append(this.config.keys.expand, this.toCommaString(expands));
}
if (normalSelects.any()) {
params = params.append(this.config.keys.select, this.toCommaString(normalSelects));
}
return params;
};
ODataOperation.prototype.handleResponse = function (entity) {
var _this = this;
return entity
.pipe(map(this.extractData), catchError(function (err, caught) {
if (_this.config.handleError) {
_this.config.handleError(err, caught);
}
return throwError(err);
}));
};
ODataOperation.prototype.getDefaultRequestOptions = function () {
var options = Object.assign({}, this.config.defaultRequestOptions);
options.params = this.getParams();
return options;
};
ODataOperation.prototype.getPostRequestOptions = function () {
var options = Object.assign({}, this.config.postRequestOptions);
options.params = this.getParams();
return options;
};
ODataOperation.prototype.GenerateUrl = function (entitiesUri) {
var params = this.getParams();
if (params.keys().length > 0) {
return entitiesUri + "?" + params;
}
return entitiesUri;
};
ODataOperation.prototype.toStringArray = function (input) {
if (!input) {
return [];
}
if (input instanceof String || typeof input === 'string') {
return input.split(',').map(function (s) { return s.trim(); });
}
if (input instanceof Array) {
return input;
}
return input.toArray();
};
ODataOperation.prototype.toCommaString = function (input) {
if (input instanceof String || typeof input === 'string') {
return input;
}
if (input instanceof Array) {
return input.join();
}
return input.toArray().join();
};
ODataOperation.prototype.extractData = function (res) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
var body = res.body;
return body || {};
};
return ODataOperation;
}());
export { ODataOperation };
var OperationWithKey = /** @class */ (function (_super) {
__extends(OperationWithKey, _super);
function OperationWithKey(_typeName, config, http, entityKey) {
var _this = _super.call(this, _typeName, config, http) || this;
_this._typeName = _typeName;
_this.config = config;
_this.http = http;
_this.entityKey = entityKey;
return _this;
}
OperationWithKey.prototype.getEntityUri = function () {
return this.config.getEntityUri(this.entityKey, this.typeName);
};
OperationWithKey.prototype.GetUrl = function () {
return this.GenerateUrl(this.getEntityUri());
};
return OperationWithKey;
}(ODataOperation));
export { OperationWithKey };
var OperationWithEntity = /** @class */ (function (_super) {
__extends(OperationWithEntity, _super);
function OperationWithEntity(_typeName, config, http, entity) {
var _this = _super.call(this, _typeName, config, http) || this;
_this._typeName = _typeName;
_this.config = config;
_this.http = http;
_this.entity = entity;
return _this;
}
OperationWithEntity.prototype.getEntitiesUri = function () {
return this.config.getEntitiesUri(this._typeName);
};
OperationWithEntity.prototype.GetUrl = function () {
return this.GenerateUrl(this.getEntitiesUri());
};
return OperationWithEntity;
}(ODataOperation));
export { OperationWithEntity };
var OperationWithKeyAndEntity = /** @class */ (function (_super) {
__extends(OperationWithKeyAndEntity, _super);
function OperationWithKeyAndEntity(_typeName, config, http, entityKey, entity) {
var _this = _super.call(this, _typeName, config, http, entityKey) || this;
_this._typeName = _typeName;
_this.config = config;
_this.http = http;
_this.entityKey = entityKey;
_this.entity = entity;
return _this;
}
OperationWithKeyAndEntity.prototype.getEntityUri = function () {
return this.config.getEntityUri(this.entityKey, this._typeName);
};
return OperationWithKeyAndEntity;
}(OperationWithKey));
export { OperationWithKeyAndEntity };
var GetOperation = /** @class */ (function (_super) {
__extends(GetOperation, _super);
function GetOperation() {
return _super !== null && _super.apply(this, arguments) || this;
}
GetOperation.prototype.Exec = function () {
return _super.prototype.handleResponse.call(this, this.http.get(this.getEntityUri(), this.getDefaultRequestOptions()));
};
return GetOperation;
}(OperationWithKey));
export { GetOperation };
var PostOperation = /** @class */ (function (_super) {
__extends(PostOperation, _super);
function PostOperation() {
return _super !== null && _super.apply(this, arguments) || this;
}
PostOperation.prototype.Exec = function () {
var body = this.entity ? JSON.stringify(this.entity) : null;
return _super.prototype.handleResponse.call(this, this.http.post(this.getEntitiesUri(), body, this.getPostRequestOptions()));
};
return PostOperation;
}(OperationWithEntity));
export { PostOperation };
var PatchOperation = /** @class */ (function (_super) {
__extends(PatchOperation, _super);
function PatchOperation() {
return _super !== null && _super.apply(this, arguments) || this;
}
PatchOperation.prototype.Exec = function () {
var body = this.entity ? JSON.stringify(this.entity) : null;
return _super.prototype.handleResponse.call(this, this.http.patch(this.getEntityUri(), body, this.getPostRequestOptions()));
};
return PatchOperation;
}(OperationWithKeyAndEntity));
export { PatchOperation };
var PutOperation = /** @class */ (function (_super) {
__extends(PutOperation, _super);
function PutOperation() {
return _super !== null && _super.apply(this, arguments) || this;
}
PutOperation.prototype.Exec = function () {
var body = this.entity ? JSON.stringify(this.entity) : null;
return _super.prototype.handleResponse.call(this, this.http.put(this.getEntityUri(), body, this.getPostRequestOptions()));
};
return PutOperation;
}(OperationWithKeyAndEntity));
export { PutOperation };
var DeleteOperation = /** @class */ (function (_super) {
__extends(DeleteOperation, _super);
function DeleteOperation() {
return _super !== null && _super.apply(this, arguments) || this;
}
DeleteOperation.prototype.Exec = function () {
return _super.prototype.handleResponse.call(this, this.http.delete(this.getEntityUri(), this.config.defaultRequestOptions));
};
return DeleteOperation;
}(OperationWithKey));
export { DeleteOperation };
//# sourceMappingURL=angularODataOperation.js.map