Service Tokens
import 'reflect-metadata';
import { Container, Token } from 'typedi';
export const JWT_SECRET_TOKEN = new Token<string>('MY_SECRET');
Container.set(JWT_SECRET_TOKEN, 'wow-such-secure-much-encryption');
/**
* Somewhere else in the application after the JWT_SECRET_TOKEN is
* imported in can be used to request the secret from the Container.
*
* This value is type-safe also because the Token is typed.
*/
const JWT_SECRET = Container.get(JWT_SECRET_TOKEN);Injecting service tokens
import 'reflect-metadata';
import { Container, Token, Inject, Service } from 'typedi';
export const JWT_SECRET_TOKEN = new Token<string>('MY_SECRET');
Container.set(JWT_SECRET_TOKEN, 'wow-such-secure-much-encryption');
@Service()
class Example {
@Inject(JWT_SECRET_TOKEN)
myProp: string;
}
const instance = Container.get(Example);
// The instance.myProp property has the value assigned for the TokenTokens with same name
Difference between Token and string identifier
Last updated
Was this helpful?