0.0.4 • Published 1 year ago

axios-multi-host v0.0.4

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

Axios Multi Host

Make axios use multiple host as backup if some host is down

Installation

Install with npm or yarn

  npm install axios axios-multi-host
  yarn add axios axios-multi-host

Usage/Examples

import axios from 'axios'
import { AxiosMultiHost } from 'axios-multi-host'

const host = new AxiosMultiHost(['https://google.com', 'https://bing.com'])
host.repetitiveCheck() // optional but recommended

const api = axios.create()
api.interceptors.request.use(host.middleware, host.error)

api.get('/search?q=tes')

note: host.repetitiveCheck() when invoked will repetitively check every host and choose the first one from the list that is up, you can pass configuration of this function in the example below.

Configuration

configuration on repetitiveCheck

const list = ['https://google.com', 'https://bing.com']
const config = {
  method: 'head',
  timeout: 30000,
}

new AxiosMultiHost(list, config)
ParameterTypeDefaultDescription
methodstringheadoptional. method to check the url
intervalnumber30000optional. time interval of repetitiveCheck in miliseconds

also you can change the interval check on run by invoke repetitiveCheck and passing the new timeout

host.repetitiveCheck(60000)

Host list configuration

plain string array

const list = ['https://google.com', 'https://bing.com']

from another endpoint/url

const list = [
  {
    host: 'https://google.com',
    check: 'https://google.com/status',
  },
]

Catch all host down

host.onAllDown(() => {
  console.log('all host down')
})

Update List

host.updateList(['https://google.com', 'https://bing.com'])

Get current list

host.getList()
0.0.4

1 year ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago