All files / src/impl ContractFactory.impl.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 5/5
100% Lines 10/10

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 4625x   25x                   25x 65x               25x 65x                   65x 22x   43x               65x    
import { create as createBasicContract } from "@jonloucks/contracts-ts/api/BasicContract";
import { Config, Contract } from "@jonloucks/contracts-ts/api/Contract";
import { create as createRatifiedContract, isRatifiableConfig } from "@jonloucks/contracts-ts/api/RatifiedContract";
import { OptionalType, RequiredType } from "@jonloucks/contracts-ts/api/Types";
 
import { ContractFactory } from "@jonloucks/contracts-ts/api/ContractFactory";
 
/**
 * Factory method to create a ContractFactory
 * 
 * @returns the new ContractFactory implementation
 */
export function create(): RequiredType<ContractFactory> {
  return ContractFactoryImpl.internalCreate();
}
 
/**
 * Factory method to create a Contract
 * @param config optional Contract configuration
 * @returns the new Contract implementation
 */
export function createContract<T>(config?: OptionalType<Config<T>>): RequiredType<Contract<T>> {
  return create().createContract<T>(config);
}
 
// ---- Implementation details below ----
 
/**
 * An implementation of ContractFactory
 */
class ContractFactoryImpl implements ContractFactory {
  createContract<T>(config?: OptionalType<Config<T>>): RequiredType<Contract<T>> {
    if (config?.ratified === true || isRatifiableConfig<T>(config)) {
      return createRatifiedContract<T>(config);
    } else {
      return createBasicContract<T>(config);
    }
  }
 
  private constructor() {
  }
 
  static internalCreate(): RequiredType<ContractFactory> {
    return new ContractFactoryImpl();
  }
};