Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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());
}
}
export class NotBindableGreetingService {
greet(whom = 'world') {
import debugFactory from 'debug';
import {Message} from './types';
const debug = debugFactory('greeter-extension');
/**
* Configuration for CachingService
*/
export interface CachingServiceOptions {
// The time-to-live setting for a cache entry
ttl: number;
}
/**
* Message caching service
*/
@bind({scope: BindingScope.SINGLETON})
export class CachingService {
private timer: NodeJS.Timer;
private store: Map = new Map();
constructor(
@config.view()
private optionsView: ContextView,
) {
// Use a view so that we can listen on `refresh` events, which are emitted
// when the configuration binding is updated in the context.
optionsView.on('refresh', () => {
debug('Restarting the service as configuration changes...');
this.restart().catch(err => {
console.error('Cannot restart the caching service.', err);
process.exit(1);
});
it('binds singleton service from app.serviceProvider()', async () => {
@bind({scope: BindingScope.SINGLETON})
class SingletonGeocoderServiceProvider extends GeocoderServiceProvider {}
const myApp = new AppWithServiceMixin();
const binding = myApp.serviceProvider(SingletonGeocoderServiceProvider);
expect(binding.scope).to.equal(BindingScope.SINGLETON);
});
it('binds singleton repository from app.repository()', () => {
@bind({scope: BindingScope.SINGLETON})
class SingletonNoteRepo extends NoteRepo {}
const myApp = new AppWithRepoMixin();
const binding = myApp.repository(SingletonNoteRepo);
expect(binding.scope).to.equal(BindingScope.SINGLETON);
});
it('boots life cycle observers when app.boot() is called', async () => {
const expectedBinding = {
key: `${OBSERVER_PREFIX}.MyLifeCycleObserver`,
tags: [ContextTags.TYPE, OBSERVER_TAG],
scope: BindingScope.SINGLETON,
};
await app.boot();
const bindings = app
.findByTag(OBSERVER_TAG)
.map(b => ({key: b.key, tags: b.tagNames, scope: b.scope}));
expect(bindings).to.containEql(expectedBinding);
});
it('binds singleton service from app.serviceProvider()', async () => {
@bind({scope: BindingScope.SINGLETON})
class SingletonGeocoderServiceProvider extends GeocoderServiceProvider {}
const myApp = new AppWithServiceMixin();
const binding = myApp.serviceProvider(SingletonGeocoderServiceProvider);
expect(binding.scope).to.equal(BindingScope.SINGLETON);
});
it('binds singleton repository from app.repository()', () => {
@bind({scope: BindingScope.SINGLETON})
class SingletonNoteRepo extends NoteRepo {}
const myApp = new AppWithRepoMixin();
const binding = myApp.repository(SingletonNoteRepo);
expect(binding.scope).to.equal(BindingScope.SINGLETON);
});
it('adds bindings to the request context', async () => {
expect(requestCtx.contains(CoreBindings.CONTROLLER_CURRENT));
expect(
requestCtx.getBinding(CoreBindings.CONTROLLER_CURRENT).scope,
).to.equal(BindingScope.SINGLETON);
expect(await requestCtx.get(CoreBindings.CONTROLLER_CLASS)).to.equal(
MyController,
);
expect(
await requestCtx.get(CoreBindings.CONTROLLER_METHOD_NAME),
).to.equal('greet');
expect(await requestCtx.get(RestBindings.OPERATION_SPEC_CURRENT)).to.eql({
'x-controller-name': 'MyController',
'x-operation-name': 'greet',
tags: ['MyController'],
responses: {'200': {description: 'An undocumented response body.'}},
});
});