All files / src/utils checkGlobalConfig.ts

100% Statements 15/15
90% Branches 9/10
100% Functions 3/3
100% Lines 15/15

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 653x               3x     8x 4x               8x 3x               8x 2x               8x 6x             6x 8x 8x           8x 9x                  
import {
  invariant,
  isPlainObject,
  isString,
  isArray,
  isFunction
} from "mux-lib";
 
export const checkGlobalConfig = config => {
  // prefix 只能为 undefined 和 string
 
  if (config.prefix !== undefined) {
    invariant(
      isString(config.prefix),
      `[fetch] prefix should be string , but got ${typeof config.prefix}`
    );
  }
 
  // fetchOption 只能为 undefined 和 object
 
  if (config.fetchOption) {
    invariant(
      isPlainObject(config.fetchOption),
      `[fetch] fetchOption should be plain object , but got ${typeof config.fetchOption}`
    );
  }
 
  // responseHandle 只能为 undefined 和 function
 
  if (config.responseHandle) {
    invariant(
      isFunction(config.responseHandle),
      `[fetch] responseHandle should be function , but got ${typeof config.prefix}`
    );
  }
 
  // interceptor 只能为 undefined 和 object
 
  if (config.interceptor) {
    invariant(
      isPlainObject(config.interceptor),
      `[fetch] interceptor should be plain object , but got ${typeof config.interceptor}`
    );
 
    // 每一种的 interceptor 只能为 undefined 和 Array<Function>
 
    Object.keys(config.interceptor).forEach(key => {
      Eif (config.interceptor[key]) {
        invariant(
          isArray(config.interceptor[key]),
          `[fetch] each interceptor should be array , but got ${typeof config
            .interceptor[key]}`
        );
 
        config.interceptor[key].forEach(v => {
          invariant(
            isFunction(v),
            `[fetch] each interceptor should be Array<Function> , but got ${typeof v}`
          );
        });
      }
    });
  }
};