@kode-frontend/timeout-interceptor
v0.0.1
Published
timeout interceptor for handling jwt token update flow
Downloads
80
Maintainers
Readme
Timeout-interceptor
ℹ️ 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