@loopback/context 4.0.0-alpha.8 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +116 -0
  3. package/dist/binding-config.d.ts +40 -0
  4. package/dist/binding-config.js +33 -0
  5. package/dist/binding-config.js.map +1 -0
  6. package/dist/binding-decorator.d.ts +45 -0
  7. package/dist/binding-decorator.js +118 -0
  8. package/dist/binding-decorator.js.map +1 -0
  9. package/dist/binding-filter.d.ts +108 -0
  10. package/dist/binding-filter.js +162 -0
  11. package/dist/binding-filter.js.map +1 -0
  12. package/dist/binding-inspector.d.ts +150 -0
  13. package/dist/binding-inspector.js +249 -0
  14. package/dist/binding-inspector.js.map +1 -0
  15. package/dist/binding-key.d.ts +66 -0
  16. package/dist/binding-key.js +121 -0
  17. package/dist/binding-key.js.map +1 -0
  18. package/dist/binding-sorter.d.ts +71 -0
  19. package/dist/binding-sorter.js +89 -0
  20. package/dist/binding-sorter.js.map +1 -0
  21. package/dist/binding.d.ts +577 -0
  22. package/dist/binding.js +788 -0
  23. package/dist/binding.js.map +1 -0
  24. package/dist/context-event.d.ts +23 -0
  25. package/dist/context-event.js +7 -0
  26. package/dist/context-event.js.map +1 -0
  27. package/dist/context-observer.d.ts +36 -0
  28. package/dist/context-observer.js +7 -0
  29. package/dist/context-observer.js.map +1 -0
  30. package/dist/context-subscription.d.ts +147 -0
  31. package/dist/context-subscription.js +317 -0
  32. package/dist/context-subscription.js.map +1 -0
  33. package/dist/context-tag-indexer.d.ts +42 -0
  34. package/dist/context-tag-indexer.js +135 -0
  35. package/dist/context-tag-indexer.js.map +1 -0
  36. package/dist/context-view.d.ts +209 -0
  37. package/dist/context-view.js +240 -0
  38. package/dist/context-view.js.map +1 -0
  39. package/dist/context.d.ts +513 -0
  40. package/dist/context.js +717 -0
  41. package/dist/context.js.map +1 -0
  42. package/dist/index.d.ts +52 -0
  43. package/dist/index.js +60 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/inject-config.d.ts +67 -0
  46. package/dist/inject-config.js +181 -0
  47. package/dist/inject-config.js.map +1 -0
  48. package/dist/inject.d.ts +250 -0
  49. package/dist/inject.js +535 -0
  50. package/dist/inject.js.map +1 -0
  51. package/dist/interception-proxy.d.ts +76 -0
  52. package/dist/interception-proxy.js +67 -0
  53. package/dist/interception-proxy.js.map +1 -0
  54. package/dist/interceptor-chain.d.ts +121 -0
  55. package/dist/interceptor-chain.js +148 -0
  56. package/dist/interceptor-chain.js.map +1 -0
  57. package/dist/interceptor.d.ts +138 -0
  58. package/dist/interceptor.js +299 -0
  59. package/dist/interceptor.js.map +1 -0
  60. package/dist/invocation.d.ts +101 -0
  61. package/dist/invocation.js +163 -0
  62. package/dist/invocation.js.map +1 -0
  63. package/dist/json-types.d.ts +28 -0
  64. package/{lib/src/provider.js → dist/json-types.js} +3 -3
  65. package/dist/json-types.js.map +1 -0
  66. package/dist/keys.d.ts +65 -0
  67. package/dist/keys.js +74 -0
  68. package/dist/keys.js.map +1 -0
  69. package/dist/provider.d.ts +31 -0
  70. package/{lib6/src → dist}/provider.js +2 -2
  71. package/dist/provider.js.map +1 -0
  72. package/dist/resolution-session.d.ts +180 -0
  73. package/dist/resolution-session.js +274 -0
  74. package/dist/resolution-session.js.map +1 -0
  75. package/dist/resolver.d.ts +46 -0
  76. package/dist/resolver.js +203 -0
  77. package/dist/resolver.js.map +1 -0
  78. package/dist/unique-id.d.ts +14 -0
  79. package/dist/unique-id.js +26 -0
  80. package/dist/unique-id.js.map +1 -0
  81. package/dist/value-promise.d.ts +134 -0
  82. package/dist/value-promise.js +277 -0
  83. package/dist/value-promise.js.map +1 -0
  84. package/package.json +49 -34
  85. package/src/binding-config.ts +73 -0
  86. package/src/binding-decorator.ts +136 -0
  87. package/src/binding-filter.ts +250 -0
  88. package/src/binding-inspector.ts +371 -0
  89. package/src/binding-key.ts +136 -0
  90. package/src/binding-sorter.ts +124 -0
  91. package/src/binding.ts +1107 -0
  92. package/src/context-event.ts +30 -0
  93. package/src/context-observer.ts +50 -0
  94. package/src/context-subscription.ts +402 -0
  95. package/src/context-tag-indexer.ts +147 -0
  96. package/src/context-view.ts +440 -0
  97. package/src/context.ts +1079 -0
  98. package/src/index.ts +58 -0
  99. package/src/inject-config.ts +239 -0
  100. package/src/inject.ts +796 -0
  101. package/src/interception-proxy.ts +127 -0
  102. package/src/interceptor-chain.ts +268 -0
  103. package/src/interceptor.ts +430 -0
  104. package/src/invocation.ts +269 -0
  105. package/src/json-types.ts +35 -0
  106. package/src/keys.ts +85 -0
  107. package/src/provider.ts +37 -0
  108. package/src/resolution-session.ts +414 -0
  109. package/src/resolver.ts +282 -0
  110. package/src/unique-id.ts +24 -0
  111. package/src/value-promise.ts +318 -0
  112. package/index.d.ts +0 -6
  113. package/index.js +0 -9
  114. package/lib/binding.d.ts +0 -75
  115. package/lib/binding.js +0 -103
  116. package/lib/binding.js.map +0 -1
  117. package/lib/context.d.ts +0 -14
  118. package/lib/context.js +0 -97
  119. package/lib/context.js.map +0 -1
  120. package/lib/index.d.ts +0 -1
  121. package/lib/index.js +0 -12
  122. package/lib/index.js.map +0 -1
  123. package/lib/inject.d.ts +0 -46
  124. package/lib/inject.js +0 -74
  125. package/lib/inject.js.map +0 -1
  126. package/lib/isPromise.d.ts +0 -1
  127. package/lib/isPromise.js +0 -15
  128. package/lib/isPromise.js.map +0 -1
  129. package/lib/reflect.d.ts +0 -39
  130. package/lib/reflect.js +0 -20
  131. package/lib/reflect.js.map +0 -1
  132. package/lib/resolver.d.ts +0 -30
  133. package/lib/resolver.js +0 -129
  134. package/lib/resolver.js.map +0 -1
  135. package/lib/src/binding.d.ts +0 -85
  136. package/lib/src/binding.js +0 -123
  137. package/lib/src/binding.js.map +0 -1
  138. package/lib/src/context.d.ts +0 -14
  139. package/lib/src/context.js +0 -97
  140. package/lib/src/context.js.map +0 -1
  141. package/lib/src/index.d.ts +0 -10
  142. package/lib/src/index.js +0 -27
  143. package/lib/src/index.js.map +0 -1
  144. package/lib/src/inject.d.ts +0 -46
  145. package/lib/src/inject.js +0 -74
  146. package/lib/src/inject.js.map +0 -1
  147. package/lib/src/isPromise.d.ts +0 -1
  148. package/lib/src/isPromise.js +0 -15
  149. package/lib/src/isPromise.js.map +0 -1
  150. package/lib/src/provider.d.ts +0 -29
  151. package/lib/src/provider.js.map +0 -1
  152. package/lib/src/reflect.d.ts +0 -38
  153. package/lib/src/reflect.js +0 -143
  154. package/lib/src/reflect.js.map +0 -1
  155. package/lib/src/resolver.d.ts +0 -34
  156. package/lib/src/resolver.js +0 -144
  157. package/lib/src/resolver.js.map +0 -1
  158. package/lib6/binding.d.ts +0 -75
  159. package/lib6/binding.js +0 -103
  160. package/lib6/binding.js.map +0 -1
  161. package/lib6/context.d.ts +0 -14
  162. package/lib6/context.js +0 -97
  163. package/lib6/context.js.map +0 -1
  164. package/lib6/index.d.ts +0 -1
  165. package/lib6/index.js +0 -12
  166. package/lib6/index.js.map +0 -1
  167. package/lib6/inject.d.ts +0 -46
  168. package/lib6/inject.js +0 -74
  169. package/lib6/inject.js.map +0 -1
  170. package/lib6/isPromise.d.ts +0 -1
  171. package/lib6/isPromise.js +0 -15
  172. package/lib6/isPromise.js.map +0 -1
  173. package/lib6/reflect.d.ts +0 -39
  174. package/lib6/reflect.js +0 -20
  175. package/lib6/reflect.js.map +0 -1
  176. package/lib6/resolver.d.ts +0 -30
  177. package/lib6/resolver.js +0 -129
  178. package/lib6/resolver.js.map +0 -1
  179. package/lib6/src/binding.d.ts +0 -85
  180. package/lib6/src/binding.js +0 -133
  181. package/lib6/src/binding.js.map +0 -1
  182. package/lib6/src/context.d.ts +0 -14
  183. package/lib6/src/context.js +0 -97
  184. package/lib6/src/context.js.map +0 -1
  185. package/lib6/src/index.d.ts +0 -10
  186. package/lib6/src/index.js +0 -27
  187. package/lib6/src/index.js.map +0 -1
  188. package/lib6/src/inject.d.ts +0 -46
  189. package/lib6/src/inject.js +0 -74
  190. package/lib6/src/inject.js.map +0 -1
  191. package/lib6/src/isPromise.d.ts +0 -1
  192. package/lib6/src/isPromise.js +0 -15
  193. package/lib6/src/isPromise.js.map +0 -1
  194. package/lib6/src/provider.d.ts +0 -29
  195. package/lib6/src/provider.js.map +0 -1
  196. package/lib6/src/reflect.d.ts +0 -38
  197. package/lib6/src/reflect.js +0 -143
  198. package/lib6/src/reflect.js.map +0 -1
  199. package/lib6/src/resolver.d.ts +0 -34
  200. package/lib6/src/resolver.js +0 -154
  201. package/lib6/src/resolver.js.map +0 -1
