Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(context): add binding.toAlias() #2635

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
raymondfeng marked this conversation as resolved.
Show resolved Hide resolved
const childOptions = ctx.getSync('child.options');
expect(childOptions).to.eql({disabled: true});
});

it('creates the alias even when the target is not bound yet', () => {
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 @@ -118,6 +118,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 @@ -506,6 +510,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) {
raymondfeng marked this conversation as resolved.
Show resolved Hide resolved
debug('Bind %s to alias %s', this.key, keyWithPath);
}
this._type = BindingType.ALIAS;
this._setValueGetter((ctx, optionsOrSession) => {
const options = asResolutionOptions(optionsOrSession);
return ctx.getValueOrPromise(keyWithPath, options);
});
return this;
}

/**
* Unlock the binding
*/
Expand Down