Skip to main content
Skip to main content

API Reference

Complete API documentation for HexDI packages.

Packages

Quick Reference

Creating Services

// 1. Define a contract
const LoggerPort = port<Logger>()({ name: "Logger" });

// 2. Declare an implementation with dependencies
const LoggerAdapter = createAdapter({
provides: LoggerPort,
requires: [],
lifetime: "singleton",
factory: () => ({ log: console.log }),
});

// 3. Build a structurally validated graph
const graph = GraphBuilder.create().provide(LoggerAdapter).build();

// 4. Resolve services
const container = createContainer({ graph, name: "App" });
const logger = container.resolve(LoggerPort);

Type Utilities

UtilityPackagePurpose
InferService<P>coreExtract service type from port
InferPortName<P>coreExtract port name as literal type
InferPortDirection<P>coreExtract port direction
InferAdapterProvides<A>graphExtract provided port from adapter
InferAdapterRequires<A>graphExtract required ports from adapter
InferContainerProvides<C>runtimeExtract all ports from container
ServiceFromContainer<C, P>runtimeGet service type for a port

Error Classes

ErrorCodeWhen
CircularDependencyErrorCIRCULAR_DEPENDENCYCycle detected in the graph
FactoryErrorFACTORY_FAILEDAdapter factory threw
DisposedScopeErrorDISPOSED_SCOPEResolution after scope disposed
ScopeRequiredErrorSCOPE_REQUIREDScoped service resolved at root

Package Dependencies

@hex-di/core (zero dependencies — foundation)

@hex-di/graph (depends on core)

@hex-di/runtime (depends on core, graph)

├── @hex-di/react (depends on core, runtime)
└── @hex-di/testing (depends on core, graph, runtime)

@hex-di/result (zero dependencies — standalone)

The four core packages are available from the hex-di umbrella:

import { port, createAdapter, GraphBuilder, createContainer } from "hex-di";

@hex-di/result is a separate package (not bundled in hex-di):

import { ok, err, type Result } from "@hex-di/result";