Skip to content

Commit f6fe55e

Browse files
committed
feat(context): allow namespace tag for createBindingFromClass
1 parent f5bf43c commit f6fe55e

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

packages/context/src/__tests__/unit/binding-inspector.unit.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,27 @@ describe('createBindingFromClass()', () => {
185185
expect(binding.key).to.eql('classes.MyClass');
186186
});
187187

188+
it('honors namespace with @bind', () => {
189+
@bind({tags: {namespace: 'services'}})
190+
class MyService {}
191+
192+
const ctx = new Context();
193+
const binding = givenBindingFromClass(MyService, ctx);
194+
195+
expect(binding.key).to.eql('services.MyService');
196+
});
197+
198+
it('honors namespace with options', () => {
199+
class MyService {}
200+
201+
const ctx = new Context();
202+
const binding = givenBindingFromClass(MyService, ctx, {
203+
namespace: 'services',
204+
});
205+
206+
expect(binding.key).to.eql('services.MyService');
207+
});
208+
188209
function givenBindingFromClass(
189210
cls: Constructor<unknown>,
190211
ctx: Context = new Context(),

packages/context/src/binding-inspector.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ function getNamespace(type: string, typeNamespaces = DEFAULT_TYPE_NAMESPACES) {
255255
* `<namespace>.<name>`.
256256
* - namespace
257257
* - `options.namespace`
258+
* - `namespace` tag value
258259
* - Map `options.type` or `type` tag value to a namespace, for example,
259260
* 'controller` to 'controller'.
260261
* - name
@@ -276,7 +277,8 @@ function buildBindingKey(
276277
let key: string = options.key || bindingTemplate.tagMap[ContextTags.KEY];
277278
if (key) return key;
278279

279-
let namespace = options.namespace;
280+
let namespace =
281+
options.namespace || bindingTemplate.tagMap[ContextTags.NAMESPACE];
280282
if (!namespace) {
281283
const namespaces = Object.assign(
282284
{},

packages/context/src/keys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export namespace ContextTags {
1111
* Type of the artifact
1212
*/
1313
export const TYPE = 'type';
14+
/**
15+
* Namespace of the artifact
16+
*/
17+
export const NAMESPACE = 'namespace';
1418
/**
1519
* Name of the artifact
1620
*/

0 commit comments

Comments
 (0)