From 344f29f9088a48d487918fefbcc2ec900d156c15 Mon Sep 17 00:00:00 2001 From: Valentin Laurin Date: Thu, 6 Mar 2025 17:42:03 +0000 Subject: [PATCH] Fix: Make config optional on decorated Axios Align with Axios contract, config should be optional --- src/api/api-client.js | 4 +-- src/api/api-client.test.js | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/api/api-client.js b/src/api/api-client.js index c2602c1..c7465a7 100644 --- a/src/api/api-client.js +++ b/src/api/api-client.js @@ -13,7 +13,7 @@ export const ApiClient = (apiFactory) => (axiosInstance, options = {}) => (req, const controller = new AbortController(); res.on('close', () => controller.abort()); - const decorateWithoutData = (axiosMethod) => async (url, config) => axiosMethod(url, { + const decorateWithoutData = (axiosMethod) => async (url, config = {}) => axiosMethod(url, { ...config, signal: controller.signal, headers: { @@ -22,7 +22,7 @@ export const ApiClient = (apiFactory) => (axiosInstance, options = {}) => (req, }, }); - const decorateWithData = (axiosMethod) => async (url, data, config) => axiosMethod(url, data, { + const decorateWithData = (axiosMethod) => async (url, data, config = {}) => axiosMethod(url, data, { ...config, signal: controller.signal, headers: { diff --git a/src/api/api-client.test.js b/src/api/api-client.test.js index 309ebac..31704fe 100644 --- a/src/api/api-client.test.js +++ b/src/api/api-client.test.js @@ -29,11 +29,13 @@ const TestApiClient = ApiClient((axiosInstance) => ({ 'accept': 'application/samples+json;charset=UTF-8', }, }), + listNoConfig: () => axiosInstance.get('/samples'), createSample: (sample) => axiosInstance.post('/samples', sample, { headers: { 'accept': 'application/samples+json;charset=UTF-8', } }), + createNoConfig: (sample) => axiosInstance.post('/samples', sample), })); test('should make GET API calls as configured', async () => { @@ -71,6 +73,37 @@ test('should make GET API calls as configured', async () => { scope.done(); }); +test('should make GET API calls without config', async () => { + const req = MockRequest(); + const res = MockResponse(); + + const axiosInstance = axios.create({ + baseURL: 'https://api.quickcase.app', + }); + const client = TestApiClient(axiosInstance)(req, res); + + const scope = nock('https://api.quickcase.app') + .get('/samples') + .reply(200, { + samples: [ + {id: 1}, + {id: 2}, + ], + }); + + const listResponse = await client.listNoConfig(); + + expect(listResponse.status).toBe(200); + expect(listResponse.data).toEqual({ + samples: [ + {id: 1}, + {id: 2}, + ], + }); + + scope.done(); +}); + test('should authorize GET API call with provided access token', async () => { const req = MockRequest(); const res = MockResponse(); @@ -142,6 +175,35 @@ test('should make POST API calls as configured', async () => { scope.done(); }); +test('should make POST API calls without config', async () => { + const req = MockRequest(); + const res = MockResponse(); + + const axiosInstance = axios.create({ + baseURL: 'https://api.quickcase.app', + }); + const client = TestApiClient(axiosInstance)(req, res); + + const sample = {name: 'Some sample'}; + + const scope = nock('https://api.quickcase.app') + .post('/samples', sample) + .reply(201, { + id: '1', + ...sample, + }); + + const createResponse = await client.createNoConfig(sample); + + expect(createResponse.status).toBe(201); + expect(createResponse.data).toEqual({ + id: '1', + name: 'Some sample', + }); + + scope.done(); +}); + test('should authorize POST API call with provided access token', async () => { const req = MockRequest(); const res = MockResponse();