withInjections.ts
Purpose
This module implements the withInjections() extension, which resolves a set of dependencies from a ditox container and exposes them in controller context as deps.
Overview
withInjections() is used on top of withContainer() or alongside a container provider. The caller describes an object of tokens, and the extension returns a value object with the same shape, available in the controller or view-model factory.
Conceptual Architecture
The module has three main parts:
- Types
DependencyProps,DependencyTokenProps<Props>, andDependencyContext<Dependencies>. - Extraction of the container from extension params through
DITOX_EXTENSION_CONTAINER_KEY. - Resolution of all tokens through
resolveValues(container, tokens)and exposure of the result in thedepsfield.
If the container is missing, the module throws ControllerConstructorError.
Public API Description
DependencyProps
- Base shape of a dependency object.
DependencyTokenProps<Props>
- Maps a dependency shape into a
ditoxtoken shape.
DependencyContext<Dependencies>
- Controller context extended with the
depsfield.
withInjections<TSourceContext, Dependencies>(tokens): ExtensionFn<...>
tokens: object of tokens to resolve from the container.- Returns an extension that adds
depsto controller context.
Usage Examples
ts
import { declareController } from '@nrgyjs/core';
import { token } from 'ditox';
import { withInjections } from '@nrgyjs/ditox';
const API_URL = token<string>();
const Controller = declareController()
.extend(withInjections({ apiUrl: API_URL }))
.apply(({ deps }) => ({
getApiUrl: () => deps.apiUrl,
}));