All files / src/api Contract.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 1121x                                                                                                                                                                                                               1x 19x            
import { guardFunctions, OptionalType, RequiredType } from "@jonloucks/contracts-ts/api/Types";
 
/**
 * Configuration for a Contract
 * 
 * @param <T> The Contract deliverable type
 */
export interface Config<T> {
 
  /**
   * the predefine test to check if instance is of type T
   * @param instance the instance to check
   * @returns true if the instance is of type T, false otherwise
   */
  test?: (instance: unknown) => instance is RequiredType<T>;
 
  /**
   * User defined name for this contract.
   * Note: Do not rely on this being a java class name
   *
   * @return the type name
   */
  name?: string;
 
  /**
   * The type of deliverable for this contract.
   * Note: Do not rely on this being a java class name
   *
   * @return the type name, null is illegal
   */
  typeName?: string;
 
  /**
   * When replaceable a new binding can replace in an existing one
   * The default is false
   *
   * @return true if replaceable
   */
  replaceable?: boolean;
 
  /**
   * When ratified is true the Contract is a RatifiedContract
   * and must comply with RatifiedContract rules.
   */
  ratified?: boolean;
 
  /**   
   * When guarded is true the Contract throws exception if deliverable is null or undefined
   * The default is true.
   */
  guarded?: boolean;
}
 
/**
 * A Contract defines a deliverable type by name and type.
 * Note: This is a final class and should not be extended!
 * @param <T> the type of deliverable for this Contract
 */
export interface Contract<T> {
 
  /**
  * Casts the given object to the return type for this Contract
  * This is used to make sure the value is a checked value and does not sneak passed during erasure
  *
  * @param value the value to cast
  * @return the checked value. Note: null is possible. The Promisor is allowed to return null
  * @throws ContractException if the value can't be cast to the return type.
  */
  cast(value: OptionalType<unknown>): OptionalType<T>;
 
  /**
   * @return the contract name
   */
  get name(): string;
 
  /**
   * Note: Do not rely on this being a java class name
   * Note: The actual class is never exposed and is by design.
   *
   * @return the type of deliverable for this contract.
   */
  get typeName(): string;
 
  /**
   * When replaceable a new binding can replace in an existing one
   * The default is false
   *
   * @return true if replaceable
   */
  get replaceable(): boolean;
 
  /**
   * When guarded is true the Contract throws exception if deliverable is null or undefined
   * The default is true.
   */
  get guarded(): boolean;
}
 
/**
 * Duck-typing check for Contract interface.
 *
 * @param instance the instance to check
 * @returns true if the instance implements Contract, false otherwise
 */
export function guard<T>(instance: unknown): instance is RequiredType<Contract<T>> {
  return guardFunctions(instance, 'cast', 'name', 'typeName', 'replaceable');
}