All files / src/api Contracts.ts

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4

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  1x       1x                                                                                                                                                                               1x 36x  
import { AutoClose } from "@jonloucks/contracts-ts/api/AutoClose";
import { Open, guard as openGuard } from "@jonloucks/contracts-ts/api/Open";
import { BindStrategyType as BindType } from "@jonloucks/contracts-ts/api/BindStrategy";
import { Contract } from "@jonloucks/contracts-ts/api/Contract";
import { PromisorType } from "@jonloucks/contracts-ts/api/Promisor";
import { guardFunctions, OptionalType, RequiredType } from "@jonloucks/contracts-ts/api/Types";
 
/**
 * The Contracts configuration
 */
export interface Config {
  /**
   * @return if true, only ratified contracts can be used
   */
  ratified?: boolean;
 
  /**
   * @return optional partners. Aggregated Contracts
   */
  partners?: Contracts[]
 
  /**
   * an optional list of events that when fired will shutdown Contracts
   * the default is no events.
   * It can be a custom event or system events like 'beforeExit', 'SIGINT', 'SIGTERM'
   * @return list of shutdown event names
   */
  shutdownEvents?: string[];
}
 
/**
 * The actual implementation used for Contracts itself.
 * It is used to bootstrap Contracts without it knowing the implementation if Contracts.
 * It does know how to load the default from contracts-impl.
 * However, the design is open to have it replaced with an alternative implementation.
 */
export interface Contracts extends Open {
 
  /**
   * Claim the deliverable from a bound contract.
   *
   * @param contract the contract to claim
   * @param <T>      type of value returned
   * @return the value returned by the bound Promisor. A Promisor can return null
   * @throws ContractException if Promisor binding does not exist for the contract
   * @throws SecurityException if permission is denied
   * @throws IllegalArgumentException may throw when an argument is null
   */
  claim<T>(contract: Contract<T>): OptionalType<T>;
 
  /**
   * Enforce the deliverable from a bound contract.
   *
   * @param contract the contract to claim
   * @param <T>      type of value returned
   * @return the value returned by the bound Promisor, but never null or undefined
   * @throws ContractException if Promisor binding does not exist for the contract, or the deliverable is null
   * @throws SecurityException if permission is denied
   * @throws IllegalArgumentException may throw when an argument is null
   */
  enforce<T>(contract: Contract<T>): RequiredType<T>;
 
  /**
   * Checks if the contract is bound to a Promisor
   *
   * @param contract the contract to check
   * @param <T>      The type of the value returned by the promisor
   * @return true iif bound
   * @throws IllegalArgumentException may throw when an argument is null
   */
  isBound<T>(contract: Contract<T>): boolean;
 
  /**
   * Establish a binding between a Contract and a Promisor
   *
   * @param contract the contract to bind the Promisor
   * @param promisor the Promisor for the given contract
   * @param bindStrategy the binding strategy
   * @param <T>      The type of the value returned by the promisor
   * @return Use to release (unbind) this contract
   * @throws ContractException when contract is already bound, can't be replaced or not accepting bindings
   * @throws SecurityException when permission to bind is denied
   * @throws IllegalArgumentException may throw when an argument is null
   */
  bind<T>(contract: Contract<T>, promisor: PromisorType<T>, bindStrategy?: BindType): AutoClose;
}
 
/**
 * Type guard for Contracts
 *
 * @param value the value to check
 * @return true if value is Contracts, false otherwise
 */
export function guard(value: unknown): value is RequiredType<Contracts> {
  return guardFunctions(value, 'claim', 'enforce', 'isBound', 'bind') && openGuard(value);
}