@@ -0,0 +1,150 @@
1
+ import { MetadataAccessor } from '@loopback/metadata';
2
+ import { Binding, BindingScope, BindingTag, BindingTemplate, DynamicValueProviderClass } from './binding';
3
+ import { BindingAddress } from './binding-key';
4
+ import { Provider } from './provider';
5
+ import { Constructor } from './value-promise';
6
+ /**
7
+ * Binding metadata from `@injectable`
8
+ *
9
+ * @typeParam T - Value type
10
+ */
11
+ export declare 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
+ */
24
+ export declare const BINDING_METADATA_KEY: MetadataAccessor<BindingMetadata<unknown>, ClassDecorator>;
25
+ /**
26
+ * An object to configure binding scope and tags
27
+ */
28
+ export declare type BindingScopeAndTags = {
29
+ scope?: BindingScope;
30
+ tags?: BindingTag | BindingTag[];
31
+ };
32
+ /**
33
+ * Specification of parameters for `@injectable()`
34
+ */
35
+ export declare 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
+ */
42
+ export 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
+ */
50
+ export 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
+ */
59
+ export 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
+ */
66
+ export 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
+ */
73
+ export declare function getBindingMetadata<T = unknown>(target: Function): BindingMetadata<T> | undefined;
74
+ /**
75
+ * A binding template function to delete `name` and `key` tags
76
+ */
77
+ export 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
+ */
85
+ export 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
+ */
96
+ export declare type TypeNamespaceMapping = {
97
+ [name: string]: string;
98
+ };
99
+ export declare const DEFAULT_TYPE_NAMESPACES: TypeNamespaceMapping;
100
+ /**
101
+ * Options to customize the binding created from a class
102
+ */
103
+ export declare 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
+ */
150
+ export declare function createBindingFromClass<T>(cls: Constructor<T | Provider<T>> | DynamicValueProviderClass<T>, options?: BindingFromClassOptions): Binding<T>;
@@ -0,0 +1,249 @@
1
+ "use strict";
2
+ // Copyright IBM Corp. 2018,2020. All Rights Reserved.
3
+ // Node module: @loopback/context
4
+ // This file is licensed under the MIT License.
5
+ // License text available at https://opensource.org/licenses/MIT
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.createBindingFromClass = exports.DEFAULT_TYPE_NAMESPACES = exports.bindingTemplateFor = exports.removeNameAndKeyTags = exports.getBindingMetadata = exports.asBindingTemplate = exports.asClassOrProvider = exports.asProvider = exports.isProviderClass = exports.BINDING_METADATA_KEY = void 0;
8
+ const tslib_1 = require("tslib");
9
+ const metadata_1 = require("@loopback/metadata");
10
+ const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
11
+ const binding_1 = require("./binding");
12
+ const keys_1 = require("./keys");
13
+ const debug = (0, debug_1.default)('loopback:context:binding-inspector');
14
+ /**
15
+ * Metadata key for binding metadata
16
+ */
17
+ exports.BINDING_METADATA_KEY = metadata_1.MetadataAccessor.create('binding.metadata');
18
+ /**
19
+ * Check if a class implements `Provider` interface
20
+ * @param cls - A class
21
+ *
22
+ * @typeParam T - Value type
23
+ */
24
+ function isProviderClass(cls) {
25
+ var _a;
26
+ return (typeof cls === 'function' && typeof ((_a = cls.prototype) === null || _a === void 0 ? void 0 : _a.value) === 'function');
27
+ }
28
+ exports.isProviderClass = isProviderClass;
29
+ /**
30
+ * A factory function to create a template function to bind the target class
31
+ * as a `Provider`.
32
+ * @param target - Target provider class
33
+ *
34
+ * @typeParam T - Value type
35
+ */
36
+ function asProvider(target) {
37
+ return function bindAsProvider(binding) {
38
+ binding.toProvider(target).tag(keys_1.ContextTags.PROVIDER, {
39
+ [keys_1.ContextTags.TYPE]: keys_1.ContextTags.PROVIDER,
40
+ });
41
+ };
42
+ }
43
+ exports.asProvider = asProvider;
44
+ /**
45
+ * A factory function to create a template function to bind the target class
46
+ * as a class or `Provider`.
47
+ * @param target - Target class, which can be an implementation of `Provider`
48
+ * or `DynamicValueProviderClass`
49
+ *
50
+ * @typeParam T - Value type
51
+ */
52
+ function asClassOrProvider(target) {
53
+ // Add a template to bind to a class or provider
54
+ return function bindAsClassOrProvider(binding) {
55
+ if (isProviderClass(target)) {
56
+ asProvider(target)(binding);
57
+ }
58
+ else if ((0, binding_1.isDynamicValueProviderClass)(target)) {
59
+ binding.toDynamicValue(target).tag(keys_1.ContextTags.DYNAMIC_VALUE_PROVIDER, {
60
+ [keys_1.ContextTags.TYPE]: keys_1.ContextTags.DYNAMIC_VALUE_PROVIDER,
61
+ });
62
+ }
63
+ else {
64
+ binding.toClass(target);
65
+ }
66
+ };
67
+ }
68
+ exports.asClassOrProvider = asClassOrProvider;
69
+ /**
70
+ * Convert binding scope and tags as a template function
71
+ * @param scopeAndTags - Binding scope and tags
72
+ *
73
+ * @typeParam T - Value type
74
+ */
75
+ function asBindingTemplate(scopeAndTags) {
76
+ return function applyBindingScopeAndTag(binding) {
77
+ if (scopeAndTags.scope) {
78
+ binding.inScope(scopeAndTags.scope);
79
+ }
80
+ if (scopeAndTags.tags) {
81
+ if (Array.isArray(scopeAndTags.tags)) {
82
+ binding.tag(...scopeAndTags.tags);
83
+ }
84
+ else {
85
+ binding.tag(scopeAndTags.tags);
86
+ }
87
+ }
88
+ };
89
+ }
90
+ exports.asBindingTemplate = asBindingTemplate;
91
+ /**
92
+ * Get binding metadata for a class
93
+ * @param target - The target class
94
+ *
95
+ * @typeParam T - Value type
96
+ */
97
+ function getBindingMetadata(target) {
98
+ return metadata_1.MetadataInspector.getClassMetadata(exports.BINDING_METADATA_KEY, target);
99
+ }
100
+ exports.getBindingMetadata = getBindingMetadata;
101
+ /**
102
+ * A binding template function to delete `name` and `key` tags
103
+ */
104
+ function removeNameAndKeyTags(binding) {
105
+ if (binding.tagMap) {
106
+ delete binding.tagMap.name;
107
+ delete binding.tagMap.key;
108
+ }
109
+ }
110
+ exports.removeNameAndKeyTags = removeNameAndKeyTags;
111
+ /**
112
+ * Get the binding template for a class with binding metadata
113
+ *
114
+ * @param cls - A class with optional `@injectable`
115
+ *
116
+ * @typeParam T - Value type
117
+ */
118
+ function bindingTemplateFor(cls, options) {
119
+ var _a;
120
+ const spec = getBindingMetadata(cls);
121
+ debug('class %s has binding metadata', cls.name, spec);
122
+ const templateFunctions = (_a = spec === null || spec === void 0 ? void 0 : spec.templates) !== null && _a !== void 0 ? _a : [];
123
+ if ((spec === null || spec === void 0 ? void 0 : spec.target) !== cls) {
124
+ // Make sure the subclass is used as the binding source
125
+ templateFunctions.push(asClassOrProvider(cls));
126
+ }
127
+ return function applyBindingTemplatesFromMetadata(binding) {
128
+ for (const t of templateFunctions) {
129
+ binding.apply(t);
130
+ }
131
+ if ((spec === null || spec === void 0 ? void 0 : spec.target) !== cls) {
132
+ // Remove name/key tags inherited from base classes
133
+ binding.apply(removeNameAndKeyTags);
134
+ }
135
+ if (options != null) {
136
+ applyClassBindingOptions(binding, options);
137
+ }
138
+ };
139
+ }
140
+ exports.bindingTemplateFor = bindingTemplateFor;
141
+ exports.DEFAULT_TYPE_NAMESPACES = {
142
+ class: 'classes',
143
+ provider: 'providers',
144
+ dynamicValueProvider: 'dynamicValueProviders',
145
+ };
146
+ /**
147
+ * Create a binding from a class with decorated metadata. The class is attached
148
+ * to the binding as follows:
149
+ * - `binding.toClass(cls)`: if `cls` is a plain class such as `MyController`
150
+ * - `binding.toProvider(cls)`: if `cls` is a value provider class with a
151
+ * prototype method `value()`
152
+ * - `binding.toDynamicValue(cls)`: if `cls` is a dynamic value provider class
153
+ * with a static method `value()`
154
+ *
155
+ * @param cls - A class. It can be either a plain class, a value provider class,
156
+ * or a dynamic value provider class
157
+ * @param options - Options to customize the binding key
158
+ *
159
+ * @typeParam T - Value type
160
+ */
161
+ function createBindingFromClass(cls, options = {}) {
162
+ debug('create binding from class %s with options', cls.name, options);
163
+ try {
164
+ const templateFn = bindingTemplateFor(cls, options);
165
+ const key = buildBindingKey(cls, options);
166
+ const binding = binding_1.Binding.bind(key).apply(templateFn);
167
+ return binding;
168
+ }
169
+ catch (err) {
170
+ err.message += ` (while building binding for class ${cls.name})`;
171
+ throw err;
172
+ }
173
+ }
174
+ exports.createBindingFromClass = createBindingFromClass;
175
+ function applyClassBindingOptions(binding, options) {
176
+ if (options.name) {
177
+ binding.tag({ name: options.name });
178
+ }
179
+ if (options.type) {
180
+ binding.tag({ type: options.type }, options.type);
181
+ }
182
+ if (options.defaultScope) {
183
+ binding.applyDefaultScope(options.defaultScope);
184
+ }
185
+ }
186
+ /**
187
+ * Find/infer binding key namespace for a type
188
+ * @param type - Artifact type, such as `controller`, `datasource`, or `server`
189
+ * @param typeNamespaces - An object mapping type names to namespaces
190
+ */
191
+ function getNamespace(type, typeNamespaces = exports.DEFAULT_TYPE_NAMESPACES) {
192
+ if (type in typeNamespaces) {
193
+ return typeNamespaces[type];
194
+ }
195
+ else {
196
+ // Return the plural form
197
+ return `${type}s`;
198
+ }
199
+ }
200
+ /**
201
+ * Build the binding key for a class with optional binding metadata.
202
+ * The binding key is resolved in the following steps:
203
+ *
204
+ * 1. Check `options.key`, if it exists, return it
205
+ * 2. Check if the binding metadata has `key` tag, if yes, return its tag value
206
+ * 3. Identify `namespace` and `name` to form the binding key as
207
+ * `<namespace>.<name>`.
208
+ * - namespace
209
+ * - `options.namespace`
210
+ * - `namespace` tag value
211
+ * - Map `options.type` or `type` tag value to a namespace, for example,
212
+ * 'controller` to 'controller'.
213
+ * - name
214
+ * - `options.name`
215
+ * - `name` tag value
216
+ * - the class name
217
+ *
218
+ * @param cls - A class to be bound
219
+ * @param options - Options to customize how to build the key
220
+ *
221
+ * @typeParam T - Value type
222
+ */
223
+ function buildBindingKey(cls, options = {}) {
224
+ var _a, _b, _c, _d, _e;
225
+ if (options.key)
226
+ return options.key;
227
+ const templateFn = bindingTemplateFor(cls);
228
+ // Create a temporary binding
229
+ const bindingTemplate = new binding_1.Binding('template').apply(templateFn);
230
+ // Is there a `key` tag?
231
+ let key = bindingTemplate.tagMap[keys_1.ContextTags.KEY];
232
+ if (key)
233
+ return key;
234
+ let namespace = (_b = (_a = options.namespace) !== null && _a !== void 0 ? _a : bindingTemplate.tagMap[keys_1.ContextTags.NAMESPACE]) !== null && _b !== void 0 ? _b : options.defaultNamespace;
235
+ if (!namespace) {
236
+ const namespaces = Object.assign({}, exports.DEFAULT_TYPE_NAMESPACES, options.typeNamespaceMapping);
237
+ // Derive the key from type + name
238
+ let type = (_c = options.type) !== null && _c !== void 0 ? _c : bindingTemplate.tagMap[keys_1.ContextTags.TYPE];
239
+ if (!type) {
240
+ type =
241
+ (_d = bindingTemplate.tagNames.find(t => namespaces[t] != null)) !== null && _d !== void 0 ? _d : keys_1.ContextTags.CLASS;
242
+ }
243
+ namespace = getNamespace(type, namespaces);
244
+ }
245
+ const name = (_e = options.name) !== null && _e !== void 0 ? _e : (bindingTemplate.tagMap[keys_1.ContextTags.NAME] || cls.name);
246
+ key = `${namespace}.${name}`;
247
+ return key;
248
+ }
249
+ //# sourceMappingURL=binding-inspector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binding-inspector.js","sourceRoot":"","sources":["../src/binding-inspector.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,iCAAiC;AACjC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,iDAAuE;AACvE,+DAAiC;AACjC,uCAOmB;AAEnB,iCAAmC;AAInC,MAAM,KAAK,GAAG,IAAA,eAAY,EAAC,oCAAoC,CAAC,CAAC;AAkBjE;;GAEG;AACU,QAAA,oBAAoB,GAAG,2BAAgB,CAAC,MAAM,CAGzD,kBAAkB,CAAC,CAAC;AAetB;;;;;GAKG;AACH,SAAgB,eAAe,CAC7B,GAAY;;IAEZ,OAAO,CACL,OAAO,GAAG,KAAK,UAAU,IAAI,OAAO,CAAA,MAAA,GAAG,CAAC,SAAS,0CAAE,KAAK,CAAA,KAAK,UAAU,CACxE,CAAC;AACJ,CAAC;AAND,0CAMC;AAED;;;;;;GAMG;AACH,SAAgB,UAAU,CACxB,MAAgC;IAEhC,OAAO,SAAS,cAAc,CAAC,OAAO;QACpC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,kBAAW,CAAC,QAAQ,EAAE;YACnD,CAAC,kBAAW,CAAC,IAAI,CAAC,EAAE,kBAAW,CAAC,QAAQ;SACzC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AARD,gCAQC;AAED;;;;;;;GAOG;AACH,SAAgB,iBAAiB,CAC/B,MAAmE;IAEnE,gDAAgD;IAChD,OAAO,SAAS,qBAAqB,CAAC,OAAO;QAC3C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are type BindingAddress<T = unknown> = string | BindingKey<T>;
2
+ export declare class BindingKey<ValueType> {
3
+ readonly key: string;
4
+ readonly propertyPath?: string | undefined;
5
+ static readonly PROPERTY_SEPARATOR = "#";
6
+ /**
7
+ * Create a new key for a binding bound to a value of type `ValueType`.
8
+ *
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * BindingKey.create<string>('application.name');
13
+ * BindingKey.create<number>('config', 'rest.port);
14
+ * BindingKey.create<number>('config#rest.port');
15
+ * ```
16
+ *
17
+ * @param key - The binding key. When propertyPath is not provided, the key
18
+ * is allowed to contain propertyPath as encoded via `BindingKey#toString()`
19
+ * @param propertyPath - Optional path to a deep property of the bound value.
20
+ */
21
+ static create<V>(key: string, propertyPath?: string): BindingKey<V>;
22
+ private constructor();
23
+ toString(): string;
24
+ /**
25
+ * Get a binding address for retrieving a deep property of the object
26
+ * bound to the current binding key.
27
+ *
28
+ * @param propertyPath - A dot-separated path to a (deep) property, e.g. "server.port".
29
+ */
30
+ deepProperty<PropertyValueType>(propertyPath: string): BindingKey<PropertyValueType>;
31
+ /**
32
+ * Validate the binding key format. Please note that `#` is reserved.
33
+ * Returns a string representation of the binding key.
34
+ *
35
+ * @param key - Binding key, such as `a`, `a.b`, `a:b`, or `a/b`
36
+ */
37
+ static validate<T>(key: BindingAddress<T>): string;
38
+ /**
39
+ * Parse a string containing both the binding key and the path to the deeply
40
+ * nested property to retrieve.
41
+ *
42
+ * @param keyWithPath - The key with an optional path,
43
+ * e.g. "application.instance" or "config#rest.port".
44
+ */
45
+ static parseKeyWithPath<T>(keyWithPath: BindingAddress<T>): BindingKey<T>;
46
+ /**
47
+ * Name space for configuration binding keys
48
+ */
49
+ static CONFIG_NAMESPACE: string;
50
+ /**
51
+ * Build a binding key for the configuration of the given binding.
52
+ * The format is `<key>:$config`
53
+ *
54
+ * @param key - Key of the target binding to be configured
55
+ */
56
+ static buildKeyForConfig<T>(key?: BindingAddress): BindingAddress<T>;
57
+ /**
58
+ * Generate a universally unique binding key.
59
+ *
60
+ * Please note the format of they generated key is not specified, you must
61
+ * not rely on any specific formatting (e.g. UUID style).
62
+ *
63
+ * @param namespace - Namespace for the binding
64
+ */
65
+ static generate<T>(namespace?: string): BindingKey<T>;
66
+ }
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ // Copyright IBM Corp. 2018,2020. All Rights Reserved.
3
+ // Node module: @loopback/context
4
+ // This file is licensed under the MIT License.
5
+ // License text available at https://opensource.org/licenses/MIT
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.BindingKey = void 0;
8
+ const unique_id_1 = require("./unique-id");
9
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
10
+ class BindingKey {
11
+ constructor(key, propertyPath) {
12
+ this.key = key;
13
+ this.propertyPath = propertyPath;
14
+ }
15
+ /**
16
+ * Create a new key for a binding bound to a value of type `ValueType`.
17
+ *
18
+ * @example
19
+ *
20
+ * ```ts
21
+ * BindingKey.create<string>('application.name');
22
+ * BindingKey.create<number>('config', 'rest.port);
23
+ * BindingKey.create<number>('config#rest.port');
24
+ * ```
25
+ *
26
+ * @param key - The binding key. When propertyPath is not provided, the key
27
+ * is allowed to contain propertyPath as encoded via `BindingKey#toString()`
28
+ * @param propertyPath - Optional path to a deep property of the bound value.
29
+ */
30
+ static create(key, propertyPath) {
31
+ // TODO(bajtos) allow chaining of propertyPaths, e.g.
32
+ // BindingKey.create('config#rest', 'port')
33
+ // should create {key: 'config', path: 'rest.port'}
34
+ if (propertyPath) {
35
+ BindingKey.validate(key);
36
+ return new BindingKey(key, propertyPath);
37
+ }
38
+ return BindingKey.parseKeyWithPath(key);
39
+ }
40
+ toString() {
41
+ return this.propertyPath
42
+ ? `${this.key}${BindingKey.PROPERTY_SEPARATOR}${this.propertyPath}`
43
+ : this.key;
44
+ }
45
+ /**
46
+ * Get a binding address for retrieving a deep property of the object
47
+ * bound to the current binding key.
48
+ *
49
+ * @param propertyPath - A dot-separated path to a (deep) property, e.g. "server.port".
50
+ */
51
+ deepProperty(propertyPath) {
52
+ // TODO(bajtos) allow chaining of propertyPaths, e.g.
53
+ // BindingKey.create('config', 'rest').deepProperty('port')
54
+ // should create {key: 'config', path: 'rest.port'}
55
+ return BindingKey.create(this.key, propertyPath);
56
+ }
57
+ /**
58
+ * Validate the binding key format. Please note that `#` is reserved.
59
+ * Returns a string representation of the binding key.
60
+ *
61
+ * @param key - Binding key, such as `a`, `a.b`, `a:b`, or `a/b`
62
+ */
63
+ static validate(key) {
64
+ if (!key)
65
+ throw new Error('Binding key must be provided.');
66
+ key = key.toString();
67
+ if (key.includes(BindingKey.PROPERTY_SEPARATOR)) {
68
+ throw new Error(`Binding key ${key} cannot contain` +
69
+ ` '${BindingKey.PROPERTY_SEPARATOR}'.`);
70
+ }
71
+ return key;
72
+ }
73
+ /**
74
+ * Parse a string containing both the binding key and the path to the deeply
75
+ * nested property to retrieve.
76
+ *
77
+ * @param keyWithPath - The key with an optional path,
78
+ * e.g. "application.instance" or "config#rest.port".
79
+ */
80
+ static parseKeyWithPath(keyWithPath) {
81
+ if (typeof keyWithPath !== 'string') {
82
+ return BindingKey.create(keyWithPath.key, keyWithPath.propertyPath);
83
+ }
84
+ const index = keyWithPath.indexOf(BindingKey.PROPERTY_SEPARATOR);
85
+ if (index === -1) {
86
+ return new BindingKey(keyWithPath);
87
+ }
88
+ return BindingKey.create(keyWithPath.substr(0, index).trim(), keyWithPath.substr(index + 1));
89
+ }
90
+ /**
91
+ * Build a binding key for the configuration of the given binding.
92
+ * The format is `<key>:$config`
93
+ *
94
+ * @param key - Key of the target binding to be configured
95
+ */
96
+ static buildKeyForConfig(key = '') {
97
+ const suffix = BindingKey.CONFIG_NAMESPACE;
98
+ const bindingKey = key ? `${key}:${suffix}` : suffix;
99
+ return bindingKey;
100
+ }
101
+ /**
102
+ * Generate a universally unique binding key.
103
+ *
104
+ * Please note the format of they generated key is not specified, you must
105
+ * not rely on any specific formatting (e.g. UUID style).
106
+ *
107
+ * @param namespace - Namespace for the binding
108
+ */
109
+ static generate(namespace = '') {
110
+ const prefix = namespace ? `${namespace}.` : '';
111
+ const name = (0, unique_id_1.generateUniqueId)();
112
+ return BindingKey.create(`${prefix}${name}`);
113
+ }
114
+ }
115
+ exports.BindingKey = BindingKey;
116
+ BindingKey.PROPERTY_SEPARATOR = '#';
117
+ /**
118
+ * Name space for configuration binding keys
119
+ */
120
+ BindingKey.CONFIG_NAMESPACE = '$config';
121
+ //# sourceMappingURL=binding-key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binding-key.js","sourceRoot":"","sources":["../src/binding-key.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,iCAAiC;AACjC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,2CAA6C;AAI7C,6DAA6D;AAC7D,MAAa,UAAU;IA8BrB,YACkB,GAAW,EACX,YAAqB;QADrB,QAAG,GAAH,GAAG,CAAQ;QACX,iBAAY,GAAZ,YAAY,CAAS;IACpC,CAAC;IA9BJ;;;;;;;;;;;;;;OAcG;IACI,MAAM,CAAC,MAAM,CAAI,GAAW,EAAE,YAAqB;QACxD,qDAAqD;QACrD,6CAA6C;QAC7C,mDAAmD;QACnD,IAAI,YAAY,EAAE;YAChB,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACzB,OAAO,IAAI,UAAU,CAAI,GAAG,EAAE,YAAY,CAAC,CAAC;SAC7C;QAED,OAAO,UAAU,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;IAC1C,CAAC;IAOD,QAAQ;QACN,OAAO,IAAI,CAAC,YAAY;YACtB,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,kBAAkB,GAAG,IAAI,CAAC,YAAY,EAAE;YACnE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;IACf,CAAC;IAED;;;;;OAKG;IACH,YAAY,CAAoB,YAAoB;QAClD,qDAAqD;QACrD,6DAA6D;QAC7D,mDAAmD;QACnD,OAAO,UAAU,CAAC,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D,OAAO,UAAU,CAAC;IACpB,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,QAAQ,CAAI,SAAS,GAAG,EAAE;QAC/B,MAAM,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,GAAG,IAAA,4BAAgB,GAAE,CAAC;QAChC,OAAO,UAAU,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,IAAI,EAAE,CAAC,CAAC;IAC/C,CAAC;;AA5HH,gCA6HC;AA5HiB,6BAAkB,GAAG,GAAG,CAAC;AA8FzC;;GAEG;AACI,2BAAgB,GAAG,SAAS,CAAC"}
@@ -0,0 +1,71 @@
1
+ import { Binding } from './binding';
2
+ /**
3
+ * Compare function to sort an array of bindings.
4
+ * It is used by `Array.prototype.sort()`.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const compareByKey: BindingComparator = (a, b) => a.key.localeCompare(b.key);
9
+ * ```
10
+ */
11
+ export interface BindingComparator {
12
+ /**
13
+ * Compare two bindings
14
+ * @param bindingA - First binding
15
+ * @param bindingB - Second binding
16
+ * @returns A number to determine order of bindingA and bindingB
17
+ * - 0 leaves bindingA and bindingB unchanged
18
+ * - <0 bindingA comes before bindingB
19
+ * - >0 bindingA comes after bindingB
20
+ */
21
+ (bindingA: Readonly<Binding<unknown>>, bindingB: Readonly<Binding<unknown>>): number;
22
+ }
23
+ /**
24
+ * Creates a binding compare function to sort bindings by tagged phase name.
25
+ *
26
+ * @remarks
27
+ * Two bindings are compared as follows:
28
+ *
29
+ * 1. Get values for the given tag as `phase` for bindings, if the tag is not
30
+ * present, default `phase` to `''`.
31
+ * 2. If both bindings have `phase` value in `orderOfPhases`, honor the order
32
+ * specified by `orderOfPhases`.
33
+ * 3. If a binding's `phase` does not exist in `orderOfPhases`, it comes before
34
+ * the one with `phase` exists in `orderOfPhases`.
35
+ * 4. If both bindings have `phase` value outside of `orderOfPhases`, they are
36
+ * ordered by phase names alphabetically and symbol values come before string
37
+ * values.
38
+ *
39
+ * @param phaseTagName - Name of the binding tag for phase
40
+ * @param orderOfPhases - An array of phase names as the predefined order
41
+ */
42
+ export declare function compareBindingsByTag(phaseTagName?: string, orderOfPhases?: (string | symbol)[]): BindingComparator;
43
+ /**
44
+ * Compare two values by the predefined order
45
+ *
46
+ * @remarks
47
+ *
48
+ * The comparison is performed as follows:
49
+ *
50
+ * 1. If both values are included in `order`, they are sorted by their indexes in
51
+ * `order`.
52
+ * 2. The value included in `order` comes after the value not included in `order`.
53
+ * 3. If neither values are included in `order`, they are sorted:
54
+ * - symbol values come before string values
55
+ * - alphabetical order for two symbols or two strings
56
+ *
57
+ * @param a - First value
58
+ * @param b - Second value
59
+ * @param order - An array of values as the predefined order
60
+ */
61
+ export declare function compareByOrder(a: string | symbol | undefined | null, b: string | symbol | undefined | null, order?: (string | symbol)[]): number;
62
+ /**
63
+ * Sort bindings by phase names denoted by a tag and the predefined order
64
+ *
65
+ * @param bindings - An array of bindings
66
+ * @param phaseTagName - Tag name for phase, for example, we can use the value
67
+ * `'a'` of tag `order` as the phase name for `binding.tag({order: 'a'})`.
68
+ *
69
+ * @param orderOfPhases - An array of phase names as the predefined order
70
+ */
71
+ export declare function sortBindingsByPhase<T = unknown>(bindings: Readonly<Binding<T>>[], phaseTagName?: string, orderOfPhases?: (string | symbol)[]): Readonly<Binding<T>>[];