Skip to content

API Reference

Complete API documentation for Craft.

Core Functions

The main functions for creating and managing immutable state updates.

Composition

Utilities for composing and chaining state updates.

Introspection

Functions for inspecting draft state.

Patches

JSON Patch support (RFC 6902) for tracking state changes.

Debugging

Development utilities for debugging draft state.

Configuration

Functions for configuring Craft's behavior.

Utilities

Helper functions and type utilities.

Type Exports

typescript
import type {
  Draft,
  Immutable,
  Producer,
  Patch,
  PatchListener,
} from "@sylphx/craft";

Quick Reference

Basic Usage

typescript
import { craft } from "@sylphx/craft";

const next = craft(state, draft => {
  draft.value = newValue;
});

Curried Updates

typescript
import { crafted } from "@sylphx/craft";

const increment = crafted((draft: State) => {
  draft.count++;
});

const next = increment(state);

Composition

typescript
import { compose, pipe } from "@sylphx/craft";

// Combine producers
const next = craft(state, compose(producer1, producer2));

// Sequential application
const next = pipe(state, producer1, producer2);

Patches

typescript
import { craftWithPatches, applyPatches } from "@sylphx/craft";

const [next, patches, inversePatches] = craftWithPatches(state, draft => {
  draft.value = newValue;
});

const recreated = applyPatches(state, patches);
const reverted = applyPatches(next, inversePatches);

Introspection

typescript
import { isDraft, current, original } from "@sylphx/craft";

craft(state, draft => {
  console.log(isDraft(draft)); // true
  console.log(current(draft)); // immutable snapshot
  console.log(original(draft)); // original value
});

Next Steps

Released under the MIT License.