您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import axios from "axios";
  2. import { setConnectionError } from "../store/actions";
  3. import store from "../store/index";
  4. import { API_ENDPOINT } from './endpointDef';
  5. import { refreshTokens } from "./tokenService/tokenApiClient";
  6. const axiosApiInstance = axios.create();
  7. const globalLog = false;
  8. const defaultOptions = { log: false }
  9. export const Get = (url, options) => {
  10. return request("GET", url, { ...defaultOptions, ...options });
  11. }
  12. export const Post = (url, data, options) => {
  13. return request("POST", url, { ...defaultOptions, ...options, data });
  14. }
  15. export const Put = (url, data, options) => {
  16. return request("PUT", url, { ...defaultOptions, ...options, data });
  17. }
  18. export const Delete = (url, options) => {
  19. return request("DELETE", url, { ...defaultOptions, ...options });
  20. }
  21. const isLogging = (log) => {
  22. return (globalLog || log);
  23. }
  24. const request = (method, url, options) => {
  25. const { data, log } = options;
  26. if (isLogging(log)) console.log("REQUEST URL : ", url);
  27. const requestObject = {
  28. method,
  29. url: API_ENDPOINT + url,
  30. timeout: 60000
  31. }
  32. if (data) {
  33. if (isLogging(log)) console.log(`DATA FOR : ${url}`, data);
  34. requestObject.data = data;
  35. }
  36. else {
  37. requestObject.data = undefined;
  38. }
  39. // console.log("url", url)
  40. return axiosApiInstance(requestObject).then((response) => {
  41. if (isLogging(log)) console.log(`RESPONSE for ${url} : `, response);
  42. return { ...response, OK: true };
  43. })
  44. .catch((error) => {
  45. if (isLogging(log)) console.log(`RESPONSE for catch ${url} : `, (error.response ? error.response : error));
  46. //if we get a request timeout error
  47. if (!error.response) {
  48. store.dispatch(setConnectionError(error));
  49. return { error, OK: false };
  50. }
  51. //if we get a response other than OK
  52. if (error.response) {
  53. return { ...error.response, OK: false };
  54. }
  55. return { OK: false };
  56. })
  57. }
  58. axiosApiInstance.interceptors.request.use(
  59. (config) => {
  60. // console.log('sending request')
  61. const accessToken = store.getState().user.token;
  62. config.headers = {
  63. "Authorization": `Bearer ${accessToken}`,
  64. "Accept": "application/json",
  65. "Content-Type": "application/json",
  66. "Referer": config.url
  67. };
  68. return config;
  69. },
  70. (error) => {
  71. Promise.reject(error);
  72. }
  73. );
  74. axiosApiInstance.interceptors.response.use(
  75. (response) => {
  76. return response;
  77. },
  78. async (error) => {
  79. // console.log('********************************************************************', error)
  80. const originalRequest = error.config
  81. if (originalRequest._retried) {
  82. console.log("Request second attempt failed.")
  83. }
  84. if (!originalRequest._retried && error.response && error.response.status === 401) {
  85. try {
  86. await refreshTokens()
  87. }
  88. catch (e) {
  89. throw error;
  90. }
  91. return await axiosApiInstance({ ...originalRequest, _retried: true })
  92. }
  93. throw error;
  94. }
  95. );
  96. export default axiosApiInstance;