hex-di
v0.3.0
Published
Type-safe dependency injection with hexagonal architecture — umbrella package for the full HexDI stack
Maintainers
Readme
hex-di
Type-safe dependency injection with hexagonal architecture.
Single-package install for the full HexDI stack.
Installation
pnpm add hex-di
# or
npm install hex-di
# or
yarn add hex-diQuick Start
import { port, createAdapter, GraphBuilder, createContainer } from "hex-di";
// 1. Define ports (interfaces)
interface Logger {
log(message: string): void;
}
interface Database {
query(sql: string): Promise<unknown[]>;
}
const LoggerPort = port<Logger>()({ name: "Logger" });
const DatabasePort = port<Database>()({ name: "Database" });
// 2. Define adapters (implementations)
const loggerAdapter = createAdapter({
provides: LoggerPort,
requires: [],
lifetime: "singleton",
factory: () => ({ log: msg => console.log(msg) }),
});
const databaseAdapter = createAdapter({
provides: DatabasePort,
requires: [LoggerPort],
lifetime: "singleton",
factory: ({ Logger }) => ({
query: async sql => {
Logger.log(`Executing: ${sql}`);
return [];
},
}),
});
// 3. Build the dependency graph (validated at compile time)
const graph = GraphBuilder.create().provide(loggerAdapter).provide(databaseAdapter).build();
// 4. Create the container and resolve services
const container = createContainer({ graph, name: "App" });
const db = container.resolve(DatabasePort);
await db.query("SELECT * FROM users");What's included
This package re-exports the complete public API of:
| Package | What it provides |
| ------------------------------- | --------------------------------------------------------------------- |
| @hex-di/core | port, createAdapter, lazyPort, error classes, utilities |
| @hex-di/graph | GraphBuilder, graph inference types, build errors |
| @hex-di/runtime | createContainer, Container, Scope, resolution hooks, inspection |
Selective imports
Install individual packages if you only need part of the stack — for example, library authors who only define ports and adapters only need @hex-di/core:
pnpm add @hex-di/core