angular-odata-es5
Version:
OData service for Angular (es5 version)
100 lines • 4.44 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ODataPagedResult } from './angularODataPagedResult';
import { ODataUtils } from './angularODataUtils';
var KeyConfigs = /** @class */ (function () {
function KeyConfigs() {
this.filter = '$filter';
this.top = '$top';
this.skip = '$skip';
this.orderBy = '$orderby';
this.select = '$select';
this.search = '$search';
this.expand = '$expand';
this.apply = '$apply';
this.count = '$count';
this.maxPerPage = 'odata.maxpagesize';
}
return KeyConfigs;
}());
export { KeyConfigs };
var ODataConfiguration = /** @class */ (function () {
function ODataConfiguration() {
this._postHeaders = new HttpHeaders({ 'Content-Type': 'application/json; charset=utf-8' });
this._baseUrl = 'http://localhost/odata';
this.keys = new KeyConfigs();
this.defaultRequestOptions = { headers: new HttpHeaders(), observe: 'response' };
this.postRequestOptions = { headers: this._postHeaders, observe: 'response' };
this.customRequestOptions = { headers: new HttpHeaders(), observe: 'response' };
}
Object.defineProperty(ODataConfiguration.prototype, "baseUrl", {
get: function () {
return this._baseUrl;
},
set: function (baseUrl) {
this._baseUrl = baseUrl.replace(/\/+$/, '');
},
enumerable: true,
configurable: true
});
ODataConfiguration.prototype.getEntitiesUri = function (typeName) {
if (typeName) {
return this.baseUrl + "/" + this.sanitizeTypeName(typeName);
}
return this.baseUrl;
};
ODataConfiguration.prototype.getEntityUri = function (key, typeName) {
return this.getEntitiesUri(typeName) + "(" + ODataUtils.quoteValue(key) + ")";
};
ODataConfiguration.prototype.handleError = function (err, caught) {
console.warn('OData error: ', err, caught);
};
ODataConfiguration.prototype.extractQueryResultDataAsNumber = function (res) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
return (res && res.body);
};
ODataConfiguration.prototype.extractQueryResultData = function (res) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
return (res && res.body && res.body.value);
};
ODataConfiguration.prototype.extractQueryResultDataWithCount = function (res) {
var pagedResult = new ODataPagedResult();
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
var body = res.body;
var entities = body.value;
pagedResult.data = entities;
var parseResult = ODataUtils.tryParseInt(body['@odata.count']);
if (parseResult.valid) {
pagedResult.count = parseResult.value;
}
else {
console.warn('Cannot determine OData entities count. Falling back to collection length.');
pagedResult.count = entities.length;
}
if (body['@odata.nextLink']) {
pagedResult.nextLink = body['@odata.nextLink'];
}
return pagedResult;
};
ODataConfiguration.prototype.sanitizeTypeName = function (typeName) {
return typeName.replace(/\/+$/, '').replace(/^\/+/, '');
};
ODataConfiguration = __decorate([
Injectable()
], ODataConfiguration);
return ODataConfiguration;
}());
export { ODataConfiguration };
//# sourceMappingURL=angularODataConfiguration.js.map