Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
// 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());
}
}
// 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());
}
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;
},
};
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}.`,
);
}
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;
}
}