Code Monkey home page Code Monkey logo

axios-status's Introduction

Network requests status tracking for Axios

This package can be used to trace status of all Axios requests for the purpose of application-wide progress indicator

Installation:

npm install axios-status --save

Usage:

First, create a new instance of AxiosStatus, and register your axios instances with it. You can provide following options to constructor

const axiosStatus = new AxiosStatus({
  timeout: 20, // default 10
  autoRetry: true // default false
})

axiosStatus.register(axios)
axiosStatus.register(mySpecialAxiosInstance)

The autoRetry options specifies if axiosStatus should auto retry all failed requests after given timeout (seconds)

Then, you can subscribe on AxiosStatus events to be able to show them in UI.

  • The busy event has values of true / false and signals that there are XHR requests in progress
  • The offline event has values of true / false and signals that request failed because network is temporariy down
  • The timer event emits a number that signals seconds to next auto-retry
axiosStatus.on('busy', (val) => this.loadingProgress = val)
axiosStatus.on('offline', (val) => this.disconnected = val)
axiosStatus.on('timer', (val) => this.secondsToReconnect = val)

To be able to correctly run a callback on auto-retry, you need to use axiosStatus.request. It supports all axios request options, plus success callback, error callback, and instance to specify axios instance on which this request should be run.

axiosStatus.request({
  instance: mySpecialAxiosInstance,	
  method: 'get',
  url: API_URL + '/me',
  success: (res) => {
    this.user = res.data
  },
  error: (err) => {
  	throw err
  }
})

To retry manually, you can just call axiosStatus.retry() to retry all calls initiated when offline

axios-status's People

Contributors

tomaash avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.