| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import axios from "axios";
- import { setConnectionError } from "../store/actions";
- import store from "../store/store";
- import { API_ENDPOINT } from './endpointDef';
- import { refreshTokens } from "./tokenService/tokenApiClient";
-
- const axiosApiInstance = axios.create();
- const globalLog = false;
-
- const defaultOptions = { log: false }
-
- export const Get = (url, options) => {
- return request("GET", url, { ...defaultOptions, ...options });
- }
-
- export const Post = (url, data, options) => {
- return request("POST", url, { ...defaultOptions, ...options, data });
- }
-
- export const Put = (url, data, options) => {
- return request("PUT", url, { ...defaultOptions, ...options, data });
- }
-
- export const Delete = (url, options) => {
- return request("DELETE", url, { ...defaultOptions, ...options });
- }
-
- const isLogging = (log) => {
- return (globalLog || log);
- }
-
- const request = (method, url, options) => {
- const { data, log } = options;
- if (isLogging(log)) console.log("REQUEST URL : ", url);
- const requestObject = {
- method,
- url: API_ENDPOINT + url,
- timeout: 60000
- }
- if (data) {
- if (isLogging(log)) console.log(`DATA FOR : ${url}`, data);
- requestObject.data = data;
- }
- else {
- requestObject.data = undefined;
- }
- // console.log("url", url)
- return axiosApiInstance(requestObject).then((response) => {
- if (isLogging(log)) console.log(`RESPONSE for ${url} : `, response);
-
- return { ...response, OK: true };
- })
- .catch((error) => {
- if (isLogging(log)) console.log(`RESPONSE for catch ${url} : `, (error.response ? error.response : error));
-
- //if we get a request timeout error
- if (!error.response) {
- store.dispatch(setConnectionError(error));
- return { error, OK: false };
- }
-
- //if we get a response other than OK
- if (error.response) {
- return { ...error.response, OK: false };
- }
-
- return { OK: false };
- })
- }
-
- axiosApiInstance.interceptors.request.use(
- (config) => {
- // console.log('sending request')
- const accessToken = store.getState().user.token;
- config.headers = {
- "Authorization": `Bearer ${accessToken}`,
- "Accept": "application/json",
- "Content-Type": "application/json",
- "Referer": config.url
- };
- return config;
- },
- (error) => {
- Promise.reject(error);
- }
- );
-
- axiosApiInstance.interceptors.response.use(
- (response) => {
- return response;
- },
- async (error) => {
- // console.log('********************************************************************', error)
- const originalRequest = error.config
- if (originalRequest._retried) {
- console.log("Request second attempt failed.")
- }
- if (!originalRequest._retried && error.response && error.response.status === 401) {
- try {
- await refreshTokens()
- }
- catch (e) {
- throw error;
- }
- return await axiosApiInstance({ ...originalRequest, _retried: true })
- }
- throw error;
- }
- );
-
- export default axiosApiInstance;
|