2.3.0 • Published 1 year ago

@nuskin/axios-util v2.3.0

Weekly downloads
949
License
MIT
Repository
-
Last release
1 year ago

@nuskin/axios-util

axios-util enables you to utilize Axios as you normally would. By default, axios-util sets the timeout to 10000ms.

Retry functionality is implemented using the axios-retry module. Utilize getRetryAxiosInstance() to obtain an Axios instance with retry capabilities. All default settings of axios-retry are applied, except for the retryDelay, which is set to axiosRetry.exponentialDelay. You can customize the retry behavior by supplying an axios-retry options object to getRetryAxiosInstance().

Installing

Using npm:

npm install @nuskin/axios-util

Using yarn:

yarn add @nuskin/axios-util

Example usage

For ESM syntax, use import:

import { axios, getRetryAxiosInstance } from '@nuskin/axios-util'

For CommonJS, use require:

const { axios, getRetryAxiosInstance } = require('@nuskin/axios-util')

Customizing Retry

To customize the retry logic, access axios-retry with ESM import syntax:

let retryDelay = process.env.AXIOS_RETRY_DELAY || 5000
const myAxiosWithRetry = getRetryAxiosInstance({
    retries: 3,
    shouldResetTimeout: true,
    retryDelay: (retryCount) => {
        return retryCount * retryDelay
    },
    retryCondition: (error) => {
        // If retry condition is not specified, by default, idempotent requests are retried
        return error.response?.status >= 500 || error.code === 'ECONNABORTED'
    },
    onRetry: (retryCount, error, requestConfig) => {
        console.log(
            {
                retryCount,
                message: error.message,
                statusCode: error.response?.status,
                statusText: error.response?.statusText,
                responseData: error.response?.data
            },
            requestConfig.metric
        )
        return true;
    }
})

Other Possibilities for axios-util

License

MIT