diff --git a/src/services/common/__tests__/__snapshots__/helpers.test.js.snap b/src/services/common/__tests__/__snapshots__/helpers.test.js.snap index f25ecce45..d24c76adc 100644 --- a/src/services/common/__tests__/__snapshots__/helpers.test.js.snap +++ b/src/services/common/__tests__/__snapshots__/helpers.test.js.snap @@ -28,12 +28,24 @@ Object { } `; +exports[`Service Helpers should support applying data to a supplied callback: passDataToCallback, error 1`] = ` +Object { + "data": Array [ + Object { + "dolor": "sit", + }, + ], + "error": [Error: hello world], +} +`; + exports[`Service Helpers should support applying data to a supplied callback: passDataToCallback, success 1`] = ` Array [ Array [ Object { "hello": "world", }, + "lorem ipsum", ], ] `; diff --git a/src/services/common/__tests__/__snapshots__/serviceConfig.test.js.snap b/src/services/common/__tests__/__snapshots__/serviceConfig.test.js.snap index 70c415ef0..e156a415c 100644 --- a/src/services/common/__tests__/__snapshots__/serviceConfig.test.js.snap +++ b/src/services/common/__tests__/__snapshots__/serviceConfig.test.js.snap @@ -13,12 +13,12 @@ Array [ exports[`ServiceConfig should handle caching service calls: cached responses, emulated 304 1`] = ` Array [ - "1. method=get, status=200, desc=initial call", - "2. method=get, status=304, desc=repeat 1st call and config", - "3. method=get, status=200, desc=updated config", - "4. method=post, status=200, desc=attempt post method", - "5. method=get, status=304, desc=repeat 3rd call and config", - "6. method=get, status=200, desc=no caching", + "1. method=get, status=200, cacheId=eyJ0aW1lb3V0Ijo2MDAwMCwidXJsIjoiL3Rlc3QvIiwicGFyYW1zIjoiW1tcImRvbG9yXCIsXCJzaXRcIl0sW1wibG9yZW1cIixcImlwc3VtXCJdXSIsImV4cG9zZUNhY2hlSWQiOnRydWUsImNhY2hlUmVzcG9uc2UiOnRydWUsIm1ldGhvZCI6ImdldCJ9, desc=initial call", + "2. method=get, status=304, cacheId=eyJ0aW1lb3V0Ijo2MDAwMCwidXJsIjoiL3Rlc3QvIiwicGFyYW1zIjoiW1tcImRvbG9yXCIsXCJzaXRcIl0sW1wibG9yZW1cIixcImlwc3VtXCJdXSIsImV4cG9zZUNhY2hlSWQiOnRydWUsImNhY2hlUmVzcG9uc2UiOnRydWUsIm1ldGhvZCI6ImdldCJ9, desc=repeat 1st call and config", + "3. method=get, status=200, cacheId=eyJ0aW1lb3V0Ijo2MDAwMCwidXJsIjoiL3Rlc3QvIiwicGFyYW1zIjoiW1tcImxvcmVtXCIsXCJpcHN1bVwiXV0iLCJleHBvc2VDYWNoZUlkIjp0cnVlLCJjYWNoZVJlc3BvbnNlIjp0cnVlLCJtZXRob2QiOiJnZXQifQ==, desc=updated config", + "4. method=post, status=200, cacheId=null, desc=attempt post method", + "5. method=get, status=304, cacheId=eyJ0aW1lb3V0Ijo2MDAwMCwidXJsIjoiL3Rlc3QvIiwicGFyYW1zIjoiW1tcImxvcmVtXCIsXCJpcHN1bVwiXV0iLCJleHBvc2VDYWNoZUlkIjp0cnVlLCJjYWNoZVJlc3BvbnNlIjp0cnVlLCJtZXRob2QiOiJnZXQifQ==, desc=repeat 3rd call and config", + "6. method=get, status=200, cacheId=null, desc=no caching", ] `; diff --git a/src/services/common/__tests__/helpers.test.js b/src/services/common/__tests__/helpers.test.js index e4ceff55a..a832ca40a 100644 --- a/src/services/common/__tests__/helpers.test.js +++ b/src/services/common/__tests__/helpers.test.js @@ -51,17 +51,29 @@ describe('Service Helpers', () => { it('should support applying data to a supplied callback', () => { const mockSchema = jest.fn(); - serviceHelpers.passDataToCallback({ hello: 'world' }, mockSchema); + serviceHelpers.passDataToCallback(mockSchema, { hello: 'world' }, 'lorem ipsum'); expect(mockSchema.mock.calls).toMatchSnapshot('passDataToCallback, success'); mockSchema.mockClear(); + + expect( + serviceHelpers.passDataToCallback( + () => { + throw new Error('hello world'); + }, + { dolor: 'sit' } + ) + ).toMatchSnapshot('passDataToCallback, error'); }); it('should attempt to apply a Joi schema to a response', () => { const mockValidate = jest.fn().mockImplementation(response => ({ value: response })); - serviceHelpers.schemaResponse({ schema: { validate: mockValidate }, response: { lorem_ipsum: 'dolor_sit' } }); + serviceHelpers.schemaResponse({ + schema: { validate: mockValidate }, + response: { lorem_ipsum: 'dolor_sit' } + }); expect(mockValidate.mock.calls).toMatchSnapshot('schemaResponse, parameters'); expect( @@ -71,5 +83,7 @@ describe('Service Helpers', () => { casing: 'camel' }) ).toMatchSnapshot('schemaResponse, camelCasing'); + + mockValidate.mockClear(); }); }); diff --git a/src/services/common/__tests__/serviceConfig.test.js b/src/services/common/__tests__/serviceConfig.test.js index 82187c24c..7a9c15ef2 100644 --- a/src/services/common/__tests__/serviceConfig.test.js +++ b/src/services/common/__tests__/serviceConfig.test.js @@ -106,7 +106,9 @@ describe('ServiceConfig', () => { params: { lorem: 'ipsum', dolor: 'sit' }, exposeCacheId: true }); - responses.push(`1. method=get, status=${responseOne.status}, desc=initial call`); + responses.push( + `1. method=get, status=${responseOne.status}, cacheId=${responseOne.request.config.cacheId}, desc=initial call` + ); // Second, call the same endpoint with same params, expect a cached response, emulated 304 const responseTwo = await serviceConfig.axiosServiceCall({ @@ -115,7 +117,9 @@ describe('ServiceConfig', () => { params: { lorem: 'ipsum', dolor: 'sit' }, exposeCacheId: true }); - responses.push(`2. method=get, status=${responseTwo.status}, desc=repeat 1st call and config`); + responses.push( + `2. method=get, status=${responseTwo.status}, cacheId=${responseTwo.request.config.cacheId}, desc=repeat 1st call and config` + ); // Third, updating config creates a new cache const responseThree = await serviceConfig.axiosServiceCall({ @@ -124,7 +128,9 @@ describe('ServiceConfig', () => { params: { lorem: 'ipsum' }, exposeCacheId: true }); - responses.push(`3. method=get, status=${responseThree.status}, desc=updated config`); + responses.push( + `3. method=get, status=${responseThree.status}, cacheId=${responseThree.request.config.cacheId}, desc=updated config` + ); // Fourth, confirm cache isn't created for other methods, i.e. post const responseFour = await serviceConfig.axiosServiceCall({ @@ -134,7 +140,9 @@ describe('ServiceConfig', () => { params: { lorem: 'ipsum' }, exposeCacheId: true }); - responses.push(`4. method=post, status=${responseFour.status}, desc=attempt post method`); + responses.push( + `4. method=post, status=${responseFour.status}, cacheId=${responseFour.request.config.cacheId}, desc=attempt post method` + ); // Fifth, confirm cache exists from responseThree const responseFive = await serviceConfig.axiosServiceCall({ @@ -143,7 +151,9 @@ describe('ServiceConfig', () => { params: { lorem: 'ipsum' }, exposeCacheId: true }); - responses.push(`5. method=get, status=${responseFive.status}, desc=repeat 3rd call and config`); + responses.push( + `5. method=get, status=${responseFive.status}, cacheId=${responseFive.request.config.cacheId}, desc=repeat 3rd call and config` + ); // Six, don't use a cache const responseSix = await serviceConfig.axiosServiceCall({ @@ -152,7 +162,9 @@ describe('ServiceConfig', () => { params: { lorem: 'ipsum' }, exposeCacheId: true }); - responses.push(`6. method=get, status=${responseSix.status}, desc=no caching`); + responses.push( + `6. method=get, status=${responseSix.status}, cacheId=${responseSix.request.config.cacheId}, desc=no caching` + ); expect(responses).toMatchSnapshot('cached responses, emulated 304'); }); diff --git a/src/services/common/helpers.js b/src/services/common/helpers.js index 6a0dc664b..4a00b5b68 100644 --- a/src/services/common/helpers.js +++ b/src/services/common/helpers.js @@ -60,16 +60,16 @@ const camelCase = obj => { /** * Apply data to a callback, pass original data on error. * - * @param {object} data * @param {Function} callback + * @param {Array} data * @returns {{data: *, error}} */ -const passDataToCallback = (data, callback) => { +const passDataToCallback = (callback, ...data) => { let error; let updatedData = data; try { - updatedData = callback(data); + updatedData = callback(...data); } catch (e) { error = e; } diff --git a/src/services/common/serviceConfig.js b/src/services/common/serviceConfig.js index 6afacb52e..cccf8a01b 100644 --- a/src/services/common/serviceConfig.js +++ b/src/services/common/serviceConfig.js @@ -134,8 +134,9 @@ const axiosServiceCall = async ( transformers[0] = response => { const updatedResponse = { ...response }; const { data, error: normalizeError } = serviceHelpers.passDataToCallback( + successTransform, updatedResponse.data, - successTransform + updatedResponse.config ); if (!normalizeError) { @@ -155,8 +156,9 @@ const axiosServiceCall = async ( } const { data, error: normalizeError } = serviceHelpers.passDataToCallback( + errorTransform, updatedResponse?.data || updatedResponse?.message, - errorTransform + updatedResponse.config ); if (!normalizeError) {