Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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));
// License text available at https://opensource.org/licenses/MIT
import {createBindingFromClass} from '@loopback/context';
import {Component} from '@loopback/core';
import {ChineseGreeter} from './greeters/greeter-cn';
import {EnglishGreeter} from './greeters/greeter-en';
import {GreetingService} from './greeting-service';
import {GREETING_SERVICE} from './keys';
/**
* Define a component to register the greeter extension point and built-in
* extensions
*/
export class GreetingComponent implements Component {
bindings = [
createBindingFromClass(GreetingService, {
key: GREETING_SERVICE,
}),
createBindingFromClass(EnglishGreeter),
createBindingFromClass(ChineseGreeter),
];
}
dataSource(
dataSource: Class | D,
name?: string,
): Binding {
// We have an instance of
if (dataSource instanceof juggler.DataSource) {
const key = `datasources.${name || dataSource.name}`;
return this.bind(key)
.to(dataSource)
.tag('datasource');
} else if (typeof dataSource === 'function') {
const binding = createBindingFromClass(dataSource, {
name: name || dataSource.dataSourceName,
namespace: 'datasources',
type: 'datasource',
defaultScope: BindingScope.SINGLETON,
});
this.add(binding);
return binding;
} else {
throw new Error('not a valid DataSource.');
}
}
it('honors @lifeCycleObserver', async () => {
const app = new Application();
const binding = createBindingFromClass(MyObserverWithDecorator);
app.add(binding);
expect(binding.tagMap[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]).to.eql(
'my-group',
);
expect(binding.scope).to.eql(BindingScope.SINGLETON);
const observer = await app.get(binding.key);
expect(observer.status).to.equal('not-initialized');
await app.start();
expect(observer.status).to.equal('started');
await app.stop();
expect(observer.status).to.equal('stopped');
});
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;
}
});
export async function main() {
const ctx = new Context();
ctx.bind('name1').to('John');
ctx.bind('name2').to('Jane');
const class1 = createClassWithDecoration('name1', {tags: {prefix: '1'}});
const binding1 = createBindingFromClass(class1, {key: 'greeter1'});
ctx.add(binding1);
console.log('1:', binding1.tagMap);
const class2 = createClassWithDecoration('name2', {tags: {prefix: '2'}});
const binding2 = createBindingFromClass(class2, {key: 'greeter2'});
ctx.add(binding2);
console.log('2:', binding2.tagMap);
const greeting1 = await ctx.get('greeter1');
console.log('1: %s', greeting1.hello());
const greeting2 = await ctx.get('greeter2');
console.log('2: %s', greeting2.hello());
}
export async function main() {
const ctx = new Context();
ctx.bind('name1').to('John');
ctx.bind('name2').to('Jane');
const class1 = createClassWithDecoration('name1', {tags: {prefix: '1'}});
const binding1 = createBindingFromClass(class1, {key: 'greeter1'});
ctx.add(binding1);
console.log('1:', binding1.tagMap);
const class2 = createClassWithDecoration('name2', {tags: {prefix: '2'}});
const binding2 = createBindingFromClass(class2, {key: 'greeter2'});
ctx.add(binding2);
console.log('2:', binding2.tagMap);
const greeting1 = await ctx.get('greeter1');
console.log('1: %s', greeting1.hello());
const greeting2 = await ctx.get('greeter2');
console.log('2: %s', greeting2.hello());
}
import {Component} from '@loopback/core';
import {ChineseGreeter} from './greeters/greeter-cn';
import {EnglishGreeter} from './greeters/greeter-en';
import {GreetingService} from './greeting-service';
import {GREETING_SERVICE} from './keys';
/**
* Define a component to register the greeter extension point and built-in
* extensions
*/
export class GreetingComponent implements Component {
bindings = [
createBindingFromClass(GreetingService, {
key: GREETING_SERVICE,
}),
createBindingFromClass(EnglishGreeter),
createBindingFromClass(ChineseGreeter),
];
}
async load() {
await super.load();
this.interceptors = this.classes as InterceptorProviderClass[];
for (const interceptor of this.interceptors) {
debug('Bind interceptor: %s', interceptor.name);
const binding = createBindingFromClass(interceptor, {
defaultScope: BindingScope.TRANSIENT,
});
this.app.add(binding);
debug('Binding created for interceptor: %j', binding);
}
}
}
repository>(
repoClass: Class,
name?: string,
): Binding {
const binding = createBindingFromClass(repoClass, {
name,
namespace: 'repositories',
type: 'repository',
defaultScope: BindingScope.TRANSIENT,
});
this.add(binding);
return binding;
}