diff --git a/src/__tests__/api-client.test.ts b/src/__tests__/api-client.test.ts index f2994f57..4c5c8ea3 100644 --- a/src/__tests__/api-client.test.ts +++ b/src/__tests__/api-client.test.ts @@ -1,4 +1,3 @@ -import { AbortController } from 'node-abort-controller'; import { Base64 } from 'js-base64'; import WazoApiClient from '../api-client'; @@ -6,8 +5,6 @@ import ServerError from '../domain/ServerError'; import BadResponse from '../domain/BadResponse'; import Session from '../domain/Session'; -const controller = new AbortController(); - const mockedResponse = { data: { token: 1, @@ -116,7 +113,7 @@ describe('With correct API results', () => { expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token`, { method: 'post', body: JSON.stringify(data), - signal: controller.signal, + signal: expect.any(Object), headers, agent: null, }); @@ -131,7 +128,7 @@ describe('With correct API results', () => { expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token/${oldToken}`, { method: 'delete', body: null, - signal: controller.signal, + signal: expect.any(Object), headers: {}, agent: null, }); @@ -154,7 +151,7 @@ describe('With unAuthorized API results', () => { expect(global.fetch).toBeCalledWith(`https://${server}/api/auth/${authVersion}/token/${tokenToCheck}`, { method: 'head', body: null, - signal: controller.signal, + signal: expect.any(Object), headers: {}, agent: null, }); @@ -186,7 +183,7 @@ describe('With not found API results', () => { expect(global.fetch).toBeCalledWith(`https://${server}/api/calld/1.0/users/me/voicemails`, { method: 'get', body: null, - signal: controller.signal, + signal: expect.any(Object), headers: { 'X-Auth-Token': token, 'Content-Type': 'application/json', diff --git a/src/utils/__tests__/api-requester.test.ts b/src/utils/__tests__/api-requester.test.ts index 77d730d3..054f4c72 100644 --- a/src/utils/__tests__/api-requester.test.ts +++ b/src/utils/__tests__/api-requester.test.ts @@ -1,5 +1,3 @@ -import { AbortController } from 'node-abort-controller'; - import ApiRequester from '../api-requester'; const server = 'localhost'; @@ -16,7 +14,6 @@ const headers = { 'Content-Type': 'application/json', 'X-Auth-Token': token, }; -const controller = new AbortController(); describe('Generating query string', () => { it('should generate a simple query string from an object', () => { @@ -103,7 +100,7 @@ describe('Calling fetch', () => { expect(global.fetch).toBeCalledWith(url, { method: 'get', body: null, - signal: controller.signal, + signal: expect.any(Object), headers: {}, agent: null, }); @@ -148,14 +145,14 @@ describe('With a refresh token', () => { expect(global.fetch).toHaveBeenNthCalledWith(1, url, { method: 'get', body: null, - signal: controller.signal, + signal: expect.any(Object), headers, agent: null, }); expect(global.fetch).toHaveBeenNthCalledWith(2, url, { method: 'get', body: null, - signal: controller.signal, + signal: expect.any(Object), headers: updatedHeaders, agent: null, }); diff --git a/src/utils/api-requester.ts b/src/utils/api-requester.ts index a8be891d..797c3296 100644 --- a/src/utils/api-requester.ts +++ b/src/utils/api-requester.ts @@ -5,7 +5,7 @@ /* eslint-disable import/no-dynamic-require */ import { Base64 } from 'js-base64'; -import { AbortController } from 'node-abort-controller'; +import { AbortController as NodeAbortController } from 'node-abort-controller'; import BadResponse from '../domain/BadResponse'; import ServerError from '../domain/ServerError'; @@ -171,9 +171,8 @@ export default class ApiRequester { const isHead = method === 'head'; const hasEmptyResponse = method === 'delete' || isHead; const newParse = hasEmptyResponse ? ApiRequester.successResponseParser : parse; - const fetchOptions = { ...(this.fetchOptions || {}), - }; - const controller = new AbortController(); + const fetchOptions = { ...(this.fetchOptions || {}) }; + const controller = typeof AbortController !== 'undefined' ? new AbortController() : new NodeAbortController(); const extraHeaders = fetchOptions.headers || {}; delete fetchOptions.headers; const options = {