-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathuseFetch.ts
265 lines (229 loc) · 8.86 KB
/
useFetch.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
import { useEffect, useCallback, useRef, useReducer, useMemo } from 'react'
import useSSR from 'use-ssr'
import useRefState from 'urs'
import {
HTTPMethod,
UseFetch,
ReqMethods,
Req,
Res,
UseFetchArrayReturn,
UseFetchObjectReturn,
UseFetchArgs,
CachePolicies,
FetchData,
NoArgs,
RouteOrBody,
UFBody,
RetryOpts
} from './types'
import useFetchArgs from './useFetchArgs'
import doFetchArgs from './doFetchArgs'
import { invariant, tryGetData, toResponseObject, useDeepCallback, isFunction, sleep, makeError, isEmpty } from './utils'
import useCache from './useCache'
const { CACHE_FIRST } = CachePolicies
function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
const { host, path, customOptions, requestInit, dependencies } = useFetchArgs(...args)
const {
cacheLife,
cachePolicy, // 'cache-first' by default
interceptors,
onAbort,
onError,
onNewData,
onTimeout,
perPage,
persist,
responseType,
retries,
retryDelay,
retryOn,
suspense,
timeout,
...defaults
} = customOptions
const cache = useCache({ persist, cacheLife, cachePolicy })
const { isServer } = useSSR()
const controller = useRef<AbortController>()
const res = useRef<Res<TData>>({} as Res<TData>)
const data = useRef<TData>(defaults.data)
const timedout = useRef(false)
const attempt = useRef(0)
const error = useRef<any>()
const hasMore = useRef(true)
const suspenseStatus = useRef('pending')
const suspender = useRef<Promise<any>>()
const mounted = useRef(false)
const [loading, setLoading] = useRefState(defaults.loading)
const forceUpdate = useReducer(() => ({}), [])[1]
const makeFetch = useDeepCallback((method: HTTPMethod): FetchData<TData> => {
const doFetch = async (routeOrBody?: RouteOrBody, body?: UFBody): Promise<any> => {
if (isServer) return // for now, we don't do anything on the server
controller.current = new AbortController()
controller.current.signal.onabort = onAbort
const theController = controller.current
const { url, options, response } = await doFetchArgs<TData>(
requestInit,
method,
theController,
cacheLife,
cache,
host,
path,
routeOrBody,
body,
interceptors.request
)
error.current = undefined
// don't perform the request if there is no more data to fetch (pagination)
if (perPage > 0 && !hasMore.current && !error.current) return data.current
if (!suspense) setLoading(true)
const timer = timeout && setTimeout(() => {
timedout.current = true
theController.abort()
if (onTimeout) onTimeout()
}, timeout)
let newData
let newRes
try {
if (response.isCached && cachePolicy === CACHE_FIRST) {
newRes = response.cached as Response
} else {
newRes = (await fetch(url, options)).clone()
}
res.current = newRes.clone()
newData = await tryGetData(newRes, defaults.data, responseType)
res.current.data = onNewData(data.current, newData)
res.current = interceptors.response ? await interceptors.response({ response: res.current, request: requestInit }) : res.current
invariant('data' in res.current, 'You must have `data` field on the Response returned from your `interceptors.response`')
data.current = res.current.data as TData
const opts = { attempt: attempt.current, response: newRes }
const shouldRetry = (
// if we just have `retries` set with NO `retryOn` then
// automatically retry on fail until attempts run out
!isFunction(retryOn) && Array.isArray(retryOn) && retryOn.length < 1 && newRes?.ok === false
// otherwise only retry when is specified
|| Array.isArray(retryOn) && retryOn.includes(newRes.status)
|| isFunction(retryOn) && await (retryOn as Function)(opts)
) && retries > 0 && retries > attempt.current
if (shouldRetry) {
const theData = await retry(opts, routeOrBody, body)
return theData
}
if (cachePolicy === CACHE_FIRST && !response.isCached) {
await cache.set(response.id, newRes.clone())
}
if (Array.isArray(data.current) && !!(data.current.length % perPage)) hasMore.current = false
} catch (err) {
if (attempt.current >= retries && timedout.current) error.current = makeError('AbortError', 'Timeout Error')
const opts = { attempt: attempt.current, error: err }
const shouldRetry = (
// if we just have `retries` set with NO `retryOn` then
// automatically retry on fail until attempts run out
!isFunction(retryOn) && Array.isArray(retryOn) && retryOn.length < 1
// otherwise only retry when is specified
|| isFunction(retryOn) && await (retryOn as Function)(opts)
) && retries > 0 && retries > attempt.current
if (shouldRetry) {
const theData = await retry(opts, routeOrBody, body)
return theData
}
if (err.name !== 'AbortError') {
error.current = err
}
} finally {
timedout.current = false
if (timer) clearTimeout(timer)
controller.current = undefined
}
if (newRes && !newRes.ok && !error.current) error.current = makeError(newRes.status, newRes.statusText)
if (!suspense) setLoading(false)
if (attempt.current === retries) attempt.current = 0
if (error.current) onError({ error: error.current })
return data.current
} // end of doFetch()
const retry = async (opts: RetryOpts, routeOrBody?: RouteOrBody, body?: UFBody) => {
const delay = (isFunction(retryDelay) ? (retryDelay as Function)(opts) : retryDelay) as number
if (!(Number.isInteger(delay) && delay >= 0)) {
console.error('retryDelay must be a number >= 0! If you\'re using it as a function, it must also return a number >= 0.')
}
attempt.current++
if (delay) await sleep(delay)
const d = await doFetch(routeOrBody, body)
return d
}
if (suspense) {
return async (...args) => {
suspender.current = doFetch(...args).then(
(newData) => {
suspenseStatus.current = 'success'
return newData
},
() => {
suspenseStatus.current = 'error'
}
)
forceUpdate()
const newData = await suspender.current
return newData
}
}
return doFetch
}, [isServer, onAbort, requestInit, host, path, interceptors, cachePolicy, perPage, timeout, persist, cacheLife, onTimeout, defaults.data, onNewData, forceUpdate, suspense])
const post = useCallback(makeFetch(HTTPMethod.POST), [makeFetch])
const del = useCallback(makeFetch(HTTPMethod.DELETE), [makeFetch])
const request: Req<TData> = useMemo(() => Object.defineProperties({
get: makeFetch(HTTPMethod.GET),
post,
patch: makeFetch(HTTPMethod.PATCH),
put: makeFetch(HTTPMethod.PUT),
options: makeFetch(HTTPMethod.OPTIONS),
head: makeFetch(HTTPMethod.HEAD),
connect: makeFetch(HTTPMethod.CONNECT),
trace: makeFetch(HTTPMethod.TRACE),
del,
delete: del,
abort: () => controller.current && controller.current.abort(),
query: (query: any, variables: any) => post({ query, variables }),
mutate: (mutation: any, variables: any) => post({ mutation, variables }),
cache
}, {
loading: { get: () => loading.current },
error: { get: () => error.current },
data: { get: () => data.current },
}), [makeFetch])
const response = useMemo(() => toResponseObject<TData>(res, data), [])
// onMount/onUpdate
useEffect((): any => {
mounted.current = true
if (
Array.isArray(dependencies) &&
(dependencies.length === 0 ||
dependencies.filter((d) => !isEmpty(d)).length > 0)
) {
const methodName = requestInit.method || HTTPMethod.GET;
const methodLower = methodName.toLowerCase() as keyof ReqMethods<TData>;
const req = request[methodLower] as NoArgs;
req();
}
return () => mounted.current = false
}, dependencies)
// Cancel any running request when unmounting to avoid updating state after component has unmounted
// This can happen if a request's promise resolves after component unmounts
useEffect(() => request.abort, [])
if (suspense && suspender.current) {
if (isServer) throw new Error('Suspense on server side is not yet supported! 🙅♂️')
switch (suspenseStatus.current) {
case 'pending':
throw suspender.current
case 'error':
throw error.current
}
}
return Object.assign<UseFetchArrayReturn<TData>, UseFetchObjectReturn<TData>>(
[request, response, loading.current, error.current],
{ request, response, ...request, loading: loading.current, data: data.current, error: error.current }
)
}
export { useFetch }
export default useFetch