contracts-ts

contracts-ts Documentation

This document is the primary written guide for using and contributing to @jonloucks/contracts-ts.

Overview

@jonloucks/contracts-ts provides TypeScript contracts and promisor patterns to support dependency inversion, runtime validation, and controlled lifecycle management.

Key goals:

Installation

npm install @jonloucks/contracts-ts

Node.js Support

Supported Node.js versions for development and CI are:

Quick Start

1) Import core APIs

import { createContract } from "@jonloucks/contracts-ts";
import { bind, createSingleton, enforce, guardFunctions } from "@jonloucks/contracts-ts/api/Convenience";

2) Define a contract

interface Logger {
  log(message: string): void;
}

const LOGGER = createContract<Logger>({
  name: "Logger",
  test: (obj: unknown): obj is Logger => guardFunctions(obj, "log")
});

3) Bind and use

bind(LOGGER, createSingleton(() => ({
  log: (message: string) => console.log(message)
})));

const logger = enforce(LOGGER);
logger.log("Hello contracts-ts");

Import Paths (v2.0.0)

v2.0.0 intentionally narrows root exports.

Root package (@jonloucks/contracts-ts)

Use for essential core exports:

Convenience packages

Use convenience modules for broader helpers:

Explicit subpath imports

Use explicit modules for focused dependencies:

Core Concepts

Contract

Defines the shape and runtime validator for a dependency.

Promisor

Provides the implementation deliverable for a contract.

Binding

Associates a contract with a promisor using bind strategies.

Enforce vs Claim

Common Patterns

Singleton

Use createSingleton for one shared implementation instance.

Lifecycle

Use createLifeCycle when open/close behavior must be coordinated.

Extractor

Use createExtractor to map a promisor deliverable into another value.

Migration Notes for v2.0.0

Breaking changes

Replacements

Development Workflow

npm install
npm run build
npm test
npm run lint
npm run docs

Additional References

Governance and Contribution