-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.d.ts
57 lines (57 loc) · 2.69 KB
/
main.d.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
import * as Redux from 'redux';
import { ThunkAction } from 'redux-thunk';
export declare type Status = 'pending' | 'success' | 'fail';
/** kickoff state to store data and request status */
export interface State<T> {
data: T;
status: Status;
error?: string;
}
export interface ReducerOptions {
/** decide which statuses update the data object in the reducer. default: ['success'] */
changeDataOn: Status[];
}
/** kickoff action to dispatch promise results */
export interface Action<T> extends Redux.Action {
type: string;
data: T;
status: Status;
error?: string;
}
export declare type ActionCreatorCallback<Format, State> = (dispatch: Redux.Dispatch<State>, getState: () => State, data: Format) => void;
export interface ActionCreatorOptions<R, State, Format = R> {
/** continue the thunk on success to chain actions that depend on a success */
onSuccess: ActionCreatorCallback<Format, State>;
/** continue the thunk on pening to chain actions that depend on a pending */
onPending: ActionCreatorCallback<Format, State>;
/** continue the thunk on fail to chain actions that respond to a fail */
onFail: ActionCreatorCallback<Format, State>;
/** optional default response data that will be inserted in the store when changeDataOn contains 'pending' or 'fail' */
defaultResponse: R;
/** convert the response object into your prefered object */
format: (data: R) => Format;
}
/** state object initializer */
export declare function state<T>(data: T): State<T>;
export declare function reducer<T>(state: State<T>, action: Action<T>, options?: Partial<ReducerOptions>): State<T>;
/** basic helper functions to pull out of the kickoff store */
export declare const selectors: {
getRequest: <Format>(state: State<Format>) => {
status: Status;
error: string;
};
getStatus: <Format>(state: State<Format>) => Status;
getData: <Format>(state: State<Format>) => Format;
isSuccess: <Format>(state: State<Format>) => boolean;
isFail: <Format>(state: State<Format>) => {
failed: boolean;
why: string;
};
isPending: <Format>(state: State<Format>) => boolean;
};
export declare type WrapCreator<State> = (dispatch: Redux.Dispatch<State>, getState: () => State) => void;
/** redux-thunk wrapper for calling an async function */
export declare function wrap<State>(creator: WrapCreator<State>): ThunkAction<void, State, null>;
/** action creator for kicking off a promise and loading it into the store */
export declare function kickoff<State, R, Format = R, S extends string = string>(type: S, endpoint: Promise<R>, options?: Partial<ActionCreatorOptions<R, State, Format>>): ThunkAction<void, State, null>;
export default kickoff;