-
Notifications
You must be signed in to change notification settings - Fork 18
/
useApi.js
154 lines (137 loc) · 3.56 KB
/
useApi.js
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
import urlJoin from "proper-url-join";
import queryString from "query-string";
import { useCallback, useEffect, useReducer } from "react";
import fetchJson from "../utils/fetchJson";
import fetchMockData from "../utils/fetchMockData";
const isStringifyEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
const argumentsReducer = (state, action) => {
const previous = state[action.name];
const updated = action.value;
return !isStringifyEqual(previous, updated)
? {
...state,
[action.name]: updated
}
: state;
};
const zeroState = data => ({
data: data,
isLoading: false,
isLoaded: false,
error: null,
headers: null
});
const dataFetchReducer = (state, action) => {
switch (action.type) {
case "FETCH_RESET":
return zeroState(action.payload);
case "FETCH_INIT":
return {
...state,
isLoading: true,
isLoaded: false,
error: null,
headers: null
};
case "FETCH_SUCCESS":
return {
...state,
isLoading: false,
isLoaded: true,
data: action.payload,
headers: action.headers
};
case "FETCH_FAILURE":
return {
...state,
isLoading: false,
isLoaded: true,
error: action.error
};
default:
throw new Error();
}
};
export const useApi = (
endpoint,
options,
authCtx,
result,
callImmediately = true
) => {
const [args, dispatchArgsUpdate] = useReducer(argumentsReducer, {
result,
options,
authCtx,
callImmediately
});
useEffect(() => {
dispatchArgsUpdate({ name: "options", value: options });
}, [options]);
useEffect(() => {
dispatchArgsUpdate({ name: "authCtx", value: authCtx });
}, [authCtx]);
useEffect(() => {
dispatchArgsUpdate({ name: "result", value: result });
}, [result]);
useEffect(() => {
dispatchArgsUpdate({ name: "callImmediately", value: callImmediately });
}, [callImmediately]);
const [state, dispatch] = useReducer(
dataFetchReducer,
args.result,
zeroState
);
const fetchData = useCallback(
options => {
let didCancel = false;
const apiOptions = !!options
? {
...args.options,
...options
}
: args.options;
dispatch({ type: "FETCH_INIT" });
const promise = (apiOptions.useMockData && apiOptions.mock
? fetchMockData(apiOptions.mock, apiOptions)
: fetchJson(
queryString.stringifyUrl({
url: urlJoin(apiOptions.baseApiUrl, endpoint),
// query params supplied via `apiOptions` have a higher priority
// and will override query param with the same name if it is
// present in the `endpoint`
query: apiOptions.query
}),
args.authCtx,
apiOptions
)
)
.then(result => {
if (!didCancel)
dispatch({
type: "FETCH_SUCCESS",
payload: result.body,
headers: result.headers
});
})
.catch(error => {
if (!didCancel) dispatch({ type: "FETCH_FAILURE", error: error });
});
return {
cancel: () => {
didCancel = true;
},
promise: promise
};
},
[args, endpoint]
);
useEffect(() => {
dispatch({ type: "FETCH_RESET", payload: args.result });
if (args.callImmediately) {
const call = fetchData();
return call.cancel;
}
}, [args.result, args.callImmediately, fetchData]);
return [state, fetchData];
};