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) 958 B
import { isDependencyKey } from '../container/IContainer.js'; import { SingleToken } from './SingleToken.js'; import { ClassToken } from './ClassToken.js'; import { FunctionToken } from './FunctionToken.js'; import { UnsupportedTokenTypeError } from '../errors/UnsupportedTokenTypeError.js'; import { InjectionToken, isInjectionToken } from './InjectionToken.js'; import { Is } from '../utils/basic.js'; import { ConstantToken } from './ConstantToken.js'; 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));