From a3f8efdf53de50d7ad16e68dbb8b7ff69a1bd447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Dostie?= <35579930+gdostie@users.noreply.github.com> Date: Wed, 31 Jan 2024 12:03:56 -0500 Subject: [PATCH] fix(APICore): let AbortError be thrown BREAKING CHANGE: AbortError will now throw instead of resolving with undefined. Deprecated getFile method was removed from APICore, use get with responseBodyFormat blob instead. --- src/APICore.ts | 30 +----------------------------- src/tests/APICore.spec.ts | 31 ------------------------------- 2 files changed, 1 insertion(+), 60 deletions(-) diff --git a/src/APICore.ts b/src/APICore.ts index 0eae719e2..cc3b950e2 100644 --- a/src/APICore.ts +++ b/src/APICore.ts @@ -64,35 +64,7 @@ export default class API { } async get>(url: string, args?: CoveoPlatformClientRequestInit): Promise { - try { - return await this.request(url, this.buildRequestInit('GET', args), args?.responseBodyFormat); - } catch (error) { - // Warning: the logic below does not do what the original author intended/mentions. - // It will fullfil the promise with `undefined`, instead of keeping it pending. - if (error.name === 'AbortError') { - return undefined as T; // We don't want to resolve or reject the promise - } else { - throw error; - } - } - } - - /** - * @deprecated Use `get` with `{responseBodyFormat: 'blob'}` argument instead. Will be removed in version 45 - */ - async getFile(url: string, args?: CoveoPlatformClientRequestInit): Promise { - try { - return await this.request(url, this.buildRequestInit('GET', args), 'blob'); - } catch (error) { - // Warning: the logic below does not do what the original author intended/mentions. - // It will fullfil the promise with `undefined`, instead of keeping it pending. - if (error.name === 'AbortError') { - // We don't want to resolve or reject the promise - return undefined as unknown as Blob; - } else { - throw error; - } - } + return await this.request(url, this.buildRequestInit('GET', args), args?.responseBodyFormat); } async post>( diff --git a/src/tests/APICore.spec.ts b/src/tests/APICore.spec.ts index 610f388ce..1cf770cab 100644 --- a/src/tests/APICore.spec.ts +++ b/src/tests/APICore.spec.ts @@ -188,37 +188,6 @@ describe('APICore', () => { }); }); - /** - * @deprecated Will be removed in version 45 - */ - describe('getFile', () => { - it('should do a GET request to the specified url and resolve with a blob', async () => { - fetchMock.mockResponseOnce(JSON.stringify(testData.response)); - const expectedResponse = await new Response(JSON.stringify(testData.response)).blob(); - const response = await api.getFile(testData.route); - - expect(fetchMock).toHaveBeenCalledTimes(1); - const [url, options] = fetchMock.mock.calls[0]; - - expect(url).toBe(`${testConfig.host}${testData.route}`); - expect(options?.method).toBe('GET'); - expect(response).toEqual(expectedResponse); - }); - - it('should make the promise fail on a failed request', async () => { - const error = new Error('the request has failed'); - fetchMock.mockRejectedValue(error); - await expect(api.getFile(testData.route)).rejects.toThrow(error); - }); - - it('should bind GET requests to an abort signal', async () => { - fetchMock.mockResponseOnce(JSON.stringify(testData.response)); - await api.getFile(testData.route); - const init = fetchMock.mock.calls[0][1]; - expect(init?.signal).toBeInstanceOf(AbortSignal); - }); - }); - describe('post', () => { it('should do a simple POST request', async () => { fetchMock.mockResponseOnce(JSON.stringify(testData.response));