All files / src configFactory.ts

53.33% Statements 8/15
0% Branches 0/6
25% Functions 1/4
50% Lines 7/14

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        1x 2x 2x   2x                       2x       2x       2x    
import { Config } from './types';
 
export type ConfigFunction = (arg0: keyof Config) => any; // TODO: refactor configFactory function to allow for more precise typing
 
export const configFactory = () => {
  const settings: Partial<Config> = {};
  let currentProvider: ConfigFunction | null = null;
 
  const config = function getConfig(key: keyof Config) {
    if (settings && settings[key]) {
      return settings[key];
    }
 
    if (!currentProvider) {
      throw new Error('A configuration provider has not been set');
    }
 
    return currentProvider(key);
  };
 
  config.setProvider = function setProvider(providerFunction: ConfigFunction) {
    currentProvider = providerFunction;
  };
 
  config.setValue = function setValue(key: keyof Config, value: any) {
    settings[key] = value;
  };
 
  return config;
};