-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.ts
314 lines (267 loc) · 8.21 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import * as get from 'lodash.get';
export { default as Sequence } from './Sequence';
/** fetch 中间件的 types */
type F<T> = { [key in keyof T]: { success: key; error: 'error'; loading: 'loading' } };
type B<T> = { [key in keyof T]: key };
export const NO_ERROR_TYPES = -1;
const LOADING_SUFFIX = '_LOADING';
const SUCCESS_SUFFIX = '_SUCCESS';
const ERROR_SUFFIX = '_ERROR';
declare let Proxy: any;
/** 创建 Types */
export function composeTypes<T1, T2>(config: { prefix: string; BasicTypes: T1; FetchTypes: T2 }): B<T1> & F<T2> {
const { prefix, BasicTypes: actionTypes = {}, FetchTypes: fetchActionTypes = {} } = config;
const types = { ...(actionTypes as any), ...(fetchActionTypes as any) };
const target = types;
const res = {} as any;
Object.keys(types).forEach(property => {
if (fetchActionTypes.hasOwnProperty(property)) {
let result = [] as any;
if (fetchActionTypes[property] === NO_ERROR_TYPES) {
result = [prefix + property + LOADING_SUFFIX, prefix + property + SUCCESS_SUFFIX, null];
result.loading = result[0];
result.success = result[1];
res[property] = result;
return;
}
result = [
prefix + property + LOADING_SUFFIX,
prefix + property + SUCCESS_SUFFIX,
prefix + property + ERROR_SUFFIX
];
result.loading = result[0];
result.success = result[1];
result.error = result[2];
res[property] = result;
return;
}
res[property] = prefix + property;
});
return res;
}
interface Action<key, T> {
type: key;
payload: T;
meta?: any;
params?: any;
}
export type ActionCreator<T> = <S>(type: S) => (params?: T) => { type: S; payload: T };
const identify = arg => arg;
/**
* 创建单个 action
*/
export function createAction<T>(type: T, stateKey = '') {
return <P, R = P>(fn: (params?: P, ...args: any[]) => R = identify) => (params?: P, ...args: any[]) => {
if (stateKey) {
return {
type,
stateKey,
payload: fn(params, ...args)
};
}
return {
type,
payload: fn(params, ...args)
};
};
}
interface IFetchTypes<key> {
error: 'error';
success: key;
loading: 'loading';
}
export enum Method {
Get = 'GET',
Post = 'POST',
Put = 'PUT',
Delete = 'DELETE'
}
/**
* 创建 fetch action
*/
export function createFetchAction<Key>(types: IFetchTypes<Key>, url: string, method = Method.Get) {
return <Params, Response>(stateKey?: string) => (params?: Params, meta?) => {
const action = {
stateKey,
types,
meta,
params,
url,
method
};
return action as typeof action & { type: Key; payload: Response } & Partial<Promise<Response>>;
};
}
/** 根据 Reducer Map 返回 全局 State */
export type ReturnState<ReducerMap> = {
[key in keyof ReducerMap]: ReducerMap[key] extends (state: any, action: any) => infer R ? R : any
};
type ValueOf<T> = T[keyof T];
/**
*
* 获取 action 类型
*/
export type ActionType<Actions> =
| ValueOf<{ [key in keyof Actions]: Actions[key] extends (...args: any[]) => infer R ? R : never }>
| ValueOf<{ [key in keyof Actions]: Actions[key] extends (arg: never, ...args: any[]) => infer R ? R : never }>
| {
type: 'error';
payload?: { message: string; [key: string]: any };
params?: any;
meta?: any;
}
| { type: 'loading'; payload?: any; params?: any; meta?: any };
/**
*
* 获取 mapStateToProps 的返回结果类型
*/
export function getMergedDefinetion<Result>(map: (state) => Result): Result {
return;
}
export type Dispatch = <Payload>(action: { type?: string; payload?: Payload }) => Promise<Payload>;
export type ThunkAction<Payload = never> = {
type?: '@thunkAction';
payload?: never;
(dispatch: Dispatch, getState?: any): any;
} & Partial<Promise<Payload>>;
export class AsyncTuple<T> {
/** 是否在加载中 */
loading = true;
/** 是否加载出错 */
error = false;
/** 出错的 message */
message? = '';
/** 具体的 data */
data?: T;
[x: string]: any;
constructor(initData: T)
constructor(initLoading: boolean, initData: T)
constructor(initLoading?, initData?) {
if (initData != null) {
this.data = initData;
this.loading = !!initLoading;
} if (typeof initLoading === 'boolean') {
this.loading = initLoading;
} else {
this.data = initLoading;
}
}
static handleLoading<K extends keyof T, T extends Object>(stateKey: K, state: T, extraProps: Object = {}): T {
const localState = state as any;
const localKey = stateKey as string;
return {
...localState,
[localKey]: {
...localState[localKey],
loading: true,
error: false,
...extraProps
}
};
}
static handleSuccess<K extends keyof T, T extends Object>(stateKey: K, state: T, action, extraProps: Object = {}): T {
const localState = state as any;
const localKey = stateKey as string;
return {
...localState,
[localKey]: {
...localState[localKey],
loading: false,
error: false,
data: action.payload,
...extraProps
}
};
}
static handleError<K extends keyof T, T extends Object>(stateKey: K, state: T, action, extraProps: Object = {}): T {
const localState = state as any;
const localKey = stateKey as string;
return {
...localState,
[localKey]: {
...localState[localKey],
loading: false,
error: true,
message: action.payload.message || localState[localKey].message,
...extraProps
}
};
}
static defaultProcess<K extends keyof T, T extends Object>(
stateKey: K,
state: T,
action,
fetchType: 'loading' | 'success' | 'error' | 'normal'
): T {
if (fetchType === 'loading') {
return AsyncTuple.handleLoading(stateKey, state);
} else if (fetchType === 'success') {
return AsyncTuple.handleSuccess(stateKey, state, action);
} else if (fetchType === 'error') {
return AsyncTuple.handleError(stateKey, state, action);
} else if (fetchType === 'normal') {
return {
...(state as any),
[stateKey]: action.payload
};
}
return state;
}
static handleAll<State>(prefix: string, state: State, action, process = AsyncTuple.defaultProcess) {
if (action.url && action.type && action.stateKey && action.type.startsWith(prefix)) {
const actionType = action.type as any;
let fetchType = '' as 'loading' | 'success' | 'error';
if (actionType.endsWith(LOADING_SUFFIX)) {
fetchType = 'loading';
} else if (actionType.endsWith(SUCCESS_SUFFIX)) {
fetchType = 'success';
} else if (actionType.endsWith(ERROR_SUFFIX)) {
fetchType = 'error';
}
return process(action.stateKey, state, action, fetchType);
} else if (action.type && action.stateKey && action.type.startsWith(prefix)) {
return process(action.stateKey, state, action, 'normal');
}
return state;
}
}
export function safeGet<T, K1 extends keyof T>(data: T, keys: [K1], defaultValue?): T[K1];
export function safeGet<T, K1 extends keyof T, K2 extends keyof T[K1]>(
data: T,
keys: [K1, K2],
defaultValue?
): T[K1][K2];
export function safeGet<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(
data: T,
keys: [K1, K2, K3],
defaultValue?
): T[K1][K2][K3];
export function safeGet<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3]
>(data: T, keys: [K1, K2, K3, K4], defaultValue?): T[K1][K2][K3][K4];
export function safeGet<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4]
>(data: T, keys: [K1, K2, K3, K4, K5], defaultValue?): T[K1][K2][K3][K4][K5];
export function safeGet<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4],
K6 extends keyof T[K1][K2][K3][K4][K5]
>(data: T, keys: [K1, K2, K3, K4, K5, K6], defaultValue?): T[K1][K2][K3][K4][K5][K6];
export function safeGet<T>(data: T, keys: any[], defaultValue?) {
return keys.reduce((obj, key) => {
return get(obj, key, defaultValue);
}, data);
}