-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5395 from marmelab/relax-dataprovider-signature
Relax useDataProvider signature to ease custom methods usage
- Loading branch information
Showing
10 changed files
with
319 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/ra-core/src/dataProvider/getDataProviderCallArguments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { UseDataProviderOptions } from '../types'; | ||
|
||
// List of properties we expect in the options | ||
const OptionsProperties = [ | ||
'action', | ||
'fetch', | ||
'meta', | ||
'onFailure', | ||
'onSuccess', | ||
'undoable', | ||
]; | ||
|
||
const isDataProviderOptions = (value: any) => { | ||
let options = value as UseDataProviderOptions; | ||
|
||
return Object.keys(options).some(key => OptionsProperties.includes(key)); | ||
}; | ||
|
||
// As all dataProvider methods do not have the same signature, we must differentiate | ||
// standard methods which have the (resource, params, options) signature | ||
// from the custom ones | ||
export const getDataProviderCallArguments = (args: any[]) => { | ||
const lastArg = args[args.length - 1]; | ||
let allArguments = [...args]; | ||
|
||
let resource; | ||
let payload; | ||
let options; | ||
|
||
if (isDataProviderOptions(lastArg)) { | ||
options = lastArg as UseDataProviderOptions; | ||
allArguments = allArguments.slice(0, args.length - 1); | ||
} | ||
|
||
if (typeof allArguments[0] === 'string') { | ||
resource = allArguments[0]; | ||
payload = allArguments[1]; | ||
} | ||
|
||
return { | ||
resource, | ||
payload, | ||
allArguments, | ||
options, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.