diff --git a/index.js b/index.js index bb4483bc..add8ac70 100644 --- a/index.js +++ b/index.js @@ -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 @@ -266,12 +265,9 @@ class Analytics { } const req = { - method: 'POST', - url: `${this.host}${this.path}`, auth: { username: this.writeKey }, - data, headers } @@ -279,7 +275,7 @@ class Analytics { 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) { diff --git a/test.js b/test.js index a56deb38..2cde2667 100644 --- a/test.js +++ b/test.js @@ -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')