Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #271 from North-Two-Five/using-axios-instance
Browse files Browse the repository at this point in the history
Using axios instance
  • Loading branch information
pooyaj committed Feb 25, 2021
2 parents 47ce7ae + c7d3078 commit 903194b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
8 changes: 2 additions & 6 deletions index.js
Expand Up @@ -53,8 +53,7 @@ class Analytics {
enumerable: true,
value: typeof options.enable === 'boolean' ? options.enable : true
})
this.axiosClient = axios.create()
axiosRetry(this.axiosClient, {
axiosRetry(this.axiosInstance, {
retries: options.retryCount || 3,
retryCondition: this._isErrorRetryable,
retryDelay: axiosRetry.exponentialDelay
Expand Down Expand Up @@ -266,20 +265,17 @@ class Analytics {
}

const req = {
method: 'POST',
url: `${this.host}${this.path}`,
auth: {
username: this.writeKey
},
data,
headers
}

if (this.timeout) {
req.timeout = typeof this.timeout === 'string' ? ms(this.timeout) : this.timeout
}

this.axiosClient(req)
this.axiosInstance.post(`${this.host}${this.path}`, data, req)
.then(() => done())
.catch(err => {
if (err.response) {
Expand Down
24 changes: 24 additions & 0 deletions test.js
Expand Up @@ -606,6 +606,30 @@ test('ensure that failed requests are not retried forever', async t => {
await t.throws(client.flush())
})

test('ensure we can pass our own axios instance', async t => {
const axios = require('axios')
const myAxiosInstance = axios.create()
const stubAxiosPost = stub(myAxiosInstance, 'post').resolves()
const client = createClient({
axiosInstance: myAxiosInstance,
host: 'https://my-dummy-host.com',
path: '/test/path'
})

const callback = spy()
client.queue = [
{
message: 'something',
callback
}
]

client.flush()

t.true(stubAxiosPost.called)
t.true(stubAxiosPost.alwaysCalledWith('https://my-dummy-host.com/test/path'))
})

test('ensure other axios clients are not impacted by axios-retry', async t => {
let client = createClient() // eslint-disable-line
const axios = require('axios')
Expand Down

0 comments on commit 903194b

Please sign in to comment.