-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
getDataProviderCallArguments.ts
56 lines (48 loc) · 1.33 KB
/
getDataProviderCallArguments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { UseDataProviderOptions } from '../types';
// List of properties we expect in the options
const OptionsProperties = [
'action',
'fetch',
'meta',
'onFailure',
'onSuccess',
'undoable',
'mutationMode',
'enabled',
];
const isDataProviderOptions = (value: any) => {
if (typeof value === 'undefined') return [];
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[]
): {
resource: string;
payload: any;
options: UseDataProviderOptions;
allArguments: 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,
};
};