-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
164 lines (148 loc) · 5.16 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
import {useCallback, useEffect, useReducer} from 'react';
import {camelCase, capitalize} from 'lodash';
import {EGlobalActions, useRequestContext} from "./global";
import {AxiosError} from 'axios'
type RequestFunction<RequestData, ResponseData extends object = {}> = (values: RequestData) => Promise<TResponse<ResponseData>>
interface IValidResponse<Data> {
data: Data;
}
interface IState {
Response: Record<string, any> | null
Error: unknown | null,
Loading: boolean,
Statuscode: null | number
}
interface ISuffixes<Data, R extends object> extends IState {
Request: RequestFunction<Data, R>,
Response: R | Record<string, any> | null
Error: Record<string, any> | null,
Loading: boolean,
Statuscode: null | number
}
type TResponse<Data> = IValidResponse<Data> | unknown;
type returnType<Data extends object, T extends string, R extends object> = {[P in keyof ReturnType<typeof suffixes<P, R>> & string as `${T}${P}`]: ReturnType<typeof suffixes<Data, R>>[P] };
const formatKey = (functionName: string, keyName: string) =>
`${camelCase(functionName)}${capitalize(keyName)}`;
const suffixes = <T, R extends object>(): ISuffixes<T, R> => ({
Request: async () => null,
Response: null,
Error: null,
Loading: false,
Statuscode: null,
} as const);
enum EAction {
'reset' = 'reset',
'loadingStart' = 'loadingStart',
'loadingStop' = 'loadingStop',
'handleResponse' = 'handleResponse'
}
const reducer = (state: IState, action: {type: EAction, value?: Record<keyof IState, any>}):IState => {
switch (action.type) {
case EAction.handleResponse:
return {
...state,
Error: action.value?.Error,
Response: action.value?.Response,
Statuscode: action.value?.Statuscode,
Loading: action.value?.Loading
};
case EAction.reset:
return {
...state,
Error: null,
Response: null,
Statuscode: null
};
case EAction.loadingStart:
return {
...state,
Loading: true,
};
case EAction.loadingStop:
return {
...state,
Loading: false,
};
default:
throw new Error();
}
}
/**
* Special recipe for lazy
* @example name = handleLogin -> {
* ${func}Response, example: handleLoginResponse
* ${func}Request,
* ${func}Loading
* ${func}Error
* ${func}StatusCode
* }
* @param func
* @param name
*/
export const useRequest = <Name extends string = '', RequestData extends object = {}, ResponseData extends object = {}>(
func: RequestFunction<RequestData, ResponseData>,
name: Name
) => {
if (!name) throw new Error('Function name not found, please pass the function name as the 2nd argument')
const [state, dispatch] = useReducer(reducer, {
Loading: false,
Response: null,
Statuscode: null,
Error: null,
})
const { dispatch: globalDispatch, loading, error } = useRequestContext()
const resetState = () => dispatch({type: EAction.reset})
const doRequest = useCallback(
async (_data: RequestData) => {
resetState();
dispatch({type: EAction.loadingStart})
try {
const exec = await func(_data);
return dispatch({
type: EAction.handleResponse,
value: {
Response: (exec as IValidResponse<ResponseData>).data || true,
Loading: false,
Statuscode: 200,
Error: null
}
});
} catch (error) {
return dispatch({
type: EAction.handleResponse,
value: {
Response: null,
Loading: false,
Statuscode: error?.response?.status || 500,
Error: error?.response?.data || error
}
});
}
},
[func]
);
useEffect(() => {
if (globalDispatch) globalDispatch({type: EGlobalActions.ADD_REQUEST, request: name})
resetState();
dispatch({type: EAction.loadingStop})
return () => {
resetState();
dispatch({type: EAction.loadingStop})
};
}, []);
const isForcedLoading = useCallback(() => {
if (!loading) return false
return loading.some(e=>e === name)
}, [loading]);
const isForcedError = useCallback(() => {
if (!error) return false
return error.some(e=>e === name)
}, [error]);
return {
[formatKey(name, 'request')]: doRequest,
[formatKey(name, 'response')]: state.Response,
[formatKey(name, 'error')]: isForcedError() ? new AxiosError('Generic error from @ajxb/useRequest', '400') : state.Error,
[formatKey(name, 'loading')]: isForcedLoading() ?? state.Loading,
[formatKey(name, 'statuscode')]: state.Statuscode,
} as returnType<RequestData, Name, ResponseData>
};