UNPKG

2.84 kBTypeScriptView Raw
1import { BindingAddress } from './binding-key';
2import { InjectionMetadata } from './inject';
3/**
4 * Injection metadata for `@config.*`
5 */
6export interface ConfigInjectionMetadata extends InjectionMetadata {
7 /**
8 * Property path to retrieve the configuration of the target binding, for
9 * example, `rest.host`.
10 */
11 propertyPath?: string;
12 /**
13 * Customize the target binding key from which the configuration is fetched.
14 * If not specified, the configuration of the current binding that contains
15 * the injection is used.
16 */
17 fromBinding?: BindingAddress;
18}
19/**
20 * Inject a property from `config` of the current binding. If no corresponding
21 * config value is present, `undefined` will be injected as the configuration
22 * binding is resolved with `optional: true` by default.
23 *
24 * @example
25 * ```ts
26 * class Store {
27 * constructor(
28 * @config('x') public optionX: number,
29 * @config('y') public optionY: string,
30 * ) { }
31 * }
32 *
33 * ctx.configure('store1', { x: 1, y: 'a' });
34 * ctx.configure('store2', { x: 2, y: 'b' });
35 *
36 * ctx.bind('store1').toClass(Store);
37 * ctx.bind('store2').toClass(Store);
38 *
39 * const store1 = ctx.getSync('store1');
40 * expect(store1.optionX).to.eql(1);
41 * expect(store1.optionY).to.eql('a');
42 *
43 * const store2 = ctx.getSync('store2');
44 * expect(store2.optionX).to.eql(2);
45 * expect(store2.optionY).to.eql('b');
46 * ```
47 *
48 * @param propertyPath - Optional property path of the config. If is `''` or not
49 * present, the `config` object will be returned.
50 * @param metadata - Optional metadata to help the injection
51 */
52export declare function config(propertyPath?: string | ConfigInjectionMetadata, metadata?: ConfigInjectionMetadata): (target: Object, member: string | undefined, methodDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void;
53export declare namespace config {
54 /**
55 * `@inject.getter` decorator to inject a config getter function
56 * @param propertyPath - Optional property path of the config object
57 * @param metadata - Injection metadata
58 */
59 const getter: (propertyPath?: string | ConfigInjectionMetadata, metadata?: ConfigInjectionMetadata) => (target: Object, member: string | undefined, methodDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void;
60 /**
61 * `@inject.view` decorator to inject a config context view to allow dynamic
62 * changes in configuration
63 * @param propertyPath - Optional property path of the config object
64 * @param metadata - Injection metadata
65 */
66 const view: (propertyPath?: string | ConfigInjectionMetadata, metadata?: ConfigInjectionMetadata) => (target: Object, member: string | undefined, methodDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void;
67}