UNPKG

ts-ioc-container

Version:

Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.

25 lines (24 loc) 934 B
import { isDependencyKey } from '../container/IContainer'; import { SingleToken } from './SingleToken'; import { ClassToken } from './ClassToken'; import { FunctionToken } from './FunctionToken'; import { UnsupportedTokenTypeError } from '../errors/UnsupportedTokenTypeError'; import { InjectionToken, isInjectionToken } from './InjectionToken'; import { Is } from '../utils/basic'; import { ConstantToken } from './ConstantToken'; export const toToken = (token) => { if (token instanceof InjectionToken) { return token; } if (isDependencyKey(token)) { return new SingleToken(token); } if (Is.constructor(token)) { return new ClassToken(token); } if (typeof token === 'function') { return new FunctionToken(token); } throw new UnsupportedTokenTypeError(`Unknown token ${token}`); }; export const argToToken = (v) => (isInjectionToken(v) ? v : new ConstantToken(v));