Skip to content

Commit

Permalink
feat(serviceConfig): ent-4669 pass config for transformers (#922)
Browse files Browse the repository at this point in the history
* serviceConfig, helpers config to transformers, include params
  • Loading branch information
cdcabrera committed Jun 14, 2022
1 parent 90125a4 commit 3043f6b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
12 changes: 12 additions & 0 deletions src/services/common/__tests__/__snapshots__/helpers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
`;

Expand Down
18 changes: 16 additions & 2 deletions src/services/common/__tests__/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -71,5 +83,7 @@ describe('Service Helpers', () => {
casing: 'camel'
})
).toMatchSnapshot('schemaResponse, camelCasing');

mockValidate.mockClear();
});
});
24 changes: 18 additions & 6 deletions src/services/common/__tests__/serviceConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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');
});
Expand Down
6 changes: 3 additions & 3 deletions src/services/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/services/common/serviceConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -155,8 +156,9 @@ const axiosServiceCall = async (
}

const { data, error: normalizeError } = serviceHelpers.passDataToCallback(
errorTransform,
updatedResponse?.data || updatedResponse?.message,
errorTransform
updatedResponse.config
);

if (!normalizeError) {
Expand Down

0 comments on commit 3043f6b

Please sign in to comment.