request.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import axios from 'axios';
  2. import { API_URL, APP_VERSION } from '../constants';
  3. import { Platform } from 'react-native';
  4. import { showBanner } from './bannerUtils';
  5. import { useErrorStore } from 'src/stores/errorStore';
  6. export const request = axios.create({
  7. baseURL: API_URL,
  8. timeout: 10000
  9. });
  10. export const setupInterceptors = ({
  11. showError
  12. }: {
  13. showError: (
  14. message: string,
  15. loginNeeded: boolean,
  16. authNeeded: boolean,
  17. premiumNeeded: boolean
  18. ) => void;
  19. }) => {
  20. request.interceptors.request.use(
  21. (config) => {
  22. config.headers['App-Version'] = APP_VERSION;
  23. config.headers['Platform'] = Platform.OS;
  24. if (config.data instanceof FormData) {
  25. config.timeout = 0;
  26. }
  27. return config;
  28. },
  29. (error) => {
  30. return Promise.reject(error);
  31. }
  32. );
  33. request.interceptors.response.use(
  34. (response) => {
  35. const { isErrorShown, setErrorShown } = useErrorStore.getState();
  36. if (response.data.result === 'ERROR' && response.data.result_description) {
  37. const showErrorWithDelay = (
  38. message: string,
  39. requiresLogin: boolean,
  40. requiresAuth: boolean,
  41. requiresPremium: boolean = false
  42. ) => {
  43. if (!isErrorShown) {
  44. setErrorShown(true);
  45. setTimeout(() => {
  46. showError(message, requiresLogin, requiresAuth, requiresPremium);
  47. }, 1000);
  48. }
  49. };
  50. if (response.data?.login_needed && response.data.login_needed === 1) {
  51. showErrorWithDelay(response.data.result_description, true, false);
  52. return response;
  53. } else if (
  54. response.data.result_description ===
  55. 'Only authenticated users are allowed to send messages.'
  56. ) {
  57. showErrorWithDelay(response.data.result_description, false, true);
  58. return response;
  59. } else if (response.data?.premium_needed && response.data.premium_needed === 1) {
  60. showErrorWithDelay(response.data.result_description, false, false, true);
  61. return response;
  62. }
  63. showErrorWithDelay(response.data.result_description, false, false);
  64. }
  65. return response;
  66. },
  67. (error) => {
  68. const { isErrorShown, setErrorShown } = useErrorStore.getState();
  69. if (error.code === 'ECONNABORTED') {
  70. error.isTimeout = true;
  71. showBanner('Slow internet connection!');
  72. return Promise.reject(error);
  73. } else if (error.message === 'Network Error') {
  74. return Promise.reject(error);
  75. }
  76. const shortUrl = error.config.url
  77. ? error.config.url?.split('/')?.filter(Boolean)?.pop()
  78. : 'Unknown URL';
  79. if (!isErrorShown) {
  80. setErrorShown(true);
  81. showError(`${error.message} (${shortUrl})`, false, false, false);
  82. }
  83. return Promise.reject(error);
  84. }
  85. );
  86. };