Skip to content

Commit

Permalink
add use of rest parameters in combineDataProviders
Browse files Browse the repository at this point in the history
The current implementation doesn't work with dataProvider methods with more than 2 parameters.
According to documentation, the method parameter format is not strictly typed, so it is possible to add as many params as needed and then guess why combineDataProviders doesn't work
  • Loading branch information
karpushchenko authored and Ivan Karpushchenko committed Feb 21, 2024
1 parent 6b3976a commit 384e6c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 22 additions & 0 deletions packages/ra-core/src/dataProvider/combineDataProviders.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,26 @@ describe('combineDataProviders', () => {
expect(dataProvider1.getOne).not.toHaveBeenCalled();
expect(dataProvider2.getOne).toHaveBeenCalled();
});
it('calls custom dataProvider methods with any number of parameters', async () => {
const dataProviderWithCustomMethod = testDataProvider({
customMethod: jest
.fn()
.mockResolvedValue({ data: { id: 1, foo: 'bar' } }),
});
const dataProvider = combineDataProviders(resource => {
switch (resource) {
case 'custom':
return dataProviderWithCustomMethod;
default:
throw new Error('Unknown resource');
}
});
dataProvider.customMethod('custom', 1, 2, 3);
expect(dataProviderWithCustomMethod.customMethod).toHaveBeenCalledWith(
'custom',
1,
2,
3
);
});
});
5 changes: 3 additions & 2 deletions packages/ra-core/src/dataProvider/combineDataProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export const combineDataProviders = (
): DataProvider =>
new Proxy(defaultDataProvider, {
get: (target, name) => {
return (resource, params) => {
// Using rest parameters to support custom dataProvider methods with any number of parameters
return (resource, ...params) => {
if (typeof name === 'symbol' || name === 'then') {
return;
}
return dataProviderMatcher(resource)[name](resource, params);
return dataProviderMatcher(resource)[name](resource, ...params);
};
},
});

0 comments on commit 384e6c6

Please sign in to comment.