diff --git a/packages/core/README.md b/packages/core/README.md index 094a11018cb2..c8a93c40c08e 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -46,7 +46,10 @@ const app = new Application({ port: 3001, }, }); -app.components([RestComponent, GrpcComponent]); +app.components([ + RestComponent, // REST Server + GrpcComponent, // GRPC Server + ]); (async function start() { // Let's retrieve the bound instances of our servers. diff --git a/packages/core/src/application.ts b/packages/core/src/application.ts index 5aaaa9ab5ef1..82d1acfe8e85 100644 --- a/packages/core/src/application.ts +++ b/packages/core/src/application.ts @@ -48,6 +48,26 @@ export class Application extends Context { .tag('controller'); } + /** + * Bind an array of controllers to the Application's master + * context. + * Each controller added in this way will automatically be named based on + * the class constructor name with the "controllers." prefix. + * + * If you wish to control the binding keys for particular controller + * instances, use the app.controller function instead. + * ```ts + * app.controllers([ + * FooController, + * BarController, + * ]); + * // Creates a binding for "controllers.FooController" and a binding for + * // "controllers.BarController"; + * ``` + * + * @param ctors Array of controller classes. + * @returns {Binding[]} An array of bindings for the registered controllers. + */ controllers(ctors: ControllerClass[]): Binding[] { return ctors.map(ctor => this.controller(ctor)); } @@ -199,6 +219,25 @@ export class Application extends Context { mountComponent(this, instance); } + /** + * Add multiple components to this application and register their + * extensions. + * + * Each component added in this way will automatically be named based on the + * class constructor name with the "components." prefix. + * + * If you wish to control the binding keys for particular instances, + * use the app.component function instead. + * ```ts + * app.components([ + * RestComponent, + * GRPCComponent, + * ]); + * // Creates a binding for "components.RestComponent" and a binding for + * // "components.GRPCComponent"; + * ``` + * @param ctors Array of components to add. + */ public components(ctors: Constructor[]) { ctors.map(ctor => this.component(ctor)); } diff --git a/packages/core/test/unit/application.test.ts b/packages/core/test/unit/application.test.ts index 972a541d3e04..e1c452c64536 100644 --- a/packages/core/test/unit/application.test.ts +++ b/packages/core/test/unit/application.test.ts @@ -109,13 +109,13 @@ describe('Application', () => { it('allows binding of multiple servers as an array', async () => { const app = new Application(); - const bindings = app.servers([FakeServer, FakerServer]); + const bindings = app.servers([FakeServer, AnotherServer]); expect(Array.from(bindings[0].tags)).to.containEql('server'); expect(Array.from(bindings[1].tags)).to.containEql('server'); const fakeResult = await app.getServer(FakeServer); expect(fakeResult.constructor.name).to.equal(FakeServer.name); - const fakerResult = await app.getServer(FakerServer); - expect(fakerResult.constructor.name).to.equal(FakerServer.name); + const AnotherResult = await app.getServer(AnotherServer); + expect(AnotherResult.constructor.name).to.equal(AnotherServer.name); }); }); @@ -138,4 +138,4 @@ class FakeServer extends Context implements Server { } } -class FakerServer extends FakeServer {} +class AnotherServer extends FakeServer {}