UNPKG

contexify

Version:

A TypeScript library providing a powerful dependency injection container with context-based IoC capabilities, inspired by LoopBack's Context system.

88 lines 2.32 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { ClassDecoratorFactory } from "metarize"; import { asBindingTemplate, asClassOrProvider, asProvider, BINDING_METADATA_KEY, isProviderClass, removeNameAndKeyTags } from "./binding-inspector.js"; let InjectableDecoratorFactory = class InjectableDecoratorFactory2 extends ClassDecoratorFactory { static { __name(this, "InjectableDecoratorFactory"); } mergeWithInherited(inherited, target) { if (inherited) { return { templates: [ ...inherited.templates, removeNameAndKeyTags, ...this.spec.templates ], target: this.spec.target }; } this.withTarget(this.spec, target); return this.spec; } mergeWithOwn(ownMetadata) { return { templates: [ ...ownMetadata.templates, ...this.spec.templates ], target: this.spec.target }; } withTarget(spec, target) { spec.target = target; return spec; } }; function injectable(...specs) { const templateFunctions = specs.map((t) => { if (typeof t === "function") { return t; } return asBindingTemplate(t); }); return (target) => { const cls = target; const spec = { templates: [ asClassOrProvider(cls), ...templateFunctions ], target: cls }; const decorator = InjectableDecoratorFactory.createDecorator(BINDING_METADATA_KEY, spec, { decoratorName: "@injectable" }); decorator(target); }; } __name(injectable, "injectable"); (function(injectable2) { function provider(...specs) { return (target) => { const cls = target; if (!isProviderClass(cls)) { throw new Error(`Target ${cls} is not a Provider`); } injectable2( // Set up the default for providers asProvider(cls), ...specs )(target); }; } __name(provider, "provider"); injectable2.provider = provider; })(injectable || (injectable = {})); function bind(...specs) { return injectable(...specs); } __name(bind, "bind"); (function(bind2) { bind2.provider = injectable.provider; })(bind || (bind = {})); export { bind, injectable }; //# sourceMappingURL=binding-decorator.js.map