Skip to content

Commit

Permalink
Merge pull request #350 from teamleadercrm/fix-for-missing-sideloaded…
Browse files Browse the repository at this point in the history
…-data

Support fetchall where 2nd request contains more sideloaded subjects
  • Loading branch information
lorgan3 authored Jul 12, 2024
2 parents 251a452 + a473d0c commit fa6f6a5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

### Fixed

## [6.1.1] - 2024-07-12

### Fixed

- Fix crash when using `fetchAll` and first request contains less sideloaded data than subsequent requests ([@lorgan3](https://github.com/lorgan3) in [#350](https://github.com/teamleadercrm/sdk-js/pull/350))

## [6.1.0] - 2023-05-10

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@teamleader/api",
"version": "6.1.0",
"version": "6.1.1",
"description": "Teamleader API SDK",
"main": "dist/cjs/main.js",
"module": "dist/es/main.js",
Expand Down
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 fa6f6a5

Please sign in to comment.