UNPKG

kentico-cloud-delivery

Version:

Official Kentico Cloud Delivery SDK

76 lines 3.09 kB
"use strict"; exports.__esModule = true; var axios_1 = require("axios"); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var models_1 = require("../../models"); var KCErrorNames = { errorCode: 'error_code', message: 'message', requestId: 'request_id', specificCode: 'specific_code' }; var AxiosHttpService = /** @class */ (function () { function AxiosHttpService() { } AxiosHttpService.prototype.get = function (url, headers) { return rxjs_1.from(axios_1["default"].get(url, { headers: this.getHeadersJson(headers) })).pipe(operators_1.map(function (response) { return { data: response.data, response: response }; }), operators_1.catchError(function (error) { // Handling errors: https://github.com/axios/axios#handling-errors if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx var cloudError = void 0; var data = error.response.data; if (data[KCErrorNames.requestId]) { cloudError = new models_1.CloudError({ requestId: data[KCErrorNames.requestId], message: data[KCErrorNames.message] ? data[KCErrorNames.message] : '', errorCode: data[KCErrorNames.errorCode] ? data[KCErrorNames.errorCode] : 0, specificCode: data[KCErrorNames.specificCode] ? data[KCErrorNames.specificCode] : 0 }); } return rxjs_1.throwError({ message: error.message, originalError: error, cloudError: cloudError }); } else if (error.request) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js return rxjs_1.throwError({ message: error.message, originalError: error }); } // Something happened in setting up the request that triggered an Error return rxjs_1.throwError({ message: error.message, originalError: error }); })); }; AxiosHttpService.prototype.getHeadersJson = function (headers) { var headerJson = { 'Content-Type': 'application/json' }; if (!headers) { return headerJson; } headers.forEach(function (header) { headerJson[header.header] = header.value; }); return headerJson; }; return AxiosHttpService; }()); exports.AxiosHttpService = AxiosHttpService; //# sourceMappingURL=axios-http.service.js.map