diff --git a/src/app/backend/fetch-capi.spec.ts b/src/app/backend/fetch-capi.spec.ts index a8dade0a9..08b07112a 100644 --- a/src/app/backend/fetch-capi.spec.ts +++ b/src/app/backend/fetch-capi.spec.ts @@ -86,16 +86,9 @@ describe('fetch capi', () => { } }); - test('should retry json reject', async () => { + test('should catch json reject', async () => { const errorMsg = 'Read json error'; - const expected = { - someField: 'someValue', - }; - const mockFetchJson = jest - .fn() - .mockRejectedValueOnce(errorMsg) - .mockRejectedValueOnce(errorMsg) - .mockResolvedValueOnce(expected); + const mockFetchJson = jest.fn().mockRejectedValueOnce(errorMsg); const mockFetch = jest.fn().mockResolvedValue({ status: 200, ok: true, @@ -106,69 +99,10 @@ describe('fetch capi', () => { const endpoint = 'https://api.test.com/endpoint'; const accessToken = 'testToken'; - const retryDelay = 50; - const retryLimit = 10; - - const result = await fetchCapi({ endpoint, accessToken }, retryDelay, retryLimit); - expect(result).toStrictEqual(expected); - expect(mockFetchJson).toHaveBeenCalledTimes(3); - }); - - test('should retry failed fetch requests', async () => { - const expected = { - someField: 'someValue', - }; - - const mockFetch = jest - .fn() - .mockRejectedValueOnce(new Error('TypeError: Failed to fetch')) - .mockRejectedValueOnce(new Error('TypeError: Failed to fetch')) - .mockResolvedValueOnce({ - status: 200, - ok: true, - json: async () => expected, - }); - global.fetch = mockFetch; - - const endpoint = 'https://api.test.com/endpoint'; - const accessToken = 'testToken'; - - const retryDelay = 50; - const retryLimit = 10; - - const result = await fetchCapi({ endpoint, accessToken }, retryDelay, retryLimit); - - expect(result).toStrictEqual(expected); - expect(mockFetch).toHaveBeenCalledTimes(3); - const requestInit = { - headers: { - Authorization: `Bearer ${accessToken}`, - 'Content-Type': 'application/json;charset=utf-8', - 'X-Request-ID': expect.any(String), - }, - method: 'GET', - }; - expect(mockFetch).toHaveBeenCalledWith(endpoint, requestInit); - expect(mockFetch).toHaveBeenCalledWith(endpoint, requestInit); - expect(mockFetch).toHaveBeenCalledWith(endpoint, requestInit); - }); - - test('should retry failed fetch requests based on config', async () => { - const expectedError = new Error('TypeError: Failed to fetch'); - const mockFetch = jest.fn().mockRejectedValue(expectedError); - global.fetch = mockFetch; - - const endpoint = 'https://api.test.com/endpoint'; - const accessToken = 'testToken'; - - const retryDelay = 50; - const retryLimit = 10; - try { - await fetchCapi({ endpoint, accessToken }, retryDelay, retryLimit); + await fetchCapi({ endpoint, accessToken }); } catch (error) { - expect(error).toEqual(expectedError); + expect(error).toStrictEqual(errorMsg); } - expect(mockFetch).toHaveBeenCalledTimes(retryLimit); }); }); diff --git a/src/app/backend/fetch-capi.ts b/src/app/backend/fetch-capi.ts index 2e942acce..3e70eeb26 100644 --- a/src/app/backend/fetch-capi.ts +++ b/src/app/backend/fetch-capi.ts @@ -1,4 +1,3 @@ -import delay from 'checkout/utils/delay'; import guid from 'checkout/utils/guid'; export type FetchCapiParams = { @@ -16,48 +15,29 @@ const getDetails = async (response: Response) => { } }; -const provideResponse = async (response: Response, retryDelay: number, retryLimit: number, attempt: number = 0) => { - try { - if (response.ok) { - attempt++; - return await response.json(); - } - return Promise.reject({ - status: response.status, - statusText: response.statusText || undefined, - details: await getDetails(response), - }); - } catch (ex) { - if (attempt === retryLimit) { - return Promise.reject(ex); - } - await delay(retryDelay); - return provideResponse(response, retryDelay, retryLimit, attempt); +const provideResponse = async (response: Response) => { + if (response.ok) { + return await response.json(); } + return Promise.reject({ + status: response.status, + statusText: response.statusText || undefined, + details: await getDetails(response), + }); }; -const doFetch = async (param: FetchCapiParams, retryDelay: number, retryLimit: number, attempt: number = 0) => { - try { - attempt++; - return await fetch(param.endpoint, { - method: param.method || 'GET', - headers: { - 'Content-Type': 'application/json;charset=utf-8', - Authorization: param.accessToken ? `Bearer ${param.accessToken}` : undefined, - 'X-Request-ID': guid(), - }, - body: param.body ? JSON.stringify(param.body) : undefined, - }); - } catch (ex) { - if (attempt === retryLimit) { - return Promise.reject(ex); - } - await delay(retryDelay); - return doFetch(param, retryDelay, retryLimit, attempt); - } -}; +const doFetch = async (param: FetchCapiParams) => + fetch(param.endpoint, { + method: param.method || 'GET', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + Authorization: param.accessToken ? `Bearer ${param.accessToken}` : undefined, + 'X-Request-ID': guid(), + }, + body: param.body ? JSON.stringify(param.body) : undefined, + }); -export const fetchCapi = async (param: FetchCapiParams, retryDelay = 1000, retryLimit = 20): Promise => { - const response = await doFetch(param, retryDelay, retryLimit); - return await provideResponse(response, retryDelay, retryLimit); +export const fetchCapi = async (param: FetchCapiParams): Promise => { + const response = await doFetch(param); + return await provideResponse(response); };