-
Notifications
You must be signed in to change notification settings - Fork 128
/
index.js
179 lines (141 loc) · 3.71 KB
/
index.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
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
import React from 'react'
import DefaultAxios from 'axios'
import LRU from 'lru-cache'
const actions = {
REQUEST_START: 'REQUEST_START',
REQUEST_END: 'REQUEST_END'
}
export const __ssrPromises = []
let cache
let axiosInstance
export function resetConfigure() {
cache = new LRU()
axiosInstance = DefaultAxios
}
resetConfigure()
export function configure(options) {
if (options.axios) {
axiosInstance = options.axios
}
if (options.cache) {
cache = options.cache
}
}
export function loadCache(data) {
cache.load(data)
}
export async function serializeCache() {
const ssrPromisesCopy = [...__ssrPromises]
__ssrPromises.length = 0
await Promise.all(ssrPromisesCopy)
return cache.dump()
}
async function cacheAdapter(config) {
const cacheKey = JSON.stringify(config)
const hit = cache.get(cacheKey)
if (hit) {
return hit
}
delete config.adapter
const response = await axiosInstance(config)
const responseForCache = { ...response }
delete responseForCache.config
delete responseForCache.request
cache.set(cacheKey, responseForCache)
return response
}
function createInitialState(options) {
return {
loading: !options.manual
}
}
function reducer(state, action) {
switch (action.type) {
case actions.REQUEST_START:
return {
...state,
loading: true,
error: null
}
case actions.REQUEST_END:
return {
...state,
loading: false,
...(action.error ? {} : { data: action.payload.data }),
[action.error ? 'error' : 'response']: action.payload
}
default:
return state
}
}
async function request(config, dispatch) {
try {
dispatch({ type: actions.REQUEST_START })
const response = await axiosInstance(config)
dispatch({ type: actions.REQUEST_END, payload: response })
return response
} catch (err) {
if (axiosInstance.isCancel(err)) {
return
}
dispatch({ type: actions.REQUEST_END, payload: err, error: true })
throw err
}
}
function executeRequestWithCache(config, dispatch) {
return request({ ...config, adapter: cacheAdapter }, dispatch)
}
function executeRequestWithoutCache(config, dispatch) {
return request(config, dispatch)
}
function executeRequest(config, options, dispatch) {
if (options.useCache) {
return executeRequestWithCache(config, dispatch)
}
return executeRequestWithoutCache(config, dispatch)
}
export default function useAxios(config, options) {
if (typeof config === 'string') {
config = {
url: config
}
}
const stringifiedConfig = JSON.stringify(config)
options = { manual: false, useCache: true, ...options }
const cancelSourceRef = React.useRef()
const [state, dispatch] = React.useReducer(
reducer,
createInitialState(options)
)
if (typeof window === 'undefined' && !options.manual) {
__ssrPromises.push(axiosInstance({ ...config, adapter: cacheAdapter }))
}
React.useEffect(() => {
cancelSourceRef.current = axiosInstance.CancelToken.source()
if (!options.manual) {
executeRequest(
{ cancelToken: cancelSourceRef.current.token, ...config },
options,
dispatch
).catch(() => {})
}
return () => cancelSourceRef.current.cancel()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [stringifiedConfig])
const refetch = React.useCallback(
(configOverride, options) => {
return executeRequest(
{
cancelToken: cancelSourceRef.current.token,
...config,
...configOverride
},
{ useCache: false, ...options },
dispatch
)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[stringifiedConfig]
)
return [state, refetch]
}