Getting Started
Quick Start in 4 Steps
Get up and running with typectx in just a few minutes.
1. Installation
npm install typectx
2. Create a Market
A market is a namespace where your suppliers are defined. You'll usually create one per application.
import { createMarket } from "typectx"
const market = createMarket()
3. Define Suppliers
Suppliers create your app's dependencies. Resources hold data (like config or user sessions), while Products are services that can depend on other suppliers.
// A Resource supplier for the user session
const $session = market.offer("session").asResource<{ userId: string }>()
// A Product supplier that depends on the session
const $userService = market.offer("userService").asProduct({
suppliers: [$session],
// Access the session by destructuring the factory's 1st argument.
// The property name is the name passed to market.offer() during supplier creation.
factory: ({ session }) => {
return {
id: session.userId,
name: "Jane Doe"
}
}
})
4. Assemble at Your Entry Point
At your application's entry point, assemble your main product, providing any required resources. The index() utility simplifies this process.
import { index } from "typectx"
const session = { userId: "user-123" }
// Assemble the user service with a concrete session
const userService = $userService
.assemble(index($session.pack(session)))
.unpack()
console.log(userService.id) // "user-123"
Next Steps
- Walk through a Basic Example of a complete application.
- Follow the more in-depth Basic Usage guide.
- Dive into the Design philosophy and semantics of typectx.