Skip to main content

API Reference

service(name).request<T>()

Creates a service for a value from the user's request (request params, cookies, etc.) with type T.

const $session = service("session").request<Session>()

service(name).app(config)

Creates an app service.

App services are automatically prepared when declared: typectx validates the dependency graph and builds a reusable assembly blueprint up front. This preparation step does not run factory or init yet.

const $product = service("product").app({
services: [$service1, $service2], // Services
lazy: boolean, // Eager (false) or lazy (true)
init: (value, deps) => void, // Run a function right after construction
factory: (deps, ctx) => {
// Factory function
// deps = dependencies received, can be destructured
// ctx = Function to contextualize services used in the factory
return serviceImplementation
}
})

$service.pack(value)

Provides a concrete value for a service, bypassing the factory in the case of app services.

const requestData = $requestService.pack(value)
const mockProduct = $appService.pack(mockValue) // For testing

$service.assemble(supplies)

Resolves all dependencies and creates the service's product.

During assembly, typectx automatically preserves already-prepared app services when they are still valid for the new context, and only rebuilds branches invalidated by new request supplies or hire(...).

const supply = $product.assemble(suppliesObject)
const product = supply.unpack()

$service.mock(options)

Creates an alternative implementation.

const $alternative = $originalService.mock({
services: [$differentDeps],
factory: (deps, ctx) => {
/*... alternativeImplementationBody*/
}
})

$service.hire(...hiredServices)

Composition root method to wire additional services. Hired services replace originals with the same name across the entire dependency chain.

const $modified = $originalService.hire($mockService)

You can also hire additional original services to batch-assemble multiple app services together. You access these resolved values through the returned supply's deps.

const ASupply = $A.hire($B, $C).assemble({})
// All assembled products will be available in $A's deps (see below)
const A = ASupply.unpack()
const B = ASupply.deps.B
const C = ASupply.deps.C

supply.deps

Access resolved dependencies from outside a factory. See example above in $service.hire() section. TypeScript only exposes dependencies it can prove are present for that assembled service context.

index(...supplies)

Utility to convert supply array to indexed object.

const suppliesObject = index(supply1, supply2, supply3)
// Equivalent to: { [supply1.service.name]: supply1, [supply2.service.name]: supply2, ... }

Factory Function (factory)

The factory function is where your service logic lives. It receives two arguments:

  • deps: An object of dependencies of the form: {[service.name]: value}
  • ctx($service): A function to access contextualized services in a factory and reassemble them with additional or overridden request data.