@kode-frontend/timeout-interceptor
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

Timeout-interceptor

Version

ℹ️ Why?

Axios always return ERR_NETWORK code of instead ETIMEDOUT even if you set transitional: { clarifyTimeoutError: true } for android, because RN hasn't consistent behavior for XMLHttpRequest.ontimeout callback. Check it here - https://github.com/facebook/react-native/issues?q=xmlhttprequest+timeout+android

To accurately detect a timeout error, you can using timeout-interceptor

📥 Install

Add @kode-frontend/timeout-interceptor dependency to your project.

# Using npm
npm i @kode-frontend/timeout-interceptor

# Using yarn
yarn add @kode-frontend/timeout-interceptor

# Using pnpm
pnpm add @kode-frontend/timeout-interceptor

🎮 Usage

import {
  startTimeoutInterceptor,
  isTimeoutError,
} from '@kode-frontend/timeout-interceptor'

// Init interceptor
useEffect(() => {
  const clearInterceptor = startTimeoutInterceptor(axiosInstance)

  return () => {
    clearInterceptor()
  }
}, [])

// Usage with request
async function getUser() {
  try {
    return await axios.get('/user?ID=12345')
  } catch (error) {
    if (isTimeoutError(error)) {
      // to do something for timeout
    }
  }
}

TODO

  • [ ] Tests

Package Sidebar

Install

npm i @kode-frontend/timeout-interceptor

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

7.99 kB

Total Files

10

Last publish

Collaborators

  • kode-frontend