UNPKG

1.11 kBPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2017,2018. All Rights Reserved.
2// Node module: @loopback/context
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import {ValueOrPromise} from './value-promise';
7
8/**
9 * Providers allow developers to compute injected values dynamically,
10 * with any dependencies required by the value getter injected automatically
11 * from the Context.
12 *
13 * @example
14 *
15 * ```ts
16 * export class DateProvider implements Provider<Date> {
17 * constructor(@inject('stringDate') private param: String){}
18 * value(): Date {
19 * return new Date(param);
20 * }
21 * }
22 *
23 * ctx.bind('stringDate').to('2017-01-01')
24 * ctx.bind('provider_key').toProvider(DateProvider);
25 *
26 * const value = ctx.getAsync('provider_key');
27 * // value is a Date instance
28 * ```
29 */
30export interface Provider<T> {
31 /**
32 * @returns The value to inject to dependents.
33 * This method can return a promise too, in which case the IoC framework
34 * will resolve this promise to obtain the value to inject.
35 */
36 value(): ValueOrPromise<T>;
37}