UNPKG

3.41 kBTypeScriptView Raw
1import { Scope } from '../scope-options.interface';
2import { Type } from '../type.interface';
3import { InjectionToken } from './injection-token.interface';
4import { OptionalFactoryDependency } from './optional-factory-dependency.interface';
5/**
6 *
7 * @publicApi
8 */
9export declare type Provider<T = any> = Type<any> | ClassProvider<T> | ValueProvider<T> | FactoryProvider<T> | ExistingProvider<T>;
10/**
11 * Interface defining a *Class* type provider.
12 *
13 * For example:
14 * ```typescript
15 * const configServiceProvider = {
16 * provide: ConfigService,
17 * useClass:
18 * process.env.NODE_ENV === 'development'
19 * ? DevelopmentConfigService
20 * : ProductionConfigService,
21 * };
22 * ```
23 *
24 * @see [Class providers](https://docs.nestjs.com/fundamentals/custom-providers#class-providers-useclass)
25 * @see [Injection scopes](https://docs.nestjs.com/fundamentals/injection-scopes)
26 *
27 * @publicApi
28 */
29export interface ClassProvider<T = any> {
30 /**
31 * Injection token
32 */
33 provide: InjectionToken;
34 /**
35 * Type (class name) of provider (instance to be injected).
36 */
37 useClass: Type<T>;
38 /**
39 * Optional enum defining lifetime of the provider that is injected.
40 */
41 scope?: Scope;
42}
43/**
44 * Interface defining a *Value* type provider.
45 *
46 * For example:
47 * ```typescript
48 * const connectionProvider = {
49 * provide: 'CONNECTION',
50 * useValue: connection,
51 * };
52 * ```
53 *
54 * @see [Value providers](https://docs.nestjs.com/fundamentals/custom-providers#value-providers-usevalue)
55 *
56 * @publicApi
57 */
58export interface ValueProvider<T = any> {
59 /**
60 * Injection token
61 */
62 provide: InjectionToken;
63 /**
64 * Instance of a provider to be injected.
65 */
66 useValue: T;
67}
68/**
69 * Interface defining a *Factory* type provider.
70 *
71 * For example:
72 * ```typescript
73 * const connectionFactory = {
74 * provide: 'CONNECTION',
75 * useFactory: (optionsProvider: OptionsProvider) => {
76 * const options = optionsProvider.get();
77 * return new DatabaseConnection(options);
78 * },
79 * inject: [OptionsProvider],
80 * };
81 * ```
82 *
83 * @see [Factory providers](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory)
84 * @see [Injection scopes](https://docs.nestjs.com/fundamentals/injection-scopes)
85 *
86 * @publicApi
87 */
88export interface FactoryProvider<T = any> {
89 /**
90 * Injection token
91 */
92 provide: InjectionToken;
93 /**
94 * Factory function that returns an instance of the provider to be injected.
95 */
96 useFactory: (...args: any[]) => T;
97 /**
98 * Optional list of providers to be injected into the context of the Factory function.
99 */
100 inject?: Array<InjectionToken | OptionalFactoryDependency>;
101 /**
102 * Optional enum defining lifetime of the provider that is returned by the Factory function.
103 */
104 scope?: Scope;
105}
106/**
107 * Interface defining an *Existing* (aliased) type provider.
108 *
109 * For example:
110 * ```typescript
111 * const loggerAliasProvider = {
112 * provide: 'AliasedLoggerService',
113 * useExisting: LoggerService
114 * };
115 * ```
116 *
117 * @see [Alias providers](https://docs.nestjs.com/fundamentals/custom-providers#alias-providers-useexisting)
118 *
119 * @publicApi
120 */
121export interface ExistingProvider<T = any> {
122 /**
123 * Injection token
124 */
125 provide: InjectionToken;
126 /**
127 * Provider to be aliased by the Injection token.
128 */
129 useExisting: any;
130}