Skip to content
GitHub

FactoryProvider

interface in meocord/interface and meocord/testing Since 4.1.0

interface FactoryProvider<T = any>

A provider that binds what a function returns, called once with the values of inject, in order.

Use it for a value that takes work to make, such as a database connection. factoryProvider types the function's parameters from inject, which a plain object in a list cannot.

Examples

TypeScript
class Database {
  constructor(readonly url: string) {}
}
export const DATABASE = createToken<Database>('Database')

const database: FactoryProvider<Database> = { provide: DATABASE, useFactory: () => new Database(process.env.DATABASE_URL!) }

Members

provide

provide: ProviderToken<T>

The token classes inject the value by.

useFactory

useFactory: (...args: any[]) => T | Promise<T>

Makes the value, from the values of inject in order; it may return a promise.

inject

inject?: ProviderToken[]

The tokens whose values the factory receives, in order. Not ExecutionContext: the factory runs once, so its value would keep the first call's context. Two providers that inject each other are refused, naming the cycle.

See also