API Reference
Complete API documentation for Craft.
Core Functions
The main functions for creating and managing immutable state updates.
craft()- Create immutable state updatescreateDraft()- Create a draft manuallyfinishDraft()- Finalize a draftnothing- Delete properties or array elements
Composition
Utilities for composing and chaining state updates.
crafted()- Create curried updater functionscompose()- Combine multiple producerspipe()- Apply producers sequentiallycomposer()- Fluent composition API
Introspection
Functions for inspecting draft state.
isDraft()- Check if value is a draftcurrent()- Get immutable snapshotoriginal()- Get original value
Patches
JSON Patch support (RFC 6902) for tracking state changes.
craftWithPatches()- Generate patchesapplyPatches()- Apply patches to state
Debugging
Development utilities for debugging draft state.
inspectDraft()- Inspect draft metadatavisualizeDraft()- Visualize draft treeassertDraft()- Assert draft statusassertNotDraft()- Assert not draftdescribeDraft()- Human-readable descriptiongetDraftTreeSummary()- Draft tree summaryenableDebugMode()- Enable debug modedisableDebugMode()- Disable debug modeisDebugEnabled()- Check debug statusgetDebugConfig()- Get debug config
Configuration
Functions for configuring Craft's behavior.
setAutoFreeze()- Control auto-freezesetUseStrictShallowCopy()- Strict shallow copysetCustomShallowCopy()- Custom cloning logic
Utilities
Helper functions and type utilities.
freeze()- Manually freeze objectscastDraft()- Cast to draft typecastImmutable()- Cast to immutable type
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
- Core Functions - Main API functions
- Composition - Composition utilities
- Debugging - Debug utilities
- Examples - Real-world examples