kentico-cloud-delivery
Version:
Official Kentico Cloud Delivery SDK
74 lines • 2.98 kB
JavaScript
import axios from 'axios';
import { from, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { CloudError } from '../../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 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 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 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
});
}));
};
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;
}());
export { AxiosHttpService };
//# sourceMappingURL=axios-http.service.js.map