Skip to content

Commit

Permalink
fix(APICore): let AbortError be thrown
Browse files Browse the repository at this point in the history
BREAKING CHANGE: AbortError will now throw instead of resolving with undefined.
Deprecated getFile method was removed from APICore, use get with responseBodyFormat blob instead.
  • Loading branch information
gdostie committed Jan 31, 2024
1 parent 478d2d7 commit a3f8efd
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 60 deletions.
30 changes: 1 addition & 29 deletions src/APICore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,7 @@ export default class API {
}

async get<T = Record<string, unknown>>(url: string, args?: CoveoPlatformClientRequestInit): Promise<T> {
try {
return await this.request<T>(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<Blob> {
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<T>(url, this.buildRequestInit('GET', args), args?.responseBodyFormat);
}

async post<T = Record<string, unknown>>(
Expand Down
31 changes: 0 additions & 31 deletions src/tests/APICore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit a3f8efd

Please sign in to comment.