Skip to content

Examples

Three small packages under examples/, each showing a different job @btravstack/di does — and, at the same time, exercising the library end to end from a consumer's own workspace.

PackageShows
Hexagonal order APIThe core story: ports named by the application, a private internal beside a public surface, and one application module composed against a production adapter and an in-memory one.
Request scopeLifetime management: a pool acquired once under Module.scoped, and a Module.forkScope'd transaction per request over the built parent.
Plugin registryMulti-binding: a Port.many set port fed by contributions from two independent modules, collected and run together.

Why these are tests, not just illustrations

Unlike the fenced snippets in the guide, this code compiles and its specs run in CI. Each src/index.ts reads as application code, not as a test fixture — but its src/index.spec.ts asserts real behaviour: values returned through a use case, the exact order resources release in, contributions actually accumulating.

Where a guarantee is compile-time only — an unexported port is unnameable outside its module, a resourceful graph cannot be built with Module.build — the assertion is a @ts-expect-error in a *.test-d.ts file instead. Proving those at runtime would mean either asserting a falsehood (the built context genuinely is flat, so the "private" port really is in it) or leaking a resource nothing would release (calling Module.build for real on a graph that needs a scope).

Nothing here is published: every package is private, depends on @btravstack/di via workspace:*, and declares unthrown itself — a peer dependency is the consumer's to install, and these packages are consumers.

Running them

sh
git clone https://github.com/btravstack/di.git && cd di
pnpm install
pnpm test        # every example's specs, alongside the library's own
pnpm typecheck   # includes the @ts-expect-error assertions

Released under the MIT License.