-
Notifications
You must be signed in to change notification settings - Fork 731
/
api.js
142 lines (130 loc) · 4.72 KB
/
api.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import axios from 'axios';
import { API_NOTIFICATION_MESSAGES, SERVICE_URLS } from '../constants/config';
import { getAccessToken, getRefreshToken, setAccessToken, getType } from '../utils/common-utils';
const API_URL = 'http://localhost:8000';
const axiosInstance = axios.create({
baseURL: API_URL,
timeout: 10000,
headers: {
"content-type": "application/json"
}
});
axiosInstance.interceptors.request.use(
function(config) {
if (config.TYPE.params) {
config.params = config.TYPE.params
} else if (config.TYPE.query) {
config.url = config.url + '/' + config.TYPE.query;
}
return config;
},
function(error) {
return Promise.reject(error);
}
);
axiosInstance.interceptors.response.use(
function(response) {
// Stop global loader here
return processResponse(response);
},
function(error) {
// Stop global loader here
return Promise.reject(ProcessError(error));
}
)
///////////////////////////////
// If success -> returns { isSuccess: true, data: object }
// If fail -> returns { isFailure: true, status: string, msg: string, code: int }
//////////////////////////////
const processResponse = (response) => {
if (response?.status === 200) {
return { isSuccess: true, data: response.data }
} else {
return {
isFailure: true,
status: response?.status,
msg: response?.msg,
code: response?.code
}
}
}
///////////////////////////////
// If success -> returns { isSuccess: true, data: object }
// If fail -> returns { isError: true, status: string, msg: string, code: int }
//////////////////////////////
const ProcessError = async (error) => {
if (error.response) {
// Request made and server responded with a status code
// that falls out of the range of 2xx
if (error.response?.status === 403) {
// const { url, config } = error.response;
// console.log(error);
// try {
// let response = await API.getRefreshToken({ token: getRefreshToken() });
// if (response.isSuccess) {
sessionStorage.clear();
// setAccessToken(response.data.accessToken);
// const requestData = error.toJSON();
// let response1 = await axios({
// method: requestData.config.method,
// url: requestData.config.baseURL + requestData.config.url,
// headers: { "content-type": "application/json", "authorization": getAccessToken() },
// params: requestData.config.params
// });
// }
// } catch (error) {
// return Promise.reject(error)
// }
} else {
console.log("ERROR IN RESPONSE: ", error.toJSON());
return {
isError: true,
msg: API_NOTIFICATION_MESSAGES.responseFailure,
code: error.response.status
}
}
} else if (error.request) {
// The request was made but no response was received
console.log("ERROR IN RESPONSE: ", error.toJSON());
return {
isError: true,
msg: API_NOTIFICATION_MESSAGES.requestFailure,
code: ""
}
} else {
// Something happened in setting up the request that triggered an Error
console.log("ERROR IN RESPONSE: ", error.toJSON());
return {
isError: true,
msg: API_NOTIFICATION_MESSAGES.networkError,
code: ""
}
}
}
const API = {};
for (const [key, value] of Object.entries(SERVICE_URLS)) {
API[key] = (body, showUploadProgress, showDownloadProgress) =>
axiosInstance({
method: value.method,
url: value.url,
data: value.method === 'DELETE' ? '' : body,
responseType: value.responseType,
headers: {
authorization: getAccessToken(),
},
TYPE: getType(value, body),
onUploadProgress: function(progressEvent) {
if (showUploadProgress) {
let percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
showUploadProgress(percentCompleted);
}
},
onDownloadProgress: function(progressEvent) {
if (showDownloadProgress) {
let percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
showDownloadProgress(percentCompleted);
}
}
});
}
export { API };