ng2-resource-rest
Version:
Angular2 resource module with simple decorators
143 lines (142 loc) • 4.33 kB
JavaScript
import { ResourceGlobalConfig } from './ResourceGlobalConfig';
var Resource = (function () {
function Resource(http, injector) {
this.http = http;
this.injector = injector;
this._url = null;
this._path = null;
this._headers = null;
this._params = null;
this._data = null;
}
/**
* Get main url of the resource
* @returns {string|Promise<string>}
*/
Resource.prototype.getUrl = function (methodOptions) {
return this._url || this._getUrl(methodOptions) || ResourceGlobalConfig.url || '';
};
/**
* Set resource url
* @param url
*/
Resource.prototype.setUrl = function (url) {
this._url = url;
};
/**
* Get path of the resource
* @returns {string|Promise<string>}
*/
Resource.prototype.getPath = function (methodOptions) {
return this._path || this._getPath(methodOptions) || ResourceGlobalConfig.path || '';
};
/**
* Set resource path
* @param path
*/
Resource.prototype.setPath = function (path) {
this._path = path;
};
/**
* Get headers
* @returns {any|Promise<any>}
*/
Resource.prototype.getHeaders = function (methodOptions) {
return this._headers || this._getHeaders(methodOptions) || ResourceGlobalConfig.headers || {};
};
/**
* Set resource headers
* @param headers
*/
Resource.prototype.setHeaders = function (headers) {
this._headers = headers;
};
/**
* Get default params
* @returns {any|Promise<any>|{}}
*/
Resource.prototype.getParams = function (methodOptions) {
return this._params || this._getParams(methodOptions) || ResourceGlobalConfig.params || {};
};
/**
* Set default resource params
* @param params
*/
Resource.prototype.setParams = function (params) {
this._params = params;
};
/**
* Get default data
* @returns {any|Promise<any>|{}}
*/
Resource.prototype.getData = function (methodOptions) {
return this._data || this._getData(methodOptions) || ResourceGlobalConfig.data || {};
};
/**
* Set default resource params
* @param data
*/
Resource.prototype.setData = function (data) {
this._data = data;
};
/**
* That is called before executing request
* @param req
*/
Resource.prototype.requestInterceptor = function (req, methodOptions) {
return req;
};
/**
* Request observable interceptor
* @param observable
* @returns {Observable<any>}
*/
Resource.prototype.responseInterceptor = function (observable, req, methodOptions) {
return observable.map(function (res) { return res._body ? res.json() : null; });
};
Resource.prototype.removeTrailingSlash = function () {
return true;
};
Resource.prototype.initResultObject = function () {
return {};
};
Resource.prototype.map = function (item) {
return item;
};
Resource.prototype.filter = function (item) {
return true;
};
Resource.prototype.getResourceOptions = function () {
return null;
};
Resource.prototype.createModel = function () {
var ret = this.initResultObject();
ret.$resource = this;
return ret;
};
Resource.prototype._request = function (req, methodOptions) {
if (methodOptions === void 0) { methodOptions = {}; }
var requestObservable = this.http.request(req);
// noinspection TypeScriptValidateTypes
return methodOptions.responseInterceptor ?
methodOptions.responseInterceptor(requestObservable, req, methodOptions) :
this.responseInterceptor(requestObservable, req, methodOptions);
};
Resource.prototype._getUrl = function (methodOptions) {
return null;
};
Resource.prototype._getPath = function (methodOptions) {
return null;
};
Resource.prototype._getHeaders = function (methodOptions) {
return null;
};
Resource.prototype._getParams = function (methodOptions) {
return null;
};
Resource.prototype._getData = function (methodOptions) {
return null;
};
return Resource;
}());
export { Resource };