nuxt-community / axios-module

Secure and easy axios integration for Nuxt 2
https://axios.nuxtjs.org
MIT License
1.19k stars 245 forks source link

Axios-Retry Configuration Compilation Error #467

Open axieum opened 3 years ago

axieum commented 3 years ago

Version

module: @nuxtjs/auth-next@5.0.0-1611071776.3646657 nuxt: nuxt@2.14.12

Nuxt configuration

mode:

Nuxt configuration

Please reference commit to see all relevant configuration - https://github.com/Axieum/telescope-for-spotify/commit/1944ece1649598d50fb90a3a20f1375d120bcaa4

Reproduction

What is expected?

For requests to be retried without error.

What is actually happening?

The import of axiosRetry is being compiled to axios_retry_1.default, which is not defined.

image image

Steps to reproduce

Add a custom retry configuration, like below:

import { AxiosError } from 'axios';
import axiosRetry, { IAxiosRetryConfig } from 'axios-retry';

// Axios module configuration (https://go.nuxtjs.dev/config-axios)
axios: {
  // https://axios.nuxtjs.org/options#retry
  retry: {
    // Attempts to retry N times
    retries: 3,
    // Extends the retry condition
    retryCondition(error: AxiosError): boolean {
      return !error.response
        || axiosRetry.isNetworkOrIdempotentRequestError(error)
        || error.response.status === 429; // 429 Too Many Requests
    },
    // Extends the retry delay based on the Retry-After header
    retryDelay(retryCount: number, error: AxiosError): number {
      const retryAfter: number = error.response?.headers['retry-after'];
      if (retryAfter) return retryAfter < 30 ? retryAfter * 1000 : -1;
      return axiosRetry.exponentialDelay(retryCount);
    },
  } as IAxiosRetryConfig,
};

In my case, I import this from config/axios.ts and then add it as:

import axios from './config/axios.ts';
// ...
axios,
// ...

Additional information

Checklist

farnabaz commented 3 years ago

The module does not support custom retry as you can see it is not documented in docs.

Try to require axios-retry inside retry function, it might work, otherwise it should be impossible.

axieum commented 3 years ago

Sorry for the delay in reply. Thanks for your suggestion, it works. const axiosRetry = require('axios-retry'); in each of the functions and removing the import at the top.

This led me to realising that the @nuxtjs/axios module is simply placing the custom retry into its own, and therefore there is no need for me to import/redefine axiosRetry.

This works, we just have to suppress TypeScript and ESLint errors:

import { AxiosError } from 'axios';
-import axiosRetry, { IAxiosRetryConfig } from 'axios-retry';
+import { IAxiosRetryConfig } from 'axios-retry';

// Axios module configuration (https://go.nuxtjs.dev/config-axios)
export default {
  // https://axios.nuxtjs.org/options#retry
  retry: {
    // Attempts to retry N times
    retries: 3,
    // Extends the retry condition
    retryCondition(error: AxiosError): boolean {
      return !error.response
-       || axiosRetry.isNetworkOrIdempotentRequestError(error)
+       // @ts-ignore axios-module#467 (https://git.io/J3nCc)
+       || axiosRetry.isNetworkOrIdempotentRequestError(error) // eslint-disable-line no-undef
        || error.response.status === 429; // 429 Too Many Requests
    },
    // Extends the retry delay based on the Retry-After header
    retryDelay(retryCount: number, error: AxiosError): number {
      const retryAfter: number = error.response?.headers['retry-after'];
      if (retryAfter) return retryAfter < 30 ? retryAfter * 1000 : -1;
-     return axiosRetry.exponentialDelay(retryCount);
+     // @ts-ignore axios-module#467 (https://git.io/J3nCc)
+     return axiosRetry.exponentialDelay(retryCount); // eslint-disable-line no-undef
    },
  } as IAxiosRetryConfig,
};

Hope this helps any passersby in configuring softonic/axios-retry.