All files / src fetch.ts

100% Statements 27/27
88.89% Branches 8/9
100% Functions 4/4
100% Lines 25/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 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 1253x 3x 3x         30x         2x                         11x   3x                       12x         12x       12x         12x         12x 12x             12x           12x   12x           11x                   11x               11x   1x                               3x 3x 3x 3x 3x        
import defaultFetch from "isomorphic-fetch";
import { FORM_DATA_TYPE, URL_HTTP_REG } from "./constant";
import globalConfig from "./globalConfig";
import { FastOptions, FetchOptions, GlobalConfig } from "./@types/";
/**
 * 默认加上请求方法,第二个参数约定为请求参数
 */
export const fastFetchFactory = (fetch: any, opts: FastOptions) => (
  api: string,
  params: any = {},
  ...args
) =>
  fetch(
    api,
    {
      body: params || {},
      ...opts
    },
    ...args
  );
 
/**
 * 默认的配置项
 */
 
const createDefaultConfig = (): GlobalConfig => globalConfig.get("config");
 
export const fetchWebapi = async (
  api: string,
  opts: FetchOptions = {},
  getConfig = createDefaultConfig
) => {
  /**
   * getConfig 必须在运行时调用,才能保证获取的配置是最新的
   * @returns interceptor
   * @returns prefix
   * @returns fetchOption
   */
 
  const { interceptor, prefix, fetchOption, responseHandle } = getConfig();
  const {
    requestInterceptor,
    responseInterceptor,
    errorInterceptor
  } = interceptor;
 
  let url, options, startTime, endTime;
 
  try {
    /**
     * 请求拦截器
     */
 
    const request = requestInterceptor({
      api: URL_HTTP_REG.test(api) ? api : `${prefix}${api}`,
      options: Object.assign({}, fetchOption, opts)
    });
 
    url = request.api;
    options = request.options;
 
    /**
     * 如果没有,默认使用fetch
     * 要求必须是返回promise的方法
     */
 
    const fetch = options.fetch || defaultFetch;
 
    /**
     * 打印请求开始时间
     */
 
    startTime = Date.now();
 
    const response = await fetch(url, options);
 
    /**
     * 打印请求结束时间
     */
 
    endTime = Date.now();
 
    /**
     * 返回响应拦截器的值
     *
     * @example
     * const responseInterceptor = res =>res.clone().json()
     *
     */
 
    const res = await responseHandle({
      response,
      options: {
        startTime,
        endTime
      }
    });
 
    return responseInterceptor(res);
  } catch (error) {
    errorInterceptor({
      error,
      options: {
        type: "fetch",
        url,
        args: {
          options,
          startTime,
          endTime
        }
      }
    });
  }
};
 
// 快捷方法
fetchWebapi.get = fastFetchFactory(fetchWebapi, { method: "GET" });
fetchWebapi.post = fastFetchFactory(fetchWebapi, { method: "POST" });
fetchWebapi.put = fastFetchFactory(fetchWebapi, { method: "PUT" });
fetchWebapi.delete = fastFetchFactory(fetchWebapi, { method: "DELETE" });
fetchWebapi.upload = fastFetchFactory(fetchWebapi, {
  method: "POST",
  headersType: FORM_DATA_TYPE
});