Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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);
}
/**
// 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}!`;
}
}
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')
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);
});
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);
});
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);
});
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;
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;
}
export function lifeCycleObserver(group = '', ...specs: BindingSpec[]) {
return bind(
asLifeCycleObserver,
{
tags: {
[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]: group,
},
},
...specs,
);
}