How to use @loopback/context - 10 common examples

To help you get started, we’ve selected a few @loopback/context examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / metadata.unit.ts View on Github external
it('returns cached metadata instead of recreating it', () => {
      const classMeta = MetadataInspector.getClassMetadata(
        MODEL_KEY,
        Phlange,
      ) as ModelDefinition;
      classMeta.properties = {
        foo: {
          type: String,
        },
      };
      // Intentionally change the metadata to be different from the Phlange
      // class metadata
      MetadataInspector.defineMetadata(
        MODEL_WITH_PROPERTIES_KEY.key,
        classMeta,
        Phlange,
      );
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / metadata.unit.ts View on Github external
it('returns cached metadata instead of recreating it', () => {
      const classMeta = MetadataInspector.getClassMetadata(
        MODEL_KEY,
        Phlange,
      ) as ModelDefinition;
      classMeta.properties = {
        foo: {
          type: String,
        },
      };
      // Intentionally change the metadata to be different from the Phlange
      // class metadata
      MetadataInspector.defineMetadata(
        MODEL_WITH_PROPERTIES_KEY.key,
        classMeta,
        Phlange,
      );

      const meta = ModelMetadataHelper.getModelMetadata(
        Phlange,
      ) as ModelDefinition;
      expect(meta.properties).to.eql(classMeta.properties);
    });
  });
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / model-and-relation.decorator.unit.ts View on Github external
it('adds belongsTo metadata', () => {
    const meta =
      MetadataInspector.getAllPropertyMetadata(
        RELATIONS_KEY,
        Order.prototype,
      ) ?? /* istanbul ignore next */ {};
    const relationDef = meta.customerId;
    expect(relationDef).to.containEql({
      type: RelationType.belongsTo,
      name: 'customer',
      target: () => Customer,
      keyFrom: 'customerId',
    });
    expect(relationDef.source).to.be.exactly(Order);
    expect(relationDef.target()).to.be.exactly(Customer);
  });
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / model-and-relation.decorator.unit.ts View on Github external
it('"@property.array" adds array metadata', () => {
        @model()
        class TestModel {
          @property.array(Product)
          items: Product[];
        }

        const meta =
          MetadataInspector.getAllPropertyMetadata(
            MODEL_PROPERTIES_KEY,
            TestModel.prototype,
          ) ?? /* istanbul ignore next */ {};
        expect(meta.items).to.eql({type: Array, itemType: Product});
      });
github strongloop / loopback-next / packages / boot / src / __tests__ / fixtures / non-global-interceptor.artifact.ts View on Github external
// License text available at https://opensource.org/licenses/MIT

import {
  bind,
  Interceptor,
  InvocationContext,
  InvocationResult,
  Provider,
  ValueOrPromise,
} from '@loopback/context';

/**
 * This class will be bound to the application as a global `Interceptor` during
 * `boot`
 */
@bind({tags: {namespace: 'interceptors', name: 'myInterceptor'}})
export class MyInterceptor implements Provider {
  /*
  constructor() {}
  */

  /**
   * This method is used by LoopBack context to produce an interceptor function
   * for the binding.
   *
   * @returns An interceptor function
   */
  value() {
    return this.intercept.bind(this);
  }

  /**
github strongloop / loopback-next / examples / context / src / find-bindings.ts View on Github external
export async function main() {
  const ctx = new Context('request');

  // Add EnglishGreeter for now
  ctx.add(createBindingFromClass(EnglishGreeter, {namespace: 'greeters'}));

  // Add ChineseGreeter
  ctx
    .bind('greeters.ChineseGreeter')
    .toClass(ChineseGreeter)
    .tag('greeter');

  const enlishGreeterBinding = ctx.getBinding('greeters.EnglishGreeter');
  console.log(enlishGreeterBinding.key);

  let possibleEnglishGreeters = ctx.find('*.EnglishGreeter');
  console.log(possibleEnglishGreeters.map(b => b.key));
github strongloop / loopback-next / examples / context / src / find-bindings.ts View on Github external
export async function main() {
  const ctx = new Context('request');

  // Add EnglishGreeter for now
  ctx.add(createBindingFromClass(EnglishGreeter, {namespace: 'greeters'}));

  // Add ChineseGreeter
  ctx
    .bind('greeters.ChineseGreeter')
    .toClass(ChineseGreeter)
    .tag('greeter');

  const enlishGreeterBinding = ctx.getBinding('greeters.EnglishGreeter');
  console.log(enlishGreeterBinding.key);

  let possibleEnglishGreeters = ctx.find('*.EnglishGreeter');
  console.log(possibleEnglishGreeters.map(b => b.key));

  possibleEnglishGreeters = ctx.find(/\w+\.EnglishGreeter$/);
  console.log(possibleEnglishGreeters.map(b => b.key));
github strongloop / loopback-next / examples / context / src / dependency-injection.ts View on Github external
// Now add ChineseGreeter
  ctx
    .bind('greeters.ChineseGreeter')
    .toClass(ChineseGreeter)
    .tag('greeter');

  greeting = await invokeMethod(greetingService, 'greet', ctx);
  console.log(greeting);

  // Change the current language to `en`
  ctx.bind('currentLanguage').to('en');

  // Change the current user to `Jane`
  ctx.bind('currentUser').to('Jane');

  greeting = await invokeMethod(greetingService, 'greet', ctx);
  console.log(greeting);
}
github strongloop / loopback-next / examples / context / src / context-observation.ts View on Github external
export async function main() {
  const appCtx = new Context('app');
  const requestCtx = new RequestContext(appCtx, 'request');

  // Observer events from `appCtx`
  appCtx.subscribe({
    filter: filterByTag('greeter'),
    observe: (eventType, binding) => {
      console.log('[observer] %s %s', eventType, binding.key);
    },
  });

  // Create a context view on `requestCtx`
  const greetersView = requestCtx.createView(filterByKey(/^greeters\./));
  greetersView.on('refresh', () => {
    console.log(
      '[view.refresh] %j',
      greetersView.bindings.map(b => b.key),
    );
  });

  // Add EnglishGreeter to `appCtx`
  console.log('Adding EnglishGreeter');
github strongloop / loopback-next / packages / boot / src / booters / interceptor.booter.ts View on Github external
constructor(
    @inject(CoreBindings.APPLICATION_INSTANCE)
    public app: Application,
    @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
    @config()
    public interceptorConfig: ArtifactOptions = {},
  ) {
    super(
      projectRoot,
      // Set InterceptorProvider Booter Options if passed in via bootConfig
      Object.assign({}, InterceptorProviderDefaults, interceptorConfig),
    );
  }