-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.js
100 lines (82 loc) · 3.05 KB
/
query.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import axios from 'axios';
import { trackErrorInfo } from './digitalData';
import { checkErrorCode } from '../utils/validators';
import { version } from '../../package.json';
export const setupInterceptors = (history) => {
const errorHandler = (error) => {
const response = error.response;
if (response) {
// This is wrapped in a try catch so error logging
// does not prevent the promise from being rejected
// Ex. This could occur if an error code is not returned from services
try {
const { data: { code }, status } = error.response;
trackErrorInfo({
name: code,
code: status,
action: `${code} action`,
message: checkErrorCode(code),
type: 'Application Error'
});
} catch {
console.log('An error occured while logging error.');
}
// Unauthorized response statuses from service
// Kick the user to the timeout page
if (response.status === 401 && !(window.location.href.indexOf('login') > -1)) {
let timeoutUrl = '/timeout';
if (sessionStorage.getItem('userTypeMtf') === 'true') {
timeoutUrl = '/mtfTimeout';
}
return history.push(timeoutUrl);
}
// F5 blocked the request using Shape
if (response.status === 403 && response.headers['shape-security']) {
console.log('response iss', response);
const doc = document.createElement('div');
doc.innerHTML = response.data;
const id = doc.querySelector('span').innerHTML;
return history.push(`/security/${id}`);
}
}
return Promise.reject(error);
};
axios.interceptors.request.use((request) => {
// const accessToken = authClient.getAccessToken();
// console.log('axios.interceptors.request accessToken', accessToken);
// const accessToken = JSON.parse(sessionStorage.getItem('accessToken'));
// request.headers.common.Authorization = `Bearer ${accessToken}`;
request.headers.common.Accept = 'application/json'; // eslint-disable-line no-param-reassign
return request;
}, (error) => Promise.reject(error));
axios.interceptors.response.use(
(response) => response,
errorHandler
);
};
const query = ({
endpoint,
data = null,
method = 'POST',
headers = {},
opts = { retry: true },
type = 'json'
}) => {
const requestObj = {
method,
url: endpoint,
withCredentials: true,
retry: opts.retry,
headers: {
'Content-Type': 'application/json',
...headers
},
data,
responseType: type
};
if (version.includes('alpha')) {
requestObj.headers['Cigna-Synthetic'] = 'CignaScanning';
}
return axios(requestObj);
};
export default query;