* Build the binding key for a class with optional binding metadata. * The binding key is resolved in the following steps: * * 1. Check `options.key`, if it exists, return it * 2. Check if the binding metadata has `key` tag, if yes, return its tag value * 3. Identify `namespace` and `name` to fo
(
cls: Constructor<T | Provider<T>>,
options: BindingFromClassOptions = {},
)
| 332 | * @typeParam T - Value type |
| 333 | */ |
| 334 | function buildBindingKey<T>( |
| 335 | cls: Constructor<T | Provider<T>>, |
| 336 | options: BindingFromClassOptions = {}, |
| 337 | ) { |
| 338 | if (options.key) return options.key; |
| 339 | |
| 340 | const templateFn = bindingTemplateFor(cls); |
| 341 | // Create a temporary binding |
| 342 | const bindingTemplate = new Binding('template').apply(templateFn); |
| 343 | // Is there a `key` tag? |
| 344 | let key: string = bindingTemplate.tagMap[ContextTags.KEY]; |
| 345 | if (key) return key; |
| 346 | |
| 347 | let namespace = |
| 348 | options.namespace ?? |
| 349 | bindingTemplate.tagMap[ContextTags.NAMESPACE] ?? |
| 350 | options.defaultNamespace; |
| 351 | if (!namespace) { |
| 352 | const namespaces = Object.assign( |
| 353 | {}, |
| 354 | DEFAULT_TYPE_NAMESPACES, |
| 355 | options.typeNamespaceMapping, |
| 356 | ); |
| 357 | // Derive the key from type + name |
| 358 | let type = options.type ?? bindingTemplate.tagMap[ContextTags.TYPE]; |
| 359 | if (!type) { |
| 360 | type = |
| 361 | bindingTemplate.tagNames.find(t => namespaces[t] != null) ?? |
| 362 | ContextTags.CLASS; |
| 363 | } |
| 364 | namespace = getNamespace(type, namespaces); |
| 365 | } |
| 366 | |
| 367 | const name = |
| 368 | options.name ?? (bindingTemplate.tagMap[ContextTags.NAME] || cls.name); |
| 369 | key = `${namespace}.${name}`; |
| 370 | |
| 371 | return key; |
| 372 | } |
no test coverage detected