Skip to content

Commit

Permalink
fix: fallback and merge private and public reponse for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Feb 1, 2022
1 parent 043d15a commit 2fe3623
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"hooks:uninstall": "husky uninstall",
"hooks:install": "husky install",
"predeploy": "cd example && yarn install && yarn run build",
"test:watch": "jest src --watchAll",
"test:watch": "jest src/hooks/item.test.ts --watchAll",
"test": "jest src --silent",
"test:ci": "yarn && cd example && yarn && cd .. && yarn test",
"deploy": "gh-pages -d example/build",
Expand Down
17 changes: 16 additions & 1 deletion src/api/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ const returnFallbackDataOrThrow = (error: Error, fallbackData: unknown) => {
throw error;
};

const fallbackForArray = async (data: unknown, publicRequest: () => Promise<AxiosResponse>,) => {
// the array contains an error
if (Array.isArray(data) && data.some(d => FALLBACK_TO_PUBLIC_FOR_STATUS_CODES.includes(d?.statusCode))) {
const publicCall = await publicRequest()
const { data: publicData } = publicCall

// merge private and public valid data
const finalData = data.map((d, idx) =>
d?.statusCode ? publicData[idx] : d
)
return finalData;
}
return data
}

/**
* Automatically send request depending on whether member is authenticated
* The function fallback to public depending on status code or authentication
Expand All @@ -49,7 +64,7 @@ export const fallbackToPublic = (
}

return request()
.then(({ data }) => data)
.then(({ data }) => fallbackForArray(data, publicRequest))
.catch((error) => {
if (FALLBACK_TO_PUBLIC_FOR_STATUS_CODES.includes(error.response.status)) {
return publicRequest()
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
buildGetItemMembershipsForItemsRoute,
buildGetItemRoute,
buildGetItemsRoute,
buildGetPublicItemMembershipsForItemsRoute,
buildGetPublicItemRoute,
buildGetPublicItemsWithTag,
buildPublicDownloadFilesRoute,
Expand Down Expand Up @@ -591,6 +592,24 @@ describe('Items Hooks', () => {
expect(queryClient.getQueryData(key)).toBeFalsy();
});

// this tests fallbackForArray
it(`Merge private and public data if result with correct data and errors`, async () => {
const hook = () => hooks.useItemMemberships(ids);
const publicRoute = `/${buildGetPublicItemMembershipsForItemsRoute(ids)}`;
const publicResponse = [{ statusCode: StatusCodes.FORBIDDEN }, ITEM_MEMBERSHIPS_RESPONSE];
const privateResponse = [ITEM_MEMBERSHIPS_RESPONSE, { statusCode: StatusCodes.FORBIDDEN }];
const endpoints = [{ route, response: privateResponse }, { route: publicRoute, response: publicResponse }];
const { data } = await mockHook({
endpoints,
hook,
wrapper,
});

expect((data as List<Membership[]>).toJS()).toEqual(response);
// verify cache keys
expect(queryClient.getQueryData(key)).toEqual(List(response));
});

it(`Unauthorized`, async () => {
const hook = () => hooks.useItemMemberships(ids);
const endpoints = [
Expand Down

0 comments on commit 2fe3623

Please sign in to comment.