Skip to content

Commit

Permalink
feat(boot): unify booter namespace to be booters
Browse files Browse the repository at this point in the history
Signed-off-by: Raymond Feng <enjoyjava@gmail.com>
  • Loading branch information
raymondfeng committed Sep 3, 2020
1 parent 50448cb commit c8bdfda
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 27 deletions.
16 changes: 7 additions & 9 deletions packages/boot/src/__tests__/unit/boot.component.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import {Application} from '@loopback/core';
import {expect} from '@loopback/testlab';
import {
BootBindings,
BootMixin,
Bootstrapper,
ControllerBooter,
BootMixin,
RepositoryBooter,
DataSourceBooter,
RepositoryBooter,
ServiceBooter,
} from '../../';

Expand All @@ -29,29 +29,27 @@ describe('boot.component unit tests', () => {

it('ControllerBooter is bound as a booter by default', async () => {
const booterInst = await app.get(
`${BootBindings.BOOTER_PREFIX}.ControllerBooter`,
`${BootBindings.BOOTERS}.ControllerBooter`,
);
expect(booterInst).to.be.an.instanceOf(ControllerBooter);
});

it('RepositoryBooter is bound as a booter by default', async () => {
const booterInst = await app.get(
`${BootBindings.BOOTER_PREFIX}.RepositoryBooter`,
`${BootBindings.BOOTERS}.RepositoryBooter`,
);
expect(booterInst).to.be.an.instanceOf(RepositoryBooter);
});

it('DataSourceBooter is bound as a booter by default', async () => {
const booterInst = await app.get(
`${BootBindings.BOOTER_PREFIX}.DataSourceBooter`,
`${BootBindings.BOOTERS}.DataSourceBooter`,
);
expect(booterInst).to.be.an.instanceOf(DataSourceBooter);
});

it('ServiceBooter is bound as a booter by default', async () => {
const booterInst = await app.get(
`${BootBindings.BOOTER_PREFIX}.ServiceBooter`,
);
const booterInst = await app.get(`${BootBindings.BOOTERS}.ServiceBooter`);
expect(booterInst).to.be.an.instanceOf(ServiceBooter);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/boot/src/__tests__/unit/bootstrapper.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('boot-strapper unit tests', () => {

let app: BootApp;
let bootstrapper: Bootstrapper;
const booterKey = `${BootBindings.BOOTER_PREFIX}.TestBooter`;
const anotherBooterKey = `${BootBindings.BOOTER_PREFIX}.AnotherBooter`;
const booterKey = `${BootBindings.BOOTERS}.TestBooter`;
const anotherBooterKey = `${BootBindings.BOOTERS}.AnotherBooter`;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let stub: sinon.SinonStub<[any?, ...any[]], void>;

Expand Down
16 changes: 7 additions & 9 deletions packages/boot/src/__tests__/unit/mixins/boot.mixin.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ describe('BootMixin unit tests', () => {

it('binds booter from app.booters()', async () => {
app.booters(TestBooter);
const booter = await app.get(`${BootBindings.BOOTER_PREFIX}.TestBooter`);
const booter = await app.get(`${BootBindings.BOOTERS}.TestBooter`);
expect(booter).to.be.an.instanceOf(TestBooter);
});

it('binds booter with `@bind` from app.booters()', async () => {
app.booters(TestBooterWithBind);
const booterBinding = app.getBinding(
`${BootBindings.BOOTER_PREFIX}.TestBooterWithBind`,
`${BootBindings.BOOTERS}.TestBooterWithBind`,
);
expect(booterBinding.tagMap).to.containEql({artifactType: 'xsd'});
});
Expand All @@ -58,18 +58,18 @@ describe('BootMixin unit tests', () => {

it('binds booter from app.booters() as singletons by default', async () => {
app.booters(TestBooter);
const booter1 = await app.get(`${BootBindings.BOOTER_PREFIX}.TestBooter`);
const booter2 = await app.get(`${BootBindings.BOOTER_PREFIX}.TestBooter`);
const booter1 = await app.get(`${BootBindings.BOOTERS}.TestBooter`);
const booter2 = await app.get(`${BootBindings.BOOTERS}.TestBooter`);
expect(booter1).to.be.exactly(booter2);
});

it('binds multiple booter classes from app.booters()', async () => {
app.booters(TestBooter, AnotherTestBooter);
const booter = await app.get(`${BootBindings.BOOTER_PREFIX}.TestBooter`);
const booter = await app.get(`${BootBindings.BOOTERS}.TestBooter`);
expect(booter).to.be.an.instanceOf(TestBooter);

const anotherBooter = await app.get(
`${BootBindings.BOOTER_PREFIX}.AnotherTestBooter`,
`${BootBindings.BOOTERS}.AnotherTestBooter`,
);
expect(anotherBooter).to.be.an.instanceOf(AnotherTestBooter);
});
Expand All @@ -90,9 +90,7 @@ describe('BootMixin unit tests', () => {
app.component(TestComponent);
const compInstance = await app.get('components.TestComponent');
expect(compInstance).to.be.an.instanceOf(TestComponent);
const booterInst = await app.get(
`${BootBindings.BOOTER_PREFIX}.TestBooter`,
);
const booterInst = await app.get(`${BootBindings.BOOTERS}.TestBooter`);
expect(booterInst).to.be.an.instanceOf(TestBooter);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/boot/src/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Bootstrapper {
const bindings = bootCtx.findByTag(BootTags.BOOTER);

// Prefix length. +1 because of `.` => 'booters.'
const prefixLength = BootBindings.BOOTER_PREFIX.length + 1;
const prefixLength = BootBindings.BOOTERS.length + 1;

// Names of all registered booters.
const defaultBooterNames = bindings.map(binding =>
Expand Down
4 changes: 4 additions & 0 deletions packages/boot/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export namespace BootBindings {
'application.bootstrapper',
);

/**
* Booter binding namespace
*/
export const BOOTERS = 'booters';
export const BOOTER_PREFIX = 'booters';
}

Expand Down
4 changes: 1 addition & 3 deletions packages/boot/src/mixins/boot.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
Constructor,
Context,
createBindingFromClass,
} from '@loopback/core';
import {
Application,
Component,
CoreBindings,
Expand Down Expand Up @@ -235,7 +233,7 @@ export function bindBooter(
booterCls: Constructor<Booter>,
): Binding {
const binding = createBindingFromClass(booterCls, {
namespace: BootBindings.BOOTER_PREFIX,
namespace: BootBindings.BOOTERS,
defaultScope: BindingScope.SINGLETON,
}).tag(BootTags.BOOTER);
ctx.add(binding);
Expand Down
18 changes: 15 additions & 3 deletions packages/boot/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, Binding, BindingSpec, Constructor} from '@loopback/core';
import {BootTags} from './keys';
import {
bind,
Binding,
BindingSpec,
Constructor,
ContextTags,
} from '@loopback/core';
import {BootBindings, BootTags} from './keys';

/**
* Type definition for ArtifactOptions. These are the options supported by
Expand Down Expand Up @@ -155,7 +161,13 @@ export interface Bootable {
*/
export function booter(artifactNamespace: string, ...specs: BindingSpec[]) {
return bind(
{tags: {artifactNamespace, [BootTags.BOOTER]: BootTags.BOOTER}},
{
tags: {
artifactNamespace,
[BootTags.BOOTER]: BootTags.BOOTER,
[ContextTags.NAMESPACE]: BootBindings.BOOTERS,
},
},
...specs,
);
}
Expand Down

0 comments on commit c8bdfda

Please sign in to comment.