-
Notifications
You must be signed in to change notification settings - Fork 128
/
index.js
151 lines (118 loc) · 3.01 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
import React from 'react'
import DefaultAxios from 'axios'
import LRU from 'lru-cache'
const actions = {
REQUEST_START: 'REQUEST_START',
REQUEST_END: 'REQUEST_END'
}
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 })
} catch (err) {
dispatch({ type: actions.REQUEST_END, payload: err, error: true })
}
}
function executeRequestWithCache(config, dispatch) {
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
}
}
options = { manual: false, useCache: true, ...options }
const [state, dispatch] = React.useReducer(
reducer,
createInitialState(options)
)
if (typeof window === 'undefined') {
ssrPromises.push(axiosInstance({ ...config, adapter: cacheAdapter }))
}
React.useEffect(() => {
if (!options.manual) {
executeRequest(config, options, dispatch)
}
}, [JSON.stringify(config)])
return [
state,
(configOverride, options) => {
options = { useCache: false, ...options }
return executeRequest({ ...config, ...configOverride }, options, dispatch)
}
]
}