-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
142 lines (121 loc) · 4.67 KB
/
types.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
export interface StateSlice {
status: 'idle' | 'loading' | 'succeeded' | 'failed';
mutationStatus: 'idle' | 'loading' | 'succeeded' | 'failed';
receivedAt: number;
error: Error | null;
data: any | null;
}
type FnType = (...args: Array<any>) => any;
interface HelpersBag {
/** State matching the given stateKey */
state: any;
/** The whole contexeed state */
rawState: any;
/** Action dispatcher */
dispatch: FnType;
/** Reducer invalidate action. If the contexeed is defined as slicedState,
* the slice key must be passed as the function argument. */
invalidate: FnType;
}
interface OnDoneData extends HelpersBag {
/** The resulting data if available */
data: any | null;
/** The resulting error if any */
error: Error | null;
}
export interface ContexeedApiAction {
/** Url to query */
url: string;
/** Options for the request. See `axios` documentation. */
requestOptions: Object;
/** When a sliced state is being used the sliceKey is mandatory. It is a way
* to store multiple items under the same state in the form os slices. */
sliceKey: string | Array<string | number>;
/** For "request" action the data is cached. By setting skipStateCheck to
* false, the cache is skipped.*/
skipStateCheck: boolean;
/** By default a request will be made to the given url with the given
* requestOptions. If this request needs to be customized in a specific way,
* the overrideRequest function can be used for this. This function must
* return the data to store in the state. */
overrideRequest(data: {
/** Axios */
axios: any;
/** Base request option to pass to axios. Useful when decorator functions
* are used. */
requestOptions: Object;
/** State matching the given stateKey */
state: Object;
/** The whole contexeed state */
rawState: Object;
}): any;
/** Allows the data from the request response to be transformed before being
* stored in the state. If an overrideRequest function was provided, the input
* data will be whatever the overrideRequest returns. */
transformData(data: any, helpers: HelpersBag): any;
/** Allows any error from the request to be transformed before being stored in
* the state. Errors in the overrideRequest and transformData functions are
* also captured here. */
transformError(data: any, helpers: HelpersBag): any;
/** There are times when other actions must be dispatched before sending the
* data to the state and finish the process. The onDone function is the last
* hook where that can be done.
*
* @example
* onDone: (finish, { dispatch }) => {
* dispatch({ type: 'some-action' });
*
* It is important to return finish to send the data to the store. If an
* error happened it will send the error instead of the data.
* return finish();
* }
*/
onDone(
/** Finish callback. It will send the data or the error to the state. It is
* also possible to change the data being sent to the stat by passing
* arguments to the finish function (error, data).
*/
finish: FnType,
/** Extra data */
data: OnDoneData
): any;
}
export interface ContexeedApiConfig {
/** The name of the contexeed. Helpful for logging. */
name: string;
/**
* Whether to use a sliced state. This allows us to store multiple items in
* the same state under different keys. Useful to store items by id.
* False by default
*/
slicedState: boolean;
/** The interceptor is used to modify the action and/or the state before
* hitting the reducer. Can also be used to respond to custom actions */
interceptor(
state: Object,
action: { type: string }
): { state: Object; action: { type: string } };
/** List of request actions to create */
requests: { [key: string]: ContexeedApiAction };
/** List of mutation actions to create. Mutations allow us to work with
* non-get requests when interacting with the api. */
mutations: { [key: string]: ContexeedApiAction };
}
export type ContexeedApi = (
data: ContexeedApiConfig
) => {
/** List of functions created from the "requests" and "mutations" definitions
* provided by the user */
[key: string]: FnType;
/** Invalidate action already dispatchable. If contexeed is defined as a
* slicedState a key will be required. */
invalidate(key?: string): any;
/** Function to get the current state. If contexeed is defined as a
* slicedState a key will be required and the returned state will be a state
* slice. */
getState(key?: string): any;
/** The state in raw format as stored in the reducer. */
rawState: any;
/** The reducer's dispatch function. It supports dispatching thunks. */
dispatch(action: { type: string } | FnType): any;
};