UNPKG

kentico-cloud-delivery

Version:

Official Kentico Cloud Delivery SDK

74 lines 3.01 kB
import axios from 'axios'; import { from, throwError } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; import { DeliveryCloudError } from '../../models'; var KCErrorNames = { errorCode: 'error_code', message: 'message', requestId: 'request_id', specificCode: 'specific_code' }; var DeliveryHttpService = /** @class */ (function () { function DeliveryHttpService() { } DeliveryHttpService.prototype.get = function (url, headers) { return from(axios.get(url, { headers: this.getHeadersJson(headers) })).pipe(map(function (response) { return { data: response.data, response: response }; }), 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 DeliveryCloudError({ 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 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 throwError({ message: error.message, originalError: error }); } // Something happened in setting up the request that triggered an Error return throwError({ message: error.message, originalError: error }); })); }; DeliveryHttpService.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 DeliveryHttpService; }()); export { DeliveryHttpService }; //# sourceMappingURL=delivery-http-service.js.map