All files / src/utils configFactory.ts

100% Statements 15/15
90.48% Branches 19/21
100% Functions 2/2
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  3x 3x 3x 3x 3x         10x     10x     10x             10x   30x 30x 30x     30x 30x         10x              
import { FetchConfig, GlobalConfig } from "../@types/";
import { compose } from "./compose";
import globalConfig from "../globalConfig";
import { isArray } from "mux-lib";
import { DEFAULT_CONFIG } from "../constant";
export const configFactory = (
  config: FetchConfig,
  base: FetchConfig = globalConfig.get("config") || DEFAULT_CONFIG
): GlobalConfig => {
  // 合并 prefix
  const prefix = config.prefix || base.prefix;
 
  // 合并 responseHandle
  const responseHandle = config.responseHandle || base.responseHandle;
 
  // 合并 fetchOption
  const fetchOption = Object.assign(
    {},
    base.fetchOption || {},
    config.fetchOption || {}
  );
 
  // 合并 interceptor
  const interceptor = Object.keys(DEFAULT_CONFIG.interceptor).reduce(
    (p, key) => {
      const configInterceptor = (config.interceptor || {})[key] || [];
      const baseInterceptor = (base.interceptor || {})[key] || [];
      const baseKeyInterceptor = isArray(baseInterceptor)
        ? baseInterceptor
        : [baseInterceptor];
      p[key] = compose(...baseKeyInterceptor, ...configInterceptor);
      return p;
    },
    {}
  );
 
  return {
    prefix,
    fetchOption,
    responseHandle,
    interceptor
  };
};