MCPcopy
hub / github.com/loopbackio/loopback-next / getValueOrPromise

Method getValueOrPromise

packages/context/src/context.ts:893–920  ·  view source on GitHub ↗

* Get the value bound to the given key. * * This is an internal version that preserves the dual sync/async result * of `Binding#getValue()`. Users should use `get()` or `getSync()` instead. * * @example * * ```ts * // get the value bound to "application.instance" * ctx.get

(
    keyWithPath: BindingAddress<ValueType>,
    optionsOrSession?: ResolutionOptionsOrSession,
  )

Source from the content-addressed store, hash-verified

891 * @internal
892 */
893 getValueOrPromise<ValueType>(
894 keyWithPath: BindingAddress<ValueType>,
895 optionsOrSession?: ResolutionOptionsOrSession,
896 ): ValueOrPromise<ValueType | undefined> {
897 const {key, propertyPath} = BindingKey.parseKeyWithPath(keyWithPath);
898
899 const options = asResolutionOptions(optionsOrSession);
900
901 const binding = this.getBinding<ValueType>(key, {optional: true});
902 if (binding == null) {
903 if (options.optional) return undefined;
904 throw new ResolutionError(
905 `The key '${key}' is not bound to any value in context ${this.name}`,
906 {
907 context: this,
908 binding: Binding.bind(key),
909 options,
910 },
911 );
912 }
913
914 const boundValue = binding.getValue(this, options);
915 return propertyPath == null || propertyPath === ''
916 ? boundValue
917 : transformValueOrPromise(boundValue, v =>
918 getDeepProperty<ValueType>(v, propertyPath),
919 );
920 }
921
922 /**
923 * Create a plain JSON object for the context

Callers 8

getMethod · 0.95
getSyncMethod · 0.95
resolveFunction · 0.80
toAliasMethod · 0.80
loadInterceptorMethod · 0.80
context.unit.tsFile · 0.80
getMethod · 0.80

Calls 7

getBindingMethod · 0.95
asResolutionOptionsFunction · 0.90
transformValueOrPromiseFunction · 0.90
getDeepPropertyFunction · 0.90
parseKeyWithPathMethod · 0.80
bindMethod · 0.45
getValueMethod · 0.45

Tested by

no test coverage detected