diff --git a/flow-typed/box-ui-elements.js b/flow-typed/box-ui-elements.js index c47e467e07..d4b08a5571 100644 --- a/flow-typed/box-ui-elements.js +++ b/flow-typed/box-ui-elements.js @@ -6,7 +6,7 @@ /* eslint-disable no-unused-vars */ // NOTE: all of these imports resolve to `any` // see https://github.com/facebook/flow/issues/7574 -import type { $AxiosXHR, $AxiosError, Axios, CancelTokenSource } from 'axios'; +import type { $AxiosXHR, $AxiosError, Axios, AbortController } from 'axios'; import type FolderAPI from '../src/api/Folder'; import type FileAPI from '../src/api/File'; import type WebLinkAPI from '../src/api/WebLink'; diff --git a/src/utils/Xhr.js b/src/utils/Xhr.js index 586eda1570..c167b44f0d 100644 --- a/src/utils/Xhr.js +++ b/src/utils/Xhr.js @@ -36,7 +36,7 @@ class Xhr { axios: Axios; - axiosSource: CancelTokenSource; + axiosAbortController: AbortController; clientName: ?string; @@ -107,7 +107,7 @@ class Xhr { this.version = version; this.axios = axios.create(); - this.axiosSource = axios.CancelToken.source(); + this.axiosAbortController = new AbortController(); this.axios.interceptors.response.use(this.responseInterceptor, this.errorInterceptor); if (typeof requestInterceptor === 'function') { @@ -271,7 +271,7 @@ class Xhr { }): Promise<StringAnyMap> { return this.getHeaders(id, headers).then(hdrs => this.axios.get(url, { - cancelToken: this.axiosSource.token, + signal: this.axiosAbortController.signal, params, headers: hdrs, parsedUrl: this.getParsedUrl(url), @@ -487,7 +487,7 @@ class Xhr { method, headers: hdrs, onUploadProgress: progressHandlerToUse, - cancelToken: this.axiosSource.token, + signal: this.axiosAbortController.signal, }) .then(response => { clearTimeout(idleTimeout); @@ -506,13 +506,13 @@ class Xhr { * * @return {void} */ - abort(): void { + abort(reason: string): void { if (this.retryTimeout) { clearTimeout(this.retryTimeout); } - if (this.axiosSource) { - this.axiosSource.cancel(); - this.axiosSource = axios.CancelToken.source(); + + if (this.axiosAbortController) { + this.axiosAbortController.abort(reason); } } } diff --git a/src/utils/__tests__/Xhr.test.js b/src/utils/__tests__/Xhr.test.js index f44a12b8ce..47a2e106d8 100644 --- a/src/utils/__tests__/Xhr.test.js +++ b/src/utils/__tests__/Xhr.test.js @@ -30,7 +30,7 @@ describe('util/Xhr', () => { }) .then(() => { expect(xhrInstance.axios.get).toHaveBeenCalledWith('url', { - cancelToken: xhrInstance.axiosSource.token, + signal: xhrInstance.axiosAbortController.signal, params: {}, headers: {}, parsedUrl: url, @@ -283,14 +283,15 @@ describe('util/Xhr', () => { describe('abort()', () => { test('should cancel axios request', () => { - const mockSource = { - cancel: jest.fn(), + const mockAbortController = { + signal: jest.fn(), + abort: jest.fn(), }; - xhrInstance.axiosSource = mockSource; + xhrInstance.axiosAbortController = mockAbortController; xhrInstance.abort(); - expect(mockSource.cancel).toHaveBeenCalled(); + expect(mockAbortController.abort).toHaveBeenCalled(); }); });