UNPKG

@xompass/sdk-cloud-api

Version:

Xompass Client for cloud-api

173 lines 7.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XompassBaseApi = void 0; var _1 = require("./"); var XompassClient_1 = require("../../XompassClient"); var XompassHTTP_1 = require("./XompassHTTP"); var XompassBaseApi = /** @class */ (function () { function XompassBaseApi() { } /** * This is a core method, every HTTP Call will be done from here, every API Service will * extend this class and use this method to get RESTful communication. */ XompassBaseApi.request = function (method, url, routeParams, urlParams, postBody, retry) { if (routeParams === void 0) { routeParams = {}; } if (urlParams === void 0) { urlParams = {}; } if (postBody === void 0) { postBody = {}; } if (retry === void 0) { retry = false; } // Transpile route variables to the actual request Values Object.keys(routeParams).forEach(function (key) { url = url.replace(new RegExp(':' + key + '(/|$)', 'g'), routeParams[key] + '$1'); }); // Headers to be sent var headers = { 'Content-Type': 'application/json' }; // Authenticate request headers = this.authenticate(url, headers); // Body fix for built-in remote methods using "data", "options" or "credentials // that are the actual body, Custom remote method properties are different and need // to be wrapped into a body object var body = postBody; var queryString = Object.keys(urlParams).length ? '?' : ''; // Separate filter object from url params and add to search query if (urlParams.filter) { queryString += "filter=".concat(encodeURIComponent(JSON.stringify(urlParams.filter))); delete urlParams.filter; } else if (urlParams.where) { // Separate where object from url XompassApiConfigParams and add to search query queryString += "where=".concat(encodeURIComponent(JSON.stringify(urlParams.where))); delete urlParams.where; } queryString += Object.keys(urlParams) .reduce(function (array, key) { var value = urlParams[key]; if (value) { array.push("".concat(key, "=").concat(typeof value === 'object' ? encodeURIComponent(JSON.stringify(value)) : value + '')); } return array; }, []) .join('&'); return XompassHTTP_1.XompassHTTPClient.exec({ method: method, url: "".concat(url).concat(queryString), body: body, headers: headers, routeParams: routeParams, retry: retry, }); }; /** * This method will try to authenticate using either an access_token or basic http auth */ XompassBaseApi.authenticate = function (url, headers) { if (_1.XompassAuth.getAccessTokenId()) { headers['Authorization'] = XompassClient_1.XompassClient.getAuthPrefix() + _1.XompassAuth.getAccessTokenId(); } return headers; }; /** * Generic create method */ XompassBaseApi.create = function (data, retry) { var _this = this; if (retry === void 0) { retry = false; } var url = this.getBasePath(); return this.request('POST', url, undefined, undefined, { data: data }, retry).then(function (response) { return _this.model.factory(response); }); }; /** * Generic findById method */ XompassBaseApi.findById = function (id, _filter, retry) { var _this = this; if (_filter === void 0) { _filter = {}; } if (retry === void 0) { retry = false; } var _urlParams = {}; if (_filter) { _urlParams.filter = _filter; } var url = this.getBasePath() + ':id'; return this.request('GET', url, { id: id }, _urlParams, undefined, retry).then(function (data) { return _this.model.factory(data); }); }; /** * Generic find method */ XompassBaseApi.find = function (_filter, retry) { var _this = this; if (_filter === void 0) { _filter = {}; } if (retry === void 0) { retry = false; } var url = this.getBasePath(); return this.request('GET', url, undefined, { filter: _filter }, undefined, retry).then(function (datum) { return datum.map(function (data) { return _this.model.factory(data); }); }); }; /** * Generic exists method */ XompassBaseApi.exists = function (id, retry) { if (retry === void 0) { retry = false; } var url = this.getBasePath() + ':id/exists'; return this.request('HEAD', url, { id: id }, undefined, undefined, retry).then(function (response) { return response; }); }; /** * Generic findOne method */ XompassBaseApi.findOne = function (_filter, retry) { var _this = this; if (_filter === void 0) { _filter = {}; } if (retry === void 0) { retry = false; } var url = this.getBasePath() + 'findOne'; return this.request('GET', url, undefined, { filter: _filter }, undefined, retry).then(function (data) { return _this.model.factory(data); }); }; /** * Generic deleteById method */ XompassBaseApi.deleteById = function (id, retry) { var _this = this; if (retry === void 0) { retry = false; } var url = this.getBasePath() + ':id'; return this.request('DELETE', url, { id: id }, undefined, undefined, retry).then(function (data) { return _this.model.factory(data); }); }; /** * Generic count method */ XompassBaseApi.count = function (where, retry) { if (where === void 0) { where = {}; } if (retry === void 0) { retry = false; } var url = this.getBasePath() + 'count'; var _urlParams = {}; if (where) { _urlParams.where = where; } return this.request('GET', url, undefined, _urlParams, undefined, retry); }; /** * Generic updateAttributes method */ XompassBaseApi.updateAttributes = function (id, data, retry) { var _this = this; if (retry === void 0) { retry = false; } var url = this.getBasePath() + ':id'; return this.request('PUT', url, { id: id }, undefined, { data: data }, retry).then(function (response) { return _this.model.factory(response); }); }; /** * Generic patchAttributes method */ XompassBaseApi.patchAttributes = function (id, data, retry) { var _this = this; if (retry === void 0) { retry = false; } var url = this.getBasePath() + ':id'; return this.request('PATCH', url, { id: id }, undefined, { data: data }, retry).then(function (response) { return _this.model.factory(response); }); }; XompassBaseApi.getBasePath = function () { return ([ XompassClient_1.XompassClient.getPath(), XompassClient_1.XompassClient.getApiVersion(), this.model.getModelDefinition().path, ].join('/') + '/'); }; return XompassBaseApi; }()); exports.XompassBaseApi = XompassBaseApi; //# sourceMappingURL=XompassBaseApi.js.map