Skip to content

Commit

Permalink
feat(context): add binding.toAlias() to resolve values from another b…
Browse files Browse the repository at this point in the history
…inding
  • Loading branch information
raymondfeng committed Mar 28, 2019
1 parent 29a748e commit b216ab9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/site/Binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ class MyValueProvider implements Provider<string> {
binding.toProvider(MyValueProvider);
```

#### An alias

An alias is the key with optional path to resolve the value from another
binding. For example, if we want to get options from RestServer for the API
explorer, we can configure the `apiExplorer.options` to be resolved from
`servers.RestServer.options#apiExplorer`.

```ts
ctx.bind('servers.RestServer.options').to({apiExplorer: {path: '/explorer'}});
ctx
.bind('apiExplorer.options')
.toAlias('servers.RestServer.options#apiExplorer');
const apiExplorerOptions = await ctx.get('apiExplorer.options'); // => {path: '/explorer'}
```

### Configure the scope

We allow a binding to be resolved within a context using one of the following
Expand Down
58 changes: 58 additions & 0 deletions packages/context/src/__tests__/unit/binding.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,64 @@ describe('Binding', () => {
});
});

describe('toAlias(bindingKeyWithPath)', async () => {
it('binds to another binding with sync value', () => {
ctx.bind('parent.options').to({child: {disabled: true}});
ctx.bind('child.options').toAlias('parent.options#child');
const childOptions = ctx.getSync('child.options');
expect(childOptions).to.eql({disabled: true});
});

it('binds to another binding before the it is bound', () => {
ctx.bind('child.options').toAlias('parent.options#child');
ctx.bind('parent.options').to({child: {disabled: true}});
const childOptions = ctx.getSync('child.options');
expect(childOptions).to.eql({disabled: true});
});

it('binds to another binding with async value', async () => {
ctx
.bind('parent.options')
.toDynamicValue(() => Promise.resolve({child: {disabled: true}}));
ctx.bind('child.options').toAlias('parent.options#child');
const childOptions = await ctx.get('child.options');
expect(childOptions).to.eql({disabled: true});
});

it('reports error if alias binding cannot be resolved', () => {
ctx.bind('child.options').toAlias('parent.options#child');
expect(() => ctx.getSync('child.options')).to.throw(
/The key 'parent.options' is not bound to any value in context/,
);
});

it('reports error if alias binding cannot be resolved - async', async () => {
ctx.bind('child.options').toAlias('parent.options#child');
return expect(ctx.get('child.options')).to.be.rejectedWith(
/The key 'parent.options' is not bound to any value in context/,
);
});

it('allows optional if alias binding cannot be resolved', () => {
ctx.bind('child.options').toAlias('parent.options#child');
const childOptions = ctx.getSync('child.options', {optional: true});
expect(childOptions).to.be.undefined();
});

it('allows optional if alias binding cannot be resolved - async', async () => {
ctx.bind('child.options').toAlias('parent.options#child');
const childOptions = await ctx.get('child.options', {optional: true});
expect(childOptions).to.be.undefined();
});

it('sets type to ALIAS', () => {
const childBinding = ctx
.bind('child.options')
.toAlias('parent.options#child');
expect(childBinding.type).to.equal(BindingType.ALIAS);
});
});

describe('apply(templateFn)', () => {
it('applies a template function', async () => {
binding.apply(b => {
Expand Down
22 changes: 22 additions & 0 deletions packages/context/src/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ export enum BindingType {
* A provider class with `value()` function to get the value
*/
PROVIDER = 'Provider',
/**
* A alias to another binding key with optional path
*/
ALIAS = 'Alias',
}

// tslint:disable-next-line:no-any
Expand Down Expand Up @@ -497,6 +501,24 @@ export class Binding<T = BoundValue> {
return this;
}

/**
* Bind the key to an alias of another binding
* @param keyWithPath Target binding key with optional path,
* such as `servers.RestServer.options#apiExplorer`
*/
toAlias(keyWithPath: BindingAddress<T>) {
/* istanbul ignore if */
if (debug.enabled) {
debug('Bind %s to alias %s', this.key, keyWithPath);
}
this._type = BindingType.ALIAS;
this._setGetValue((ctx, optionsOrSession) => {
const options = asResolutionOptions(optionsOrSession);
return ctx.getValueOrPromise(keyWithPath, options);
});
return this;
}

/**
* Unlock the binding
*/
Expand Down

0 comments on commit b216ab9

Please sign in to comment.