How to use the @loopback/context.bind function in @loopback/context

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 / 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 / greeter-extension / src / greeters / greeter-en.ts View on Github external
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/example-greeter-extension
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind} from '@loopback/context';
import {asGreeter, Greeter} from '../types';

/**
 * A greeter implementation for English
 */
@bind(asGreeter)
export class EnglishGreeter implements Greeter {
  language = 'en';

  greet(name: string) {
    return `Hello, ${name}!`;
  }
}
github strongloop / loopback-next / examples / context / src / find-bindings.ts View on Github external
const asGreeter: BindingTemplate = binding => {
  binding.tag('greeter');
};

const greeterFilter: BindingFilter = binding =>
  binding.tagMap['greeter'] != null;

class ChineseGreeter implements Greeter {
  language = 'zh';
  greet(name: string) {
    return `你好,${name}!`;
  }
}

@bind(asGreeter)
class EnglishGreeter implements Greeter {
  language = 'en';
  greet(name: string) {
    return `Hello, ${name}!`;
  }
}

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')
github strongloop / loopback-next / packages / core / src / __tests__ / unit / application.unit.ts View on Github external
it('binds a singleton service', () => {
      @bind({scope: BindingScope.SINGLETON})
      class MySingletonService {}

      const binding = app.service(MySingletonService);
      expect(binding.scope).to.equal(BindingScope.SINGLETON);
      expect(findKeysByTag(app, 'service')).to.containEql(binding.key);
    });
github strongloop / loopback-next / packages / core / src / __tests__ / unit / application.unit.ts View on Github external
it('binds a service provider', () => {
      @bind({tags: {date: 'now', namespace: 'localServices'}})
      class MyServiceProvider implements Provider {
        value() {
          return new Date();
        }
      }

      const binding = app.service(MyServiceProvider);
      expect(Array.from(binding.tagNames)).to.containEql(CoreTags.SERVICE);
      expect(binding.tagMap.date).to.eql('now');
      expect(binding.key).to.equal('localServices.MyService');
      expect(binding.scope).to.equal(BindingScope.TRANSIENT);
      expect(findKeysByTag(app, 'service')).to.containEql(binding.key);
    });
github strongloop / loopback-next / packages / core / src / __tests__ / unit / application.unit.ts View on Github external
it('binds a singleton controller', () => {
      @bind({scope: BindingScope.SINGLETON})
      class MySingletonController {}

      const binding = app.controller(MySingletonController);
      expect(binding.scope).to.equal(BindingScope.SINGLETON);
      expect(findKeysByTag(app, 'controller')).to.containEql(binding.key);
    });
github strongloop / loopback-next / packages / core / src / __tests__ / unit / lifecycle-registry.unit.ts View on Github external
function givenAsyncObserver(name: string, group = '', delayInMs = 0) {
    @bind({tags: {[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]: group}})
    class MyAsyncObserver implements LifeCycleObserver {
      async start() {
        await sleep(delayInMs);
        events.push(`${name}-start`);
      }
      async stop() {
        await sleep(delayInMs);
        events.push(`${name}-stop`);
      }
    }
    const binding = createBindingFromClass(MyAsyncObserver, {
      key: `observers.observer-${name}`,
    }).apply(asLifeCycleObserver);
    context.add(binding);

    return MyAsyncObserver;
github strongloop / loopback-next / examples / context / src / parameterized-decoration.ts View on Github external
function createClassWithDecoration(
  bindingKeyForName: BindingAddress,
  ...tags: BindingTag[]
): Constructor {
  @bind({tags})
  class GreeterTemplate implements Greeter {
    constructor(@inject(bindingKeyForName) private userName: string) {}

    hello() {
      return `Hello, ${this.userName}`;
    }
  }
  return GreeterTemplate;
}
github strongloop / loopback-next / packages / core / src / lifecycle.ts View on Github external
export function lifeCycleObserver(group = '', ...specs: BindingSpec[]) {
  return bind(
    asLifeCycleObserver,
    {
      tags: {
        [CoreTags.LIFE_CYCLE_OBSERVER_GROUP]: group,
      },
    },
    ...specs,
  );
}