Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.');
}
}
// We have an instance of
if (dataSource instanceof juggler.DataSource) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
name = name || dataSource.name;
const key = `datasources.${name}`;
return this.bind(key)
.to(dataSource)
.tag('datasource');
} else if (typeof dataSource === 'function') {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
name = name || dataSource.dataSourceName;
const binding = createBindingFromClass(dataSource, {
name,
namespace: 'datasources',
type: 'datasource',
defaultScope: BindingScope.SINGLETON,
});
this.add(binding);
return binding;
} else {
throw new Error('not a valid DataSource.');
}
}
it('binds classes with @bind from a component', () => {
@bind({scope: BindingScope.SINGLETON, tags: ['foo']})
class MyClass {}
class MyComponentWithClasses implements Component {
classes = {'my-class': MyClass};
}
app.component(MyComponentWithClasses);
const binding = app.getBinding('my-class');
expect(binding.scope).to.eql(BindingScope.SINGLETON);
expect(binding.tagNames).to.containEql('foo');
});
it('honors @bind', async () => {
@bind({
tags: {
[CoreTags.LIFE_CYCLE_OBSERVER]: CoreTags.LIFE_CYCLE_OBSERVER,
[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]: 'my-group',
namespace: CoreBindings.LIFE_CYCLE_OBSERVERS,
},
scope: BindingScope.SINGLETON,
})
class MyObserverWithBind implements LifeCycleObserver {
status = 'not-initialized';
start() {
this.status = 'started';
}
stop() {
this.status = 'stopped';
}
}
const app = new Application();
const binding = createBindingFromClass(MyObserverWithBind);
app.add(binding);
expect(binding.tagMap[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]).to.eql(
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');
});
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);
});
async function givenLifeCycleRegistry() {
context.bind(CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS).to({
orderedGroups: DEFAULT_ORDERED_GROUPS,
parallel: false,
});
context
.bind(CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY)
.toClass(TestObserverRegistry)
.inScope(BindingScope.SINGLETON);
registry = (await context.get(
CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY,
)) as TestObserverRegistry;
}
const asRecommenderService: BindingTemplate = binding => {
extensionFor(RECOMMENDER_SERVICE)(binding);
binding.tag({protocol}).inScope(BindingScope.SINGLETON);
};
return asRecommenderService;
export function _bindBooter(
ctx: Context,
booterCls: Constructor,
): Binding {
const binding = createBindingFromClass(booterCls, {
namespace: BootBindings.BOOTER_PREFIX,
defaultScope: BindingScope.SINGLETON,
}).tag(BootTags.BOOTER);
ctx.add(binding);
/**
* Set up configuration binding as alias to `BootBindings.BOOT_OPTIONS`
* so that the booter can use `@config`.
*/
if (binding.tagMap.artifactNamespace) {
ctx
.configure(binding.key)
.toAlias(
`${BootBindings.BOOT_OPTIONS.key}#${binding.tagMap.artifactNamespace}`,
);
}
return binding;
}
* Bind current controller to the request context in `SINGLETON` scope.
* Within the same request, we always get the same instance of the
* current controller when `requestContext.get(CoreBindings.CONTROLLER_CURRENT)`
* is invoked.
*
* Please note the controller class itself can be bound to other scopes,
* such as SINGLETON or TRANSIENT (default) in the application or server
* context.
*
* - SINGLETON: all requests share the same instance of a given controller
* - TRANSIENT: each request has its own instance of a given controller
*/
requestContext
.bind(CoreBindings.CONTROLLER_CURRENT)
.toDynamicValue(() => this._controllerFactory(requestContext))
.inScope(BindingScope.SINGLETON);
requestContext.bind(CoreBindings.CONTROLLER_CLASS).to(this._controllerCtor);
requestContext
.bind(CoreBindings.CONTROLLER_METHOD_NAME)
.to(this._methodName);
requestContext.bind(RestBindings.OPERATION_SPEC_CURRENT).to(this.spec);
}