From 5c742f5e06120568f3529e33c219798e42e52774 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 3 Sep 2020 10:24:24 -0700 Subject: [PATCH] feat(boot): unify booter namespace to be `booters` Signed-off-by: Raymond Feng --- .../src/__tests__/unit/boot.component.unit.ts | 16 +++++++--------- .../src/__tests__/unit/bootstrapper.unit.ts | 4 ++-- .../__tests__/unit/mixins/boot.mixin.unit.ts | 16 +++++++--------- packages/boot/src/bootstrapper.ts | 2 +- packages/boot/src/keys.ts | 4 ++++ packages/boot/src/mixins/boot.mixin.ts | 4 +--- packages/boot/src/types.ts | 18 +++++++++++++++--- 7 files changed, 37 insertions(+), 27 deletions(-) diff --git a/packages/boot/src/__tests__/unit/boot.component.unit.ts b/packages/boot/src/__tests__/unit/boot.component.unit.ts index cb50adb63599..483475d480b5 100644 --- a/packages/boot/src/__tests__/unit/boot.component.unit.ts +++ b/packages/boot/src/__tests__/unit/boot.component.unit.ts @@ -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 '../../'; @@ -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); }); diff --git a/packages/boot/src/__tests__/unit/bootstrapper.unit.ts b/packages/boot/src/__tests__/unit/bootstrapper.unit.ts index fd973c520614..2e0402d980a2 100644 --- a/packages/boot/src/__tests__/unit/bootstrapper.unit.ts +++ b/packages/boot/src/__tests__/unit/bootstrapper.unit.ts @@ -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>; diff --git a/packages/boot/src/__tests__/unit/mixins/boot.mixin.unit.ts b/packages/boot/src/__tests__/unit/mixins/boot.mixin.unit.ts index aaa2765989a4..9d191dfaff8e 100644 --- a/packages/boot/src/__tests__/unit/mixins/boot.mixin.unit.ts +++ b/packages/boot/src/__tests__/unit/mixins/boot.mixin.unit.ts @@ -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'}); }); @@ -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); }); @@ -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); }); diff --git a/packages/boot/src/bootstrapper.ts b/packages/boot/src/bootstrapper.ts index 3c6ea88e2472..3a47d80246ca 100644 --- a/packages/boot/src/bootstrapper.ts +++ b/packages/boot/src/bootstrapper.ts @@ -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 => diff --git a/packages/boot/src/keys.ts b/packages/boot/src/keys.ts index eb8e8ea6eed8..9214ffff5a19 100644 --- a/packages/boot/src/keys.ts +++ b/packages/boot/src/keys.ts @@ -27,6 +27,10 @@ export namespace BootBindings { 'application.bootstrapper', ); + /** + * Booter binding namespace + */ + export const BOOTERS = 'booters'; export const BOOTER_PREFIX = 'booters'; } diff --git a/packages/boot/src/mixins/boot.mixin.ts b/packages/boot/src/mixins/boot.mixin.ts index cb2812c83b68..130c9f0e5c66 100644 --- a/packages/boot/src/mixins/boot.mixin.ts +++ b/packages/boot/src/mixins/boot.mixin.ts @@ -11,8 +11,6 @@ import { Constructor, Context, createBindingFromClass, -} from '@loopback/core'; -import { Application, Component, CoreBindings, @@ -235,7 +233,7 @@ export function bindBooter( booterCls: Constructor, ): Binding { const binding = createBindingFromClass(booterCls, { - namespace: BootBindings.BOOTER_PREFIX, + namespace: BootBindings.BOOTERS, defaultScope: BindingScope.SINGLETON, }).tag(BootTags.BOOTER); ctx.add(binding); diff --git a/packages/boot/src/types.ts b/packages/boot/src/types.ts index c0643f4525aa..0e188af9cf5d 100644 --- a/packages/boot/src/types.ts +++ b/packages/boot/src/types.ts @@ -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 @@ -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, ); }