-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathxfetch.js
More file actions
286 lines (251 loc) · 9.68 KB
/
xfetch.js
File metadata and controls
286 lines (251 loc) · 9.68 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import requestAdapter from './request'
import CancelToken from './cancelToken'
import InterceptorManager from './interceptorManager'
import RequestQueue from './queue'
import { requestProxy } from './proxy'
import { Validator } from './validator'
import { isNotEmptyArray, isNotEmptyObject, transformReq, isObject, formatCacheKey, checkCacheConfig, isArray, isString, extend } from './util'
export default class XFetch {
constructor (options, MPX) {
this.CancelToken = CancelToken
this.cacheRequestData = {}
// this.requestAdapter = (config) => requestAdapter(config, MPX)
// 当存在 useQueue 配置时,才使用 this.queue 变量
if (options && options.useQueue && typeof options.useQueue === 'object') {
this.queue = new RequestQueue(extend({
adapter: (config) => requestAdapter(config, MPX)
}, options.useQueue))
} else {
this.requestAdapter = (config) => requestAdapter(config, MPX)
}
if (options && options.proxy) this.setProxy(options.proxy)
this.onValidatorError = options?.onValidatorError || (error => {
console.error(error)
})
this.interceptors = {
request: new InterceptorManager(),
response: new InterceptorManager()
}
this.useBigInt = (options && options.useBigInt) || false
}
static normalizeConfig (config) {
if (!config.url) {
throw new Error('no url')
}
transformReq(config)
if (!config.method) {
config.method = 'GET'
} else {
config.method = config.method.toUpperCase()
}
const params = config.params || {}
if (/^GET|DELETE|HEAD$/i.test(config.method)) {
Object.assign(params, config.data)
// get 请求都以params为准
delete config.data
}
if (isNotEmptyObject(params)) {
config.params = params
}
if (/^POST|PUT$/i.test(config.method)) {
const header = config.header || {}
const contentType = header['content-type'] || header['Content-Type']
if (config.emulateJSON && !contentType) {
header['content-type'] = 'application/x-www-form-urlencoded'
config.header = header
}
delete config.emulateJSON
}
if (!isObject(config.usePre)) {
config.usePre = {
enable: !!config.usePre
}
}
config.usePre.cacheInvalidationTime = config.usePre.cacheInvalidationTime || 3000 // 默认值3000
config.usePre.ignorePreParamKeys = config.usePre.ignorePreParamKeys || [] // 默认空数组
}
create (options) {
return new XFetch(options)
}
addLowPriorityWhiteList (rules) {
// when useQueue not optioned, this.quene is undefined
this.queue && this.queue.addLowPriorityWhiteList(rules)
}
setProxy (options) {
// 代理配置
if (isNotEmptyArray(options)) {
this.proxyOptions = options
} else if (isNotEmptyObject(options)) {
this.proxyOptions = [options]
} else {
console.error('仅支持不为空的对象或数组')
}
}
getProxy () {
// 返回代理配置
return this.proxyOptions
}
clearProxy () {
// 解除代理配置
this.proxyOptions = undefined
}
setValidator (options) {
// 添加校验配置
if (isNotEmptyObject(options)) {
const preValidatorOptions = this.validatorOptions || {}
this.validatorOptions = options || {}
Object.keys(this.validatorOptions).forEach(key => {
const preOption = preValidatorOptions[key]
const option = this.validatorOptions[key]
const objectRule = key === 'rules' && isNotEmptyObject(option)
const stringRule = (key === 'exclude' || key === 'include') && isString(option)
const isEnv = key === 'env'
if (isNotEmptyArray(option)) {
this.validatorOptions[key] = isArray(preOption) ? preOption.concat(option) : option
} else if (objectRule || stringRule) {
this.validatorOptions[key] = isArray(preOption) ? preOption.concat([option]) : [option]
} else if (isEnv) {
this.validatorOptions[key] = isObject(preOption) ? Object.assign({}, preOption, option) : option
} else {
console.error('rules仅支持不为空的数组或对象, include和exclude仅支持不为空的字符串或对象')
}
})
}
}
getValidator () {
// 返回校验配置
return this.validatorOptions
}
// 校验参数规则
checkValidator (config) {
const env = Object.assign({}, this.validatorOptions?.env, config?.validate?.env)
const options = Object.assign({}, this.validatorOptions, config.validate, { env: env })
return Validator(options, config)
}
// 向前追加代理规则
prependProxy (proxyRules) {
if (isNotEmptyArray(proxyRules)) {
this.proxyOptions = proxyRules.concat(this.proxyOptions)
} else if (isNotEmptyObject(proxyRules)) {
this.proxyOptions.unshift(proxyRules)
} else {
console.error('仅支持不为空的对象或数组')
}
}
// 向后追加代理规则
appendProxy (proxyRules) {
if (isNotEmptyArray(proxyRules)) {
this.proxyOptions = this.proxyOptions.concat(proxyRules)
} else if (isNotEmptyObject(proxyRules)) {
this.proxyOptions.push(proxyRules)
} else {
console.error('仅支持不为空的对象或数组')
}
}
checkProxy (config) {
return requestProxy(this.proxyOptions, config)
}
checkPreCache (config) {
// 未设置预请求 则直接 return
if (!config.usePre.enable) return false
const cacheKey = formatCacheKey(config.url)
const cacheRequestData = this.cacheRequestData[cacheKey]
if (cacheRequestData && config.usePre.mode !== 'producer') {
delete this.cacheRequestData[cacheKey]
// 缓存是否过期:大于cacheInvalidationTime(默认为3s)则算过期
const isNotExpired = Date.now() - cacheRequestData.lastTime <= config.usePre.cacheInvalidationTime
if (isNotExpired && cacheRequestData.responsePromise && checkCacheConfig(config, cacheRequestData)) {
return cacheRequestData.responsePromise.then(response => {
// 添加 isCache 标识该请求来源于缓存
return extend({ isCache: true }, response)
})
}
}
if (config.usePre.mode !== 'consumer') {
const { params, data, method } = config
this.cacheRequestData[cacheKey] = {
cacheKey,
params,
data,
method,
lastTime: Date.now(),
responsePromise: null
}
}
return false
}
isCancel (value) {
return !!(value && value.__CANCEL__)
}
fetch (config, priority) {
// 检查缓存
// middleware chain
const chain = []
let promise = Promise.resolve(config)
// use queue
const request = (config) => {
// 对config进行以下正规化处理:
// 1. 检查config.url存在
// 2. 抹平微信/支付宝header/headers字段差异
// 3. 填充默认method为GET, method大写化
// 4. 对于类GET请求将config.data移动合并至config.params(最终发送请求前进行统一序列化并拼接至config.url上)
// 5. 对于类POST请求将config.emulateJSON实现为config.header['content-type'] = 'application/x-www-form-urlencoded'
// 后续请求处理都应基于正规化后的config进行处理(proxy/mock/validate/serialize)
XFetch.normalizeConfig(config)
// 检查缓存
const responsePromise = this.checkPreCache(config)
if (responsePromise && typeof config.usePre.onUpdate !== 'function') {
return responsePromise
}
try {
const checkRes = this.checkValidator(config)
const isWrong = (typeof checkRes === 'boolean' && !checkRes) || (isObject(checkRes) && !checkRes.valid)
if (isWrong) {
this.onValidatorError(`xfetch参数校验错误 ${checkRes.url} ${checkRes?.errorResult ? 'error:' + checkRes.errorResult : ''} ${checkRes?.warningResult ? 'warning:' + checkRes?.warningResult : ''}`)
}
} catch (e) {
console.log('xfetch参数校验错误', e)
}
config = this.checkProxy(config) // proxy
if (this.useBigInt) {
config.useBigInt = this.useBigInt
}
let promise = this.queue ? this.queue.request(config, priority) : this.requestAdapter(config)
// 后置拦截器
const chain = []
this.interceptors.response.forEach(function pushResponseInterceptors (interceptor) {
chain.push(interceptor.fulfilled, interceptor.rejected)
})
while (chain.length) {
promise = promise.then(chain.shift(), chain.shift())
}
// onlyConsumer=true是一种只消费缓存数据的模式,此模式下不会产生缓存数据
if (config.usePre.enable && config.usePre.mode !== 'consumer') {
const cacheKey = formatCacheKey(config.url)
this.cacheRequestData[cacheKey] && (this.cacheRequestData[cacheKey].responsePromise = promise)
}
// 如果命中缓存,则将缓存 responsePromise 与最新 promise 竞速,返回最快的 promise
if (responsePromise && typeof config.usePre.onUpdate === 'function') {
const returnPromise = Promise.any([responsePromise, promise])
returnPromise.then(response => {
if (response.isCache) { // 如果预请求先返回,则等待实际请求返回后回调 usePre.onUpdate
promise.then(response => {
// 回调 usePre.onUpdate
config.usePre.onUpdate(response)
})
}
})
return returnPromise
}
return promise
}
this.interceptors.request.forEach(function unshiftRequestInterceptors (interceptor) {
chain.push(interceptor.fulfilled, interceptor.rejected)
})
chain.push(request, undefined)
while (chain.length) {
promise = promise.then(chain.shift(), chain.shift())
}
return promise
}
}