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

Method toProvider

packages/context/src/binding.ts:847–866  ·  view source on GitHub ↗

* Bind the key to a value computed by a Provider. * * * @example * * ```ts * export class DateProvider implements Provider { * constructor(@inject('stringDate') private param: String){} * value(): Date { * return new Date(param); * } * } * ``` *

(providerClass: Constructor<Provider<T>>)

Source from the content-addressed store, hash-verified

845 * @param provider - The value provider to use.
846 */
847 toProvider(providerClass: Constructor<Provider<T>>): this {
848 /* istanbul ignore if */
849 if (debug.enabled) {
850 debug('Bind %s to provider %s', this.key, providerClass.name);
851 }
852 this._source = {
853 type: BindingType.PROVIDER,
854 value: providerClass,
855 };
856 this._setValueGetter(resolutionCtx => {
857 const providerOrPromise = instantiateClass<Provider<T>>(
858 providerClass,
859 resolutionCtx.context,
860 resolutionCtx.options.session,
861 );
862 const value = transformValueOrPromise(providerOrPromise, p => p.value());
863 return Binding.valueOrProxy(resolutionCtx, value);
864 });
865 return this;
866 }
867
868 /**
869 * Bind the key to an instance of the given class.

Calls 5

_setValueGetterMethod · 0.95
instantiateClassFunction · 0.90
transformValueOrPromiseFunction · 0.90
valueOrProxyMethod · 0.80
valueMethod · 0.65

Tested by 8

setupBindingsFunction · 0.64
setupAuthorizationFunction · 0.64
givenContextFunction · 0.64
givenAClientFunction · 0.64
givenAClientFunction · 0.64
setupBindings2Function · 0.64