From 84d646f6b76fe5b550ae11e2b3adfc2304705e77 Mon Sep 17 00:00:00 2001 From: Patrick Bassut Date: Sun, 21 Feb 2021 15:43:29 -0300 Subject: [PATCH 1/3] using axios instance --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index 3ad916cc..ac8b1f72 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 From 0e19078d9d63a26c4fe5e1343fbbd312e2db2e9d Mon Sep 17 00:00:00 2001 From: Patrick Bassut Date: Sun, 21 Feb 2021 15:43:42 -0300 Subject: [PATCH 2/3] calling post method as its more testable --- index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/index.js b/index.js index ac8b1f72..14de03d5 100644 --- a/index.js +++ b/index.js @@ -273,12 +273,9 @@ class Analytics { } const req = { - method: 'POST', - url: `${this.host}${this.path}`, auth: { username: this.writeKey }, - data, headers } @@ -286,7 +283,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) { From c0af20a2e948b7c8889e45caaa0bc4e52b54a157 Mon Sep 17 00:00:00 2001 From: Patrick Bassut Date: Sun, 21 Feb 2021 15:43:56 -0300 Subject: [PATCH 3/3] testing if we can use the custom axios instance --- test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test.js b/test.js index cfbbdafb..c5ea996b 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')