Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 2x 2x 2x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 2x 178x 1x 2x 178x 2x 179x 179x 358x 2x 177x 50x 2x 1x 1x 1x 48x 48x | import qs from 'qs' import type { AjaxInstance, AnyObj, ProxyedAttrs } from './types' import { callCancellerByCancelKey, hasCancelers } from './canceller' /** * 标准化config配置,或根据配置项设置相关的参数 * 该函数返回的配置对象,应该完全符合axios的配置规范 * @param {*} config */ export function normalizeConfig<CusOpt extends object>(ctx: AjaxInstance<CusOpt>, config: any) { const { headers = {}, sendFormData = false, data = undefined, params = {}, baseUrl = '', baseURL = '', reUseDuration = 300, ...rest } = { ...ctx.$options, ...config }; // 数据格式为FormData,修改content-type头,以及修改data格式 const contentType = headers['content-type'] || headers['Content-Type'] || 'application/json'; headers['content-type'] = sendFormData ? 'application/x-www-form-urlencoded' : contentType; const sendData = sendFormData && typeof data === 'object' && data !== null ? qs.stringify(data) : data; // baseUrl,支持两种写法,且用户配置的baseUrl优先级最高 const bUrl = baseUrl || baseURL || ctx.$baseUrl; Iif (typeof reUseDuration !== 'number' || Number.isNaN(reUseDuration)) { throw new Error('ajax配置reUseDuration必须是数字') } return { headers, params, data: sendData, baseURL: bUrl, reUseDuration, ...rest, }; } export function addInstanceExtendMethod(ctx: any) { const extend = (source: AnyObj) => { Object.keys(source).forEach((key) => { ctx[key] = source[key]; }); }; ctx.extend = extend; } export function proxyAxiosMethods<S extends {[K in ProxyedAttrs]: any}>(target: AnyObj, source: S): void { const methods: ProxyedAttrs[] = ['request', 'interceptors']; // const methods: ProxyedAttrs[] = ['request', 'get', 'post', 'delete', 'head', 'options', 'put', 'patch', 'interceptors']; methods.forEach((method) => { Object.defineProperty(target, method, { value: source[method], writable: false, }); }); } export function proxyAjaxCanceler(ctx: AjaxInstance) { ctx.canceller = new Proxy({}, { get(target, prop: string) { if (!hasCancelers(prop)) { return () => { const str = `ajax.canceller对象上不存在名为${prop}的函数!` console.warn(str) return str } } return () => { callCancellerByCancelKey(prop) } } }) } |