generated from RedHatInsights/frontend-starter-app
-
Notifications
You must be signed in to change notification settings - Fork 25
/
axiosInterceptors.js
58 lines (43 loc) · 1.49 KB
/
axiosInterceptors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {
authInterceptor
} from '@redhat-cloud-services/frontend-components-utilities/interceptors';
import axios from 'axios';
import { ReadOnlyNotification } from './constants';
const axiosInstance = axios.create();
export function errorInterceptor(err) {
if (!axios.isCancel(err)) {
const { response } = { ...err };
if (response && err.isAxiosError) {
const { status, statusText, data } = response;
if (!status) {
return err;
} else {
const genericError = {
title: 'There was an error getting data'
};
const result = { ...genericError, detail: data.error || statusText, status };
throw result;
}
}
return err;
}
return err;
}
export function readOnlyInterceptor(error) {
if (error.response && error.response.status === 503) {
const data = ReadOnlyNotification;
throw data;
}
throw error;
}
export function responseDataInterceptor(response) {
if (response.data && typeof response.data === 'object') {
return { ...response.data, status: response.status };
}
return response;
}
axiosInstance.interceptors.request.use(authInterceptor);
axiosInstance.interceptors.response.use(responseDataInterceptor);
axiosInstance.interceptors.response.use(null, readOnlyInterceptor);
axiosInstance.interceptors.response.use(null, errorInterceptor);
export default axiosInstance;