Skip to content

Commit

Permalink
feat(core): enable application config with configure/getConfig/@config
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jun 17, 2020
1 parent a6cf16e commit 3a74ee1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
12 changes: 10 additions & 2 deletions docs/site/Context.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,13 +684,21 @@ We also allow `@config.*` to be resolved from another binding than the current
one:

```ts
import {config, CoreBindings} from '@loopback/core';

export class MyRestServer {
constructor(
// Inject the `rest.host` from the application config
@config({fromBinding: 'application', propertyPath: 'rest.host'})
@config({
fromBinding: CoreBinding.APPLICATION_INSTANCE,
propertyPath: 'rest.host',
})
host: string,
// Inject the `rest.port` from the application config
@config({fromBinding: 'application', propertyPath: 'rest.port'})
@config({
fromBinding: CoreBinding.APPLICATION_INSTANCE,
propertyPath: 'rest.port',
})
port: number,
) {
// ...
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/__tests__/unit/application.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ describe('Application', () => {

afterEach('clean up application', () => app.stop());

describe('app bindings', () => {
it('binds the application itself', () => {
app = new Application();
expect(app.getSync(CoreBindings.APPLICATION_INSTANCE)).to.equal(app);
});

it('binds the application config', () => {
const myAppConfig = {name: 'my-app', port: 3000};
app = new Application(myAppConfig);
expect(app.getSync(CoreBindings.APPLICATION_CONFIG)).to.equal(
myAppConfig,
);
});

it('configures the application', () => {
const myAppConfig = {name: 'my-app', port: 3000};
app = new Application(myAppConfig);
expect(app.getConfigSync(CoreBindings.APPLICATION_INSTANCE)).to.equal(
myAppConfig,
);
});
});

describe('controller binding', () => {
beforeEach(givenApp);

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export class Application extends Context implements LifeCycleObserver {
// Make options available to other modules as well.
this.bind(CoreBindings.APPLICATION_CONFIG).to(this.options);

// Also configure the application instance to allow `@config`
this.configure(CoreBindings.APPLICATION_INSTANCE).toAlias(
CoreBindings.APPLICATION_CONFIG,
);

this._shutdownOptions = {signals: ['SIGTERM'], ...this.options.shutdown};
}

Expand Down

0 comments on commit 3a74ee1

Please sign in to comment.