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

Commit 903194b

Browse files
authored
Merge pull request #271 from North-Two-Five/using-axios-instance
Using axios instance
2 parents 47ce7ae + c7d3078 commit 903194b

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ class Analytics {
5353
enumerable: true,
5454
value: typeof options.enable === 'boolean' ? options.enable : true
5555
})
56-
this.axiosClient = axios.create()
57-
axiosRetry(this.axiosClient, {
56+
axiosRetry(this.axiosInstance, {
5857
retries: options.retryCount || 3,
5958
retryCondition: this._isErrorRetryable,
6059
retryDelay: axiosRetry.exponentialDelay
@@ -266,20 +265,17 @@ class Analytics {
266265
}
267266

268267
const req = {
269-
method: 'POST',
270-
url: `${this.host}${this.path}`,
271268
auth: {
272269
username: this.writeKey
273270
},
274-
data,
275271
headers
276272
}
277273

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

282-
this.axiosClient(req)
278+
this.axiosInstance.post(`${this.host}${this.path}`, data, req)
283279
.then(() => done())
284280
.catch(err => {
285281
if (err.response) {

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,30 @@ test('ensure that failed requests are not retried forever', async t => {
606606
await t.throws(client.flush())
607607
})
608608

609+
test('ensure we can pass our own axios instance', async t => {
610+
const axios = require('axios')
611+
const myAxiosInstance = axios.create()
612+
const stubAxiosPost = stub(myAxiosInstance, 'post').resolves()
613+
const client = createClient({
614+
axiosInstance: myAxiosInstance,
615+
host: 'https://my-dummy-host.com',
616+
path: '/test/path'
617+
})
618+
619+
const callback = spy()
620+
client.queue = [
621+
{
622+
message: 'something',
623+
callback
624+
}
625+
]
626+
627+
client.flush()
628+
629+
t.true(stubAxiosPost.called)
630+
t.true(stubAxiosPost.alwaysCalledWith('https://my-dummy-host.com/test/path'))
631+
})
632+
609633
test('ensure other axios clients are not impacted by axios-retry', async t => {
610634
let client = createClient() // eslint-disable-line
611635
const axios = require('axios')

0 commit comments

Comments
 (0)