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

To help you get started, we’ve selected a few @loopback/core 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 / examples / greeter-extension / src / __tests__ / acceptance / greeter-extension.acceptance.ts View on Github external
it('honors a newly added/removed greeter binding', async () => {
    /**
     * A greeter implementation for French
     */
    @bind(asGreeter)
    class FrenchGreeter implements Greeter {
      language = 'fr';

      greet(name: string) {
        return `Bonjour, ${name}!`;
      }
    }
    // Add a new greeter for French
    const binding = createBindingFromClass(FrenchGreeter);
    app.add(binding);

    let msg = await greetingService.greet('fr', 'Raymond');
    expect(msg).to.eql('Bonjour, Raymond!');

    // Remove the French greeter
    app.unbind(binding.key);
github strongloop / loopback-next / packages / boot / src / __tests__ / fixtures / bindable-classes.artifact.ts View on Github external
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, BindingScope, Provider} from '@loopback/core';

@bind({
  tags: {serviceType: 'local'},
  scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
  greet(whom = 'world') {
    return Promise.resolve(`Hello ${whom}`);
  }
}

@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
  value(): Promise {
    return Promise.resolve(new Date());
  }
}
github strongloop / loopback-next / packages / boot / src / __tests__ / fixtures / bindable-classes.artifact.ts View on Github external
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, BindingScope, Provider} from '@loopback/core';

@bind({
  tags: {serviceType: 'local'},
  scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
  greet(whom = 'world') {
    return Promise.resolve(`Hello ${whom}`);
  }
}

@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
  value(): Promise {
    return Promise.resolve(new Date());
  }
}

export class NotBindableGreetingService {
  greet(whom = 'world') {
    return Promise.resolve(`Hello ${whom}`);
  }
}

export class NotBindableDateProvider implements Provider {
  value(): Promise {
    return Promise.resolve(new Date());
  }
github strongloop / loopback4-example-shopping / packages / shopping / src / services / recommender-grpc.service.ts View on Github external
import {inject, Provider, bind} from '@loopback/core';
import {RecommenderGrpcDataSource} from '../datasources';
import {Product} from '../models';
import {RecommenderService, recommender} from './recommender.service';

export interface RecommenderGrpc {
  // this is where you define the Node.js methods that will be
  // mapped to REST/SOAP/gRPC operations as stated in the datasource
  // json file.
  recommend(req: {userId: string}): Promise<{products: Product[]}>;
}

/**
 * gRPC based recommender service
 */
@bind(recommender('grpc'))
export class RecommenderGrpcServiceProvider
  implements Provider {
  constructor(
    // recommender must match the name property in the datasource json file
    @inject('datasources.recommender_grpc')
    protected dataSource: RecommenderGrpcDataSource = new RecommenderGrpcDataSource(),
  ) {}

  async value(): Promise {
    const grpcService = await getService(this.dataSource);
    const service: RecommenderService = {
      getProductRecommendations: async (userId: string) => {
        const res = await grpcService.recommend({userId});
        return res.products;
      },
    };
github strongloop / loopback-next / packages / rest-crud / src / crud-rest-builder.plugin.ts View on Github external
Entity,
  EntityCrudRepository,
  Model,
} from '@loopback/repository';
import * as debugFactory from 'debug';
import {defineCrudRestController} from './crud-rest.controller';
import {defineRepositoryClass} from './repository-builder';

const debug = debugFactory('loopback:boot:crud-rest');

export interface ModelCrudRestApiConfig extends ModelApiConfig {
  // E.g. '/products'
  basePath: string;
}

@bind(asModelApiBuilder)
export class CrudRestApiBuilder implements ModelApiBuilder {
  readonly pattern: string = 'CrudRest';

  build(
    application: ApplicationWithRepositories,
    modelClass: typeof Model & {prototype: Model},
    cfg: ModelApiConfig,
  ): Promise {
    const modelName = modelClass.name;
    const config = cfg as ModelCrudRestApiConfig;
    if (!config.basePath) {
      throw new Error(
        `Missing required field "basePath" in configuration for model ${modelName}.`,
      );
    }
github strongloop / loopback-next / packages / boot / src / __tests__ / unit / mixins / boot.mixin.unit.ts View on Github external
expect(compInstance).to.be.an.instanceOf(TestComponent);
    const booterInst = await app.get(
      `${BootBindings.BOOTER_PREFIX}.TestBooter`,
    );
    expect(booterInst).to.be.an.instanceOf(TestBooter);
  });

  class TestBooter implements Booter {
    configured = false;

    async configure() {
      this.configured = true;
    }
  }

  @bind({tags: {artifactType: 'xsd'}})
  class TestBooterWithBind implements Booter {
    configured = false;

    async configure() {
      this.configured = true;
    }
  }

  class AnotherTestBooter implements Booter {
    discovered = false;

    async discover() {
      this.discovered = true;
    }
  }