MCPcopy Index your code
hub / github.com/didi/mpx / fetch

Method fetch

packages/fetch/src/xfetch.js:203–285  ·  view source on GitHub ↗
(config, priority)

Source from the content-addressed store, hash-verified

201 }
202
203 fetch (config, priority) {
204 // 检查缓存
205 // middleware chain
206 const chain = []
207 let promise = Promise.resolve(config)
208
209 // use queue
210 const request = (config) => {
211 // 对config进行以下正规化处理:
212 // 1. 检查config.url存在
213 // 2. 抹平微信/支付宝header/headers字段差异
214 // 3. 填充默认method为GET, method大写化
215 // 4. 对于类GET请求将config.data移动合并至config.params(最终发送请求前进行统一序列化并拼接至config.url上)
216 // 5. 对于类POST请求将config.emulateJSON实现为config.header['content-type'] = 'application/x-www-form-urlencoded'
217 // 后续请求处理都应基于正规化后的config进行处理(proxy/mock/validate/serialize)
218 XFetch.normalizeConfig(config)
219
220 // 检查缓存
221 const responsePromise = this.checkPreCache(config)
222 if (responsePromise && typeof config.usePre.onUpdate !== 'function') {
223 return responsePromise
224 }
225
226 try {
227 const checkRes = this.checkValidator(config)
228 const isWrong = (typeof checkRes === 'boolean' && !checkRes) || (isObject(checkRes) && !checkRes.valid)
229 if (isWrong) {
230 this.onValidatorError(`xfetch参数校验错误 ${checkRes.url} ${checkRes?.errorResult ? 'error:' + checkRes.errorResult : ''} ${checkRes?.warningResult ? 'warning:' + checkRes?.warningResult : ''}`)
231 }
232 } catch (e) {
233 console.log('xfetch参数校验错误', e)
234 }
235 config = this.checkProxy(config) // proxy
236
237 if (this.useBigInt) {
238 config.useBigInt = this.useBigInt
239 }
240
241 let promise = this.queue ? this.queue.request(config, priority) : this.requestAdapter(config)
242 // 后置拦截器
243 const chain = []
244 this.interceptors.response.forEach(function pushResponseInterceptors (interceptor) {
245 chain.push(interceptor.fulfilled, interceptor.rejected)
246 })
247 while (chain.length) {
248 promise = promise.then(chain.shift(), chain.shift())
249 }
250
251 // onlyConsumer=true是一种只消费缓存数据的模式,此模式下不会产生缓存数据
252 if (config.usePre.enable && config.usePre.mode !== 'consumer') {
253 const cacheKey = formatCacheKey(config.url)
254 this.cacheRequestData[cacheKey] && (this.cacheRequestData[cacheKey].responsePromise = promise)
255 }
256
257 // 如果命中缓存,则将缓存 responsePromise 与最新 promise 竞速,返回最快的 promise
258 if (responsePromise && typeof config.usePre.onUpdate === 'function') {
259 const returnPromise = Promise.any([responsePromise, promise])
260 returnPromise.then(response => {

Callers 1

getNetworkTypeFunction · 0.80

Calls 3

resolveMethod · 0.80
forEachMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected