Fundamentals
Custom providers
Supply a value, build one with a function, or bind an interface to a class.
Normally you write @Injectable class UserService and Kest creates it. Sometimes that is not enough: you need to hand over a ready-made value (a configuration object), build something with logic (a connection that needs a URL from the config), or let code ask for an interface while a specific class answers. These are custom providers.
| You want | NestJS | Kest |
|---|---|---|
| a ready value | useValue | provide(value) |
| build it with a function (and the container) | useFactory | provide<T> { r -> ... } |
| the same object under another type | useExisting | alias<A, B>() |
| a class answering an interface | useClass | @Injectable(binds = [Iface::class]) |
| build it asynchronously | async useFactory | provideAsync<T> { } / provideSuspend<T> { } (Asynchronous providers) |
You add them where the application is started, in the Kest.run block (or, inside your own module, with DynamicModule.create):
Kest.run<AppModule> {
provide(AppSettings(region = "eu", debug = false)) // a value
provide<Mailer> { r -> Mailer(r.get(ConfigService::class).getOrThrow<String>("smtp.host")) } // a factory: r is the container
alias<Notifier, EmailNotifier>() // anyone asking for Notifier gets the EmailNotifier
}@Injectable class Welcome(private val mailer: Mailer, private val settings: AppSettings) // injected like any providerMark a type you supply this way with @Provided, so the build knows it exists even though no module lists it:
@Provided data class AppSettings(val region: String, val debug: Boolean)A class behind an interface
The most common "custom" need needs no code at all. Depend on the interface, and say which class implements it:
interface PaymentGateway { fun charge(cents: Long): Receipt }
@Injectable(binds = [PaymentGateway::class])
class StripeGateway : PaymentGateway { ... }
@Injectable class Checkout(private val gateway: PaymentGateway) // gets the StripeGatewayTo swap it (tests, another provider) change one line. In a test: overrideProvider(PaymentGateway::class, FakeGateway()) (Testing).
Tokens: injecting plain values
Strings and numbers have no useful type to ask for, so give them a token, a name that is also a type-safe key:
val API_URL = Token<String>("api.url")
Kest.run<AppModule> { provide(API_URL) { "https://api.example.com" } }
@Injectable class Client(@Inject("api.url") private val url: String)Factories that read configuration
A factory receives the container (r) so it can ask for other providers. It runs once, at start-up, in dependency order:
provide<HttpClientPool> { r ->
val cfg = r.get(ConfigService::class)
HttpClientPool(maxConnections = cfg.get<Int>("http.pool") ?: 20, baseUrl = cfg.getOrThrow<String>("upstream.url"))
}When to use which
- Interface +
binds: the default for anything with more than one implementation. provide(value): a plain configuration object you already built.provide { r -> }: needs other providers or logic to be created.- A token: a loose value (a URL, a flag).
- Inside a reusable module, put the same calls in
DynamicModule.createso users justimport(MyModule.forRoot()).