Skip to content

Commit

Permalink
Support fetchall where 2nd request contains more sideloaded subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
lorgan3 committed Jul 12, 2024
1 parent 251a452 commit 95a6ba6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/utils/__tests__/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ describe('fetch response handling', () => {
const headers = { 'content-type': 'application/json' };

fetchMock
.once(
JSON.stringify({
data: [{ name: 'Jane', last_name: 'Doe' }],
included: {},
meta: {
page: {
size: 1,
number: 1,
},
matches: 4,
},
}),
{ headers },
)
.once(
JSON.stringify({
data: [{ name: 'John', last_name: 'Doe' }],
Expand All @@ -111,7 +125,7 @@ describe('fetch response handling', () => {
size: 1,
number: 1,
},
matches: 3,
matches: 4,
},
}),
{ headers },
Expand All @@ -125,7 +139,7 @@ describe('fetch response handling', () => {
size: 1,
number: 2,
},
matches: 3,
matches: 4,
},
}),
{ headers },
Expand All @@ -139,7 +153,7 @@ describe('fetch response handling', () => {
size: 1,
number: 3,
},
matches: 3,
matches: 4,
},
}),
{ headers },
Expand All @@ -153,6 +167,7 @@ describe('fetch response handling', () => {

expect(jsonResponse).toEqual({
data: [
{ name: 'Jane', lastName: 'Doe' },
{ name: 'John', lastName: 'Doe' },
{ name: 'Alex', lastName: 'Turner' },
{ name: 'William', lastName: 'Hurt' },
Expand Down
8 changes: 7 additions & 1 deletion src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ const request = async ({
included: mergeWith(
firstRequestData.included,
...parallelRequestData.map(({ included }) => included),
(objValue: unknown[], srcValue: unknown[]) => objValue.concat(srcValue),
(objValue: unknown[] | undefined, srcValue: unknown[]) => {
if (!objValue) {
return srcValue;
}

return objValue.concat(srcValue);
},
),
},
responsePlugins,
Expand Down

0 comments on commit 95a6ba6

Please sign in to comment.