MCPcopy Index your code
hub / github.com/loopbackio/loopback-next / buildBindingKey

Function buildBindingKey

packages/context/src/binding-inspector.ts:334–372  ·  view source on GitHub ↗

* 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 = {},
)

Source from the content-addressed store, hash-verified

332 * @typeParam T - Value type
333 */
334function 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}

Callers 1

createBindingFromClassFunction · 0.85

Calls 4

bindingTemplateForFunction · 0.85
getNamespaceFunction · 0.85
applyMethod · 0.80
findMethod · 0.65

Tested by

no test coverage detected