Advertisement
crutch12

Untitled

Feb 4th, 2021
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // plugins/nuxt/axios.ts
  2.  
  3. import type { AxiosInstance, AxiosError } from 'axios';
  4. import type { Context } from '@nuxt/types';
  5. // @ts-ignore
  6. import isAbsoluteURL from 'axios/lib/helpers/isAbsoluteURL';
  7. import { axios, axiosStatic, withCredentials } from '@monq/core/src/lib/axios';
  8.  
  9. // source: https://github.com/nuxt-community/axios-module/blob/85840c6b087c1b250e9d930f3db44c5fd5f41d59/lib/plugin.js#L116
  10. const setupNuxtProgress = (axiosInstance: AxiosInstance) => {
  11.   if (typeof window === 'undefined') {
  12.     return;
  13.   }
  14.  
  15.   // A noop loading inteterface for when $nuxt is not yet ready
  16.   const noopLoading = {
  17.     finish: () => { },
  18.     start: () => { },
  19.     fail: () => { },
  20.     set: () => { },
  21.   };
  22.  
  23.   const $loading = () => {
  24.     const $nuxt = typeof window !== 'undefined' && window.$nuxt;
  25.     return ($nuxt && $nuxt.$loading && $nuxt.$loading.hasOwnProperty('set')) ? $nuxt.$loading : noopLoading;
  26.   };
  27.  
  28.   let currentRequests = 0;
  29.  
  30.   axiosInstance.interceptors.request.use((config) => {
  31.     currentRequests++;
  32.     return config;
  33.   });
  34.  
  35.   axiosInstance.interceptors.response.use((response) => {
  36.     currentRequests--;
  37.  
  38.     if (currentRequests <= 0) {
  39.       currentRequests = 0;
  40.       $loading().finish();
  41.     }
  42.  
  43.     return response;
  44.   });
  45.  
  46.   const onError = (error: AxiosError) => {
  47.     currentRequests--;
  48.  
  49.     if (axiosStatic.isCancel(error)) {
  50.       if (currentRequests <= 0) {
  51.         currentRequests = 0;
  52.         $loading().finish();
  53.       }
  54.       return Promise.reject(error);
  55.     }
  56.  
  57.     // @ts-ignore
  58.     $loading().fail();
  59.     $loading().finish();
  60.  
  61.     return Promise.reject(error);
  62.   };
  63.  
  64.   axiosInstance.interceptors.request.use(undefined, onError);
  65.   axiosInstance.interceptors.response.use(undefined, onError);
  66.  
  67.   const onProgress = (e: { loaded: number, total: number }) => {
  68.     if (!currentRequests) {
  69.       return;
  70.     }
  71.     const progress = ((e.loaded * 100) / (e.total * currentRequests));
  72.     // @ts-ignore
  73.     $loading().set(Math.min(100, progress));
  74.   };
  75.  
  76.   axiosInstance.defaults.onUploadProgress = onProgress;
  77.   axiosInstance.defaults.onDownloadProgress = onProgress;
  78. };
  79.  
  80. export default function({ $config }: Context) {
  81.   // apply interceptors on response
  82.   axios.interceptors.request.use(withCredentials);
  83.  
  84.   setupNuxtProgress(axios);
  85.  
  86.   const defaults = axios.defaults;
  87.   defaults.baseURL = $config.BASE_API_URL;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement