Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import axios from "axios";
  2. // import queryString from "qs";
  3. const request = axios.create({
  4. // baseURL: "http://192.168.88.150:3001/", // DJOLE
  5. // baseURL: "http://192.168.88.175:3005/",
  6. // baseURL: "http://192.168.88.143:3001/", // DULE
  7. // baseURL: "https://trampa-api.dilig.net/",
  8. // baseURL: "https://trampa-api-test.dilig.net/",
  9. // baseURL: "http://localhost:3001/",
  10. baseURL: process.env.REACT_APP_BASE_API_URL,
  11. headers: {
  12. "Content-Type": "application/json",
  13. },
  14. // withCredentials: true,
  15. // paramsSerializer: (params) =>
  16. // queryString.stringify(params, { arrayFormat: "comma" }),
  17. });
  18. export const getRequest = (url, params = null, options = null) =>
  19. request.get(url, { params, ...options });
  20. export const postRequest = (url, data, params = null, options = null) =>
  21. request.post(url, data, { params, ...options });
  22. export const putRequest = (url, data, params = null, options = null) =>
  23. request.put(url, data, { params, ...options });
  24. export const patchRequest = (url, data, params = null, options = null) =>
  25. request.patch(url, data, { params, ...options });
  26. export const deleteRequest = (url, params = null, options = null) =>
  27. request.delete(url, { params, ...options });
  28. export const downloadRequest = (url, params = null, options = null) =>
  29. request.get(url, { params, ...options, responseType: "blob" });
  30. export const replaceInUrl = (url, pathVariables = {}) => {
  31. const keys = Object.keys(pathVariables);
  32. if (!keys.length) {
  33. return url;
  34. }
  35. return keys.reduce(
  36. (acc, key) => acc.replace(`{${key}}`, pathVariables[`${key}`]),
  37. url
  38. );
  39. };
  40. export const addHeaderToken = (token) => {
  41. request.defaults.headers.Authorization = `Bearer ${token}`;
  42. };
  43. export const addHeaderCookie = (key, value) => {
  44. request.defaults.headers[`${key}`] = value;
  45. };
  46. export const removeHeaderToken = () => {
  47. delete request.defaults.headers.Authorization;
  48. };
  49. // If you pass function to interceptor of axios, it only adds that function
  50. // to existing array of interceptor functions. That causes that same function
  51. // of interceptors getting called multiple times instead of just one time, as it
  52. // is supposed to do. Thats why there is 'global' axios interceptor array, which indicates
  53. // axios to eject previous interceptor. This approach requires that every middleware has its
  54. // unique name from which it is being recognized. Every object in those arrays contains
  55. // interceptor name and ID of interceptor function.
  56. let axiosInterceptorRequests = [];
  57. let axiosInterceptorResponses = [];
  58. export const attachPostRequestListener = (
  59. postRequestListener,
  60. interceptorName
  61. ) => {
  62. let previousAxiosInterceptor = axiosInterceptorResponses.find(
  63. (item) => item.name === interceptorName
  64. );
  65. let previousAxiosInterceptorResponses = axiosInterceptorResponses;
  66. if (previousAxiosInterceptor !== undefined) {
  67. request.interceptors.response.eject(previousAxiosInterceptor.interceptorID);
  68. previousAxiosInterceptorResponses = axiosInterceptorResponses.filter(
  69. (item) => item.interceptorID !== previousAxiosInterceptor.interceptorID
  70. );
  71. }
  72. let axiosInterceptorID = request.interceptors.response.use(
  73. (response) => response,
  74. (response) => postRequestListener(response)
  75. );
  76. previousAxiosInterceptorResponses.push({
  77. name: interceptorName,
  78. interceptorID: axiosInterceptorID,
  79. });
  80. axiosInterceptorResponses = [...previousAxiosInterceptorResponses];
  81. };
  82. export const attachBeforeRequestListener = (
  83. beforeRequestListener,
  84. interceptorName
  85. ) => {
  86. let previousAxiosInterceptor = axiosInterceptorRequests.find(
  87. (item) => item.name === interceptorName
  88. );
  89. let previousAxiosInterceptorRequests = axiosInterceptorRequests;
  90. if (previousAxiosInterceptor !== undefined) {
  91. request.interceptors.request.eject(previousAxiosInterceptor.interceptorID);
  92. previousAxiosInterceptorRequests = axiosInterceptorRequests.filter(
  93. (item) => item.interceptorID !== previousAxiosInterceptor.interceptorID
  94. );
  95. }
  96. let axiosInterceptorID = request.interceptors.request.use(
  97. (response) => beforeRequestListener(response),
  98. (response) => response
  99. );
  100. previousAxiosInterceptorRequests.push({
  101. name: interceptorName,
  102. interceptorID: axiosInterceptorID,
  103. });
  104. axiosInterceptorRequests = [...previousAxiosInterceptorRequests];
  105. };
  106. export const apiDefaultUrl = request.defaults.baseURL;