All files / src blockFetch.ts

83.33% Statements 25/30
75% Branches 9/12
100% Functions 2/2
82.76% Lines 24/29

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 693x 3x 3x   3x   3x   3x                 3x                       3x 2x       2x 2x                   3x 1x 1x 1x     3x 2x   1x 1x         3x 3x 3x 3x 3x        
import { fetchWebapi, fastFetchFactory } from "./fetch";
import { FORM_DATA_TYPE } from "./constant";
import blockConfigMap from "./blockConfig";
import { GlobalConfig } from "./@types/";
import { invariant, isString } from "mux-lib";
 
export const fetch = (...args) => {
  let namespace, api, options;
  Iif (!args.length) {
    invariant(false, "[fetch] fetch need one argument at least");
  }
  /**
   * 第一种,简单的调用接口
   * @example
   * fetch(api)
   */
 
  Iif (args.length === 1) {
    api = args[0];
    options = {};
  }
 
  /**
   * 第二种,调用接口带参数
   * @example
   * 1. fetch(api,options)
   * 2. fetch(namespace,api)
   *  */
 
  if (args.length === 2) {
    Iif (isString(args[1])) {
      namespace = args[0];
      api = args[1];
    } else {
      api = args[0];
      options = args[1];
    }
  }
 
  /**
   * 第三种,调用接口加上区块的参数
   * @example
   * fetch(namespace,api,options)
   */
 
  if (args.length === 3) {
    namespace = args[0];
    api = args[1];
    options = args[2];
  }
 
  if (!namespace) {
    return fetchWebapi(api, options);
  } else {
    const getConfig = (): GlobalConfig => blockConfigMap.get(namespace);
    return fetchWebapi(api, options, getConfig);
  }
};
 
// 快捷方法
fetch.get = fastFetchFactory(fetch, { method: "GET" });
fetch.post = fastFetchFactory(fetch, { method: "POST" });
fetch.put = fastFetchFactory(fetch, { method: "PUT" });
fetch.delete = fastFetchFactory(fetch, { method: "DELETE" });
fetch.upload = fastFetchFactory(fetch, {
  method: "POST",
  headersType: FORM_DATA_TYPE
});