UNPKG

4.38 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 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 * This option is only available on factory providers!
44 *
45 * @see [Use factory](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory)
46 */
47 inject?: never;
48 /**
49 * Flags provider as durable. This flag can be used in combination with custom context id
50 * factory strategy to construct lazy DI subtrees.
51 *
52 * This flag can be used only in conjunction with scope = Scope.REQUEST.
53 */
54 durable?: boolean;
55}
56/**
57 * Interface defining a *Value* type provider.
58 *
59 * For example:
60 * ```typescript
61 * const connectionProvider = {
62 * provide: 'CONNECTION',
63 * useValue: connection,
64 * };
65 * ```
66 *
67 * @see [Value providers](https://docs.nestjs.com/fundamentals/custom-providers#value-providers-usevalue)
68 *
69 * @publicApi
70 */
71export interface ValueProvider<T = any> {
72 /**
73 * Injection token
74 */
75 provide: InjectionToken;
76 /**
77 * Instance of a provider to be injected.
78 */
79 useValue: T;
80 /**
81 * This option is only available on factory providers!
82 *
83 * @see [Use factory](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory)
84 */
85 inject?: never;
86}
87/**
88 * Interface defining a *Factory* type provider.
89 *
90 * For example:
91 * ```typescript
92 * const connectionFactory = {
93 * provide: 'CONNECTION',
94 * useFactory: (optionsProvider: OptionsProvider) => {
95 * const options = optionsProvider.get();
96 * return new DatabaseConnection(options);
97 * },
98 * inject: [OptionsProvider],
99 * };
100 * ```
101 *
102 * @see [Factory providers](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory)
103 * @see [Injection scopes](https://docs.nestjs.com/fundamentals/injection-scopes)
104 *
105 * @publicApi
106 */
107export interface FactoryProvider<T = any> {
108 /**
109 * Injection token
110 */
111 provide: InjectionToken;
112 /**
113 * Factory function that returns an instance of the provider to be injected.
114 */
115 useFactory: (...args: any[]) => T | Promise<T>;
116 /**
117 * Optional list of providers to be injected into the context of the Factory function.
118 */
119 inject?: Array<InjectionToken | OptionalFactoryDependency>;
120 /**
121 * Optional enum defining lifetime of the provider that is returned by the Factory function.
122 */
123 scope?: Scope;
124 /**
125 * Flags provider as durable. This flag can be used in combination with custom context id
126 * factory strategy to construct lazy DI subtrees.
127 *
128 * This flag can be used only in conjunction with scope = Scope.REQUEST.
129 */
130 durable?: boolean;
131}
132/**
133 * Interface defining an *Existing* (aliased) type provider.
134 *
135 * For example:
136 * ```typescript
137 * const loggerAliasProvider = {
138 * provide: 'AliasedLoggerService',
139 * useExisting: LoggerService
140 * };
141 * ```
142 *
143 * @see [Alias providers](https://docs.nestjs.com/fundamentals/custom-providers#alias-providers-useexisting)
144 *
145 * @publicApi
146 */
147export interface ExistingProvider<T = any> {
148 /**
149 * Injection token
150 */
151 provide: InjectionToken;
152 /**
153 * Provider to be aliased by the Injection token.
154 */
155 useExisting: any;
156}