UNPKG

4.93 kBTypeScriptView Raw
1import { MetadataAccessor } from '@loopback/metadata';
2import { Binding, BindingScope, BindingTag, BindingTemplate, DynamicValueProviderClass } from './binding';
3import { BindingAddress } from './binding-key';
4import { Provider } from './provider';
5import { Constructor } from './value-promise';
6/**
7 * Binding metadata from `@injectable`
8 *
9 * @typeParam T - Value type
10 */
11export type BindingMetadata<T = unknown> = {
12 /**
13 * An array of template functions to configure a binding
14 */
15 templates: BindingTemplate<T>[];
16 /**
17 * The target class where binding metadata is decorated
18 */
19 target: Constructor<T>;
20};
21/**
22 * Metadata key for binding metadata
23 */
24export declare const BINDING_METADATA_KEY: MetadataAccessor<BindingMetadata<unknown>, ClassDecorator>;
25/**
26 * An object to configure binding scope and tags
27 */
28export type BindingScopeAndTags = {
29 scope?: BindingScope;
30 tags?: BindingTag | BindingTag[];
31};
32/**
33 * Specification of parameters for `@injectable()`
34 */
35export type BindingSpec<T = unknown> = BindingTemplate<T> | BindingScopeAndTags;
36/**
37 * Check if a class implements `Provider` interface
38 * @param cls - A class
39 *
40 * @typeParam T - Value type
41 */
42export declare function isProviderClass<T>(cls: unknown): cls is Constructor<Provider<T>>;
43/**
44 * A factory function to create a template function to bind the target class
45 * as a `Provider`.
46 * @param target - Target provider class
47 *
48 * @typeParam T - Value type
49 */
50export declare function asProvider<T>(target: Constructor<Provider<T>>): BindingTemplate<T>;
51/**
52 * A factory function to create a template function to bind the target class
53 * as a class or `Provider`.
54 * @param target - Target class, which can be an implementation of `Provider`
55 * or `DynamicValueProviderClass`
56 *
57 * @typeParam T - Value type
58 */
59export declare function asClassOrProvider<T>(target: Constructor<T | Provider<T>> | DynamicValueProviderClass<T>): BindingTemplate<T>;
60/**
61 * Convert binding scope and tags as a template function
62 * @param scopeAndTags - Binding scope and tags
63 *
64 * @typeParam T - Value type
65 */
66export declare function asBindingTemplate<T = unknown>(scopeAndTags: BindingScopeAndTags): BindingTemplate<T>;
67/**
68 * Get binding metadata for a class
69 * @param target - The target class
70 *
71 * @typeParam T - Value type
72 */
73export declare function getBindingMetadata<T = unknown>(target: Function): BindingMetadata<T> | undefined;
74/**
75 * A binding template function to delete `name` and `key` tags
76 */
77export declare function removeNameAndKeyTags(binding: Binding<unknown>): void;
78/**
79 * Get the binding template for a class with binding metadata
80 *
81 * @param cls - A class with optional `@injectable`
82 *
83 * @typeParam T - Value type
84 */
85export declare function bindingTemplateFor<T>(cls: Constructor<T | Provider<T>> | DynamicValueProviderClass<T>, options?: BindingFromClassOptions): BindingTemplate<T>;
86/**
87 * Mapping artifact types to binding key namespaces (prefixes).
88 *
89 * @example
90 * ```ts
91 * {
92 * repository: 'repositories'
93 * }
94 * ```
95 */
96export type TypeNamespaceMapping = {
97 [name: string]: string;
98};
99export declare const DEFAULT_TYPE_NAMESPACES: TypeNamespaceMapping;
100/**
101 * Options to customize the binding created from a class
102 */
103export type BindingFromClassOptions = {
104 /**
105 * Binding key
106 */
107 key?: BindingAddress;
108 /**
109 * Artifact type, such as `server`, `controller`, `repository` or `service`
110 */
111 type?: string;
112 /**
113 * Artifact name, such as `my-rest-server` and `my-controller`. It
114 * overrides the name tag
115 */
116 name?: string;
117 /**
118 * Namespace for the binding key, such as `servers` and `controllers`. It
119 * overrides the default namespace or namespace tag
120 */
121 namespace?: string;
122 /**
123 * Mapping artifact type to binding key namespaces
124 */
125 typeNamespaceMapping?: TypeNamespaceMapping;
126 /**
127 * Default namespace if the binding does not have an explicit namespace
128 */
129 defaultNamespace?: string;
130 /**
131 * Default scope if the binding does not have an explicit scope
132 */
133 defaultScope?: BindingScope;
134};
135/**
136 * Create a binding from a class with decorated metadata. The class is attached
137 * to the binding as follows:
138 * - `binding.toClass(cls)`: if `cls` is a plain class such as `MyController`
139 * - `binding.toProvider(cls)`: if `cls` is a value provider class with a
140 * prototype method `value()`
141 * - `binding.toDynamicValue(cls)`: if `cls` is a dynamic value provider class
142 * with a static method `value()`
143 *
144 * @param cls - A class. It can be either a plain class, a value provider class,
145 * or a dynamic value provider class
146 * @param options - Options to customize the binding key
147 *
148 * @typeParam T - Value type
149 */
150export declare function createBindingFromClass<T>(cls: Constructor<T | Provider<T>> | DynamicValueProviderClass<T>, options?: BindingFromClassOptions): Binding<T>;