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

Get the token of the target #33

Open
mahulst opened this issue Dec 29, 2020 · 1 comment
Open

Get the token of the target #33

mahulst opened this issue Dec 29, 2020 · 1 comment

Comments

@mahulst
Copy link

mahulst commented Dec 29, 2020

Very cool library, type safety is especially nice!

We currently use inversify, and in toDynamicValue you get Context as an argument. In the context you can look up the token of the target where the current value is injected into. We use that for logging purposes.

With the magic token TARGET_TOKEN I am able to get the name of the constructor that requested this value. But I also would like the token, as we can give some more context in the token than the class name.

Is there way this is currently possible?

Minimal example:

function TestFactory (target: Function | undefined) {  
    // Here I would like to get access to the token of the target.
    console.log(target) // [Function: MagicTokenTest] { inject: [ 'TestFactory' ] }
    return 'hello, world!'
}
TestFactory.inject = [TARGET_TOKEN] as const

class MagicTokenTest {
    public static inject = ['TestFactory'] as const
    constructor(testFactory: string) {}
}

const appInjector = createInjector()
    .provideFactory('TestFactory', TestFactory, Scope.Transient)
    .provideClass('token.i.use.for.logging', MagicTokenTest);

appInjector.resolve('token.i.use.for.logging');
@nicojs
Copy link
Owner

nicojs commented Oct 11, 2021

Hi @mahulst thanks for the kind words and sorry for the late reply.

Sounds like a fair proposal. I'm open to PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants