All files / src compatibleMethods.ts

96.77% Statements 30/31
71.42% Branches 5/7
100% Functions 5/5
96% Lines 24/25

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    1x 1x 1x                         1x               885x 95x 95x       95x 95x       95x               95x 95x   30x     65x     65x   65x     55x   55x       177x 177x 177x 177x 177x     1x  
// 兼容以前的老项目写法
 
import { addPendingRequest, checkIfHasPendingRequest, clearPendingRequest, getCachedRequest } from './duplicateRequestPrevent'
import { normalizeConfig } from './helpers'
import { generateRequestHash } from './utils'
import type {
  AjaxInstance, AjaxOption, AnyObj, AxiosMethods
} from './types'
 
export type OldAjaxMethods = 'query' | 'create' | 'deletes' | 'putWay' | 'patchWay'
 
export type CustomAjaxMethods<T extends object, GDataShape extends AnyObj = any> = <DataType = GDataShape>(path: string, config?: Partial<AjaxOption & T>) => (data?: any, expend?: string) => Promise<DataType>
 
type MethodMap = {
  [k in OldAjaxMethods]: AxiosMethods
}
 
const methodMap: MethodMap = {
  query: 'GET',
  create: 'POST',
  putWay: 'PUT',
  deletes: 'DELETE',
  patchWay: 'PATCH'
}
 
export const generateCustomMethod = <T extends object>(ctx: AjaxInstance<T>, name: OldAjaxMethods): CustomAjaxMethods<T> => <DataType = any>(path: string, config: Partial<AjaxOption | T> = {}) => async (data?: any, expand?: string) => {
  const url = expand ? `${path}/${expand}` : path;
  Iif (expand) {
    // eslint-disable-next-line no-console
    console.warn(`业务代码中拼接url发送请求的方式即将废弃,请使用ajax.${methodMap[name].toLocaleLowerCase()}接口替换。相关url:${url}`)
  }
  const [newParams, newData] = methodMap[name] === 'GET' ? [data, undefined] : [undefined, data]
  const config1 = normalizeConfig(ctx, {
    ...config, url, params: newParams, data: newData, method: methodMap[name],
  });
 
  const options = {
    ...config1,
    custom: {
      ...ctx.$options,
      ...config1,
    },
  };
 
  const hashKey = generateRequestHash(config1)
  if (checkIfHasPendingRequest(hashKey)) {
    // 重复,且处于pending状态的请求,复用
    return getCachedRequest(hashKey)
  }
 
  const request = ctx.request<any, DataType>(options);
 
  // 缓存请求
  addPendingRequest(hashKey, request, options.reUseDuration)
 
  const res = await request
 
  // 清理缓存的请求
  clearPendingRequest(hashKey)
 
  return res
};
 
function addCompatibleMethods<CusOpts extends NonNullable<unknown>>(ctx: AjaxInstance<CusOpts>) {
  ctx.query = generateCustomMethod<CusOpts>(ctx, 'query');
  ctx.create = generateCustomMethod<CusOpts>(ctx, 'create');
  ctx.deletes = generateCustomMethod<CusOpts>(ctx, 'deletes');
  ctx.putWay = generateCustomMethod<CusOpts>(ctx, 'putWay');
  ctx.patchWay = generateCustomMethod<CusOpts>(ctx, 'patchWay');
}
 
export default addCompatibleMethods;