Skip to content

Commit

Permalink
feat(context): allows default namespace for bindings from classes
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 4, 2020
1 parent a1352cb commit ec2da01
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/site/Binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ parameter of `BindingFromClassOptions` type with the following settings:
}
```
- defaultNamespace: Default namespace if namespace or namespace tag does not
exist
- defaultScope: Default scope if the binding does not have an explicit scope
set. The `scope` from `@bind` of the bound class takes precedence.
Expand Down
28 changes: 28 additions & 0 deletions packages/context/src/__tests__/unit/binding-inspector.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createBindingFromClass,
Provider,
} from '../..';
import {ContextTags} from '../../keys';

describe('createBindingFromClass()', () => {
it('inspects classes', () => {
Expand Down Expand Up @@ -206,6 +207,33 @@ describe('createBindingFromClass()', () => {
expect(binding.key).to.eql('services.MyService');
});

it('honors default namespace with options', () => {
class MyService {}

@bind({tags: {[ContextTags.NAMESPACE]: 'my-services'}})
class MyServiceWithNS {}

const ctx = new Context();
let binding = givenBindingFromClass(MyService, ctx, {
defaultNamespace: 'services',
});

expect(binding.key).to.eql('services.MyService');

binding = givenBindingFromClass(MyService, ctx, {
namespace: 'my-services',
defaultNamespace: 'services',
});

expect(binding.key).to.eql('my-services.MyService');

binding = givenBindingFromClass(MyServiceWithNS, ctx, {
defaultNamespace: 'services',
});

expect(binding.key).to.eql('my-services.MyServiceWithNS');
});

it('includes class name in error messages', () => {
expect(() => {
// Reproduce a problem that @bajtos encountered when the project
Expand Down
14 changes: 11 additions & 3 deletions packages/context/src/binding-inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,23 @@ export type BindingFromClassOptions = {
*/
type?: string;
/**
* Artifact name, such as `my-rest-server` and `my-controller`
* Artifact name, such as `my-rest-server` and `my-controller`. It
* overrides the name tag
*/
name?: string;
/**
* Namespace for the binding key, such as `servers` and `controllers`
* Namespace for the binding key, such as `servers` and `controllers`. It
* overrides the default namespace or namespace tag
*/
namespace?: string;
/**
* Mapping artifact type to binding key namespaces
*/
typeNamespaceMapping?: TypeNamespaceMapping;
/**
* Default namespace if the binding does not have an explicit namespace
*/
defaultNamespace?: string;
/**
* Default scope if the binding does not have an explicit scope
*/
Expand Down Expand Up @@ -313,7 +319,9 @@ function buildBindingKey<T>(
if (key) return key;

let namespace =
options.namespace ?? bindingTemplate.tagMap[ContextTags.NAMESPACE];
options.namespace ??
bindingTemplate.tagMap[ContextTags.NAMESPACE] ??
options.defaultNamespace;
if (!namespace) {
const namespaces = Object.assign(
{},
Expand Down

0 comments on commit ec2da01

Please sign in to comment.