undecorated-di
Version:
Type-safe, straightforward dependency injection for TypeScript without annotations or reflect-metadata.
2 lines • 5.95 kB
JavaScript
"use strict";var g=Object.defineProperty,U=Object.defineProperties,V=Object.getOwnPropertyDescriptor,I=Object.getOwnPropertyDescriptors,N=Object.getOwnPropertyNames,R=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;var k=(r,e,t)=>e in r?g(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,h=(r,e)=>{for(var t in e||(e={}))D.call(e,t)&&k(r,t,e[t]);if(R)for(var t of R(e))C.call(e,t)&&k(r,t,e[t]);return r},b=(r,e)=>U(r,I(e));var O=(r,e)=>{for(var t in e)g(r,t,{get:e[t],enumerable:!0})},E=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of N(e))!D.call(r,i)&&i!==t&&g(r,i,{get:()=>e[i],enumerable:!(n=V(e,i))||n.enumerable});return r};var _=r=>E(g({},"__esModule",{value:!0}),r);var B={};O(B,{ContainerBuilder:()=>m,Keys:()=>f,bind:()=>S,inject:()=>P});module.exports=_(B);var T=class{constructor(e,t){this.key=e,this.bindable=t,this.dependencies=t.__dependencies__}resolve(e){return this.key.asType(this.bindable.bind(this.bindable,...e))}};var l=class{constructor(e){this.value=e}resolve(){return this.value}};var p=class{constructor(e,t,n){this.keyName=e,this.isSingleton=t,this.parent=n}};var o=class{constructor(e,t,n){this.key=e,this.injectable=t,this.dependencies=t.__dependencies__,this.isSingleton=n}resolve(e){return this.key.asType(new this.injectable(...e))}};var y=class extends Error{constructor(t){super(`A circular dependency was found when resolving "${t}." All members of a dependency cycle must be singletons.`);this.name="CircularDependencyError"}};var a=class extends Error{constructor(t){super(`"${t}" is already assigned.`);this.name="DuplicateKeyError"}};var d=class r extends Error{constructor(t){super(r.getMessage(t));this.name="MissingDependencyError"}static getMessage(t){return`Service with key ${t.keyName} not found. Dependency graph is ${this.buildDependencyGraph(t)}.`}static buildDependencyGraph(t){let n=[];for(n.push(t.keyName);t.parent;)t=t.parent,n.push(t.keyName);return n.reverse(),n.join("-->")}};var u=class extends Error{constructor(t){super(`Injectable with key name "${t}" not found. Was it registered?`);this.name="MissingInjectableError"}};var s=class extends Error{constructor(){super("A singleton that is part of a dependency cycle accessed a property of another member of that cycle in its constructor before the cycle could be resolved.");this.name="UninitializedPropertyAccessed"}};var x=class{constructor(){let e=this;this.proxy=new Proxy({},{get(t,n){if(!e.instance)throw new s;return Reflect.get(e.instance,n)},set(t,n,i){if(!e.instance)throw new s;return Reflect.set(e.instance,n,i)},getPrototypeOf(t){if(!e.instance)throw new s;return Reflect.getPrototypeOf(e.instance)},setPrototypeOf(t,n){if(!e.instance)throw new s;return Reflect.setPrototypeOf(e.instance,n)},defineProperty(t,n,i){if(!e.instance)throw new s;return Reflect.defineProperty(e.instance,n,i)},deleteProperty(t,n){if(!e.instance)throw new s;return Reflect.deleteProperty(e.instance,n)},getOwnPropertyDescriptor(t,n){if(!e.instance)throw new s;return Reflect.getOwnPropertyDescriptor(e.instance,n)},has(t,n){if(!e.instance)throw new s;return Reflect.has(e.instance,n)},ownKeys(t){if(!e.instance)throw new s;return Reflect.ownKeys(e.instance)},isExtensible(t){if(!e.instance)throw new s;return Reflect.isExtensible(e.instance)||Reflect.preventExtensions(t),Reflect.isExtensible(t)},preventExtensions(t){if(!e.instance)throw new s;return Reflect.preventExtensions(e.instance),Reflect.preventExtensions(t)}})}};var K=class{constructor(e){this.injectables=e;this.resolvedInjectables=new Set;this.singletons={};this.singletonPlaceholders={}}get(e){let t=new p(e.name,this.isSingleton(e.name));return this.resolveWithDependencyGraph(t)}resolveWithDependencyGraph(e){let t=this.singletons[e.keyName];if(t)return t;let n=this.injectables[e.keyName];if(!n)throw e.parent?new d(e):new u(e.keyName);if(n instanceof l)return n.resolve();if(!this.wasPreviouslyResolved(e.keyName)&&this.completesSingletonOnlyDependencyCycle(e)){e.keyName in this.singletonPlaceholders||(this.singletonPlaceholders[e.keyName]=[]);let c=new x;return this.singletonPlaceholders[e.keyName].push(c),c.proxy}let i=n.dependencies.map(c=>{let j=new p(c.name,this.isSingleton(c.name),e);return this.resolveWithDependencyGraph(j)}),w=this.injectables[e.keyName].resolve(i);return e.isSingleton&&(this.singletons[e.keyName]=w,e.keyName in this.singletonPlaceholders&&(this.singletonPlaceholders[e.keyName].forEach(j=>{j.instance=w}),delete this.singletonPlaceholders[e.keyName])),this.flagResolved(e.keyName),w}isSingleton(e){let t=this.injectables[e];return t instanceof o&&t.isSingleton}wasPreviouslyResolved(e){return this.resolvedInjectables.has(e)}completesSingletonOnlyDependencyCycle(e){let{keyName:t}=e,n=e.isSingleton;for(;e.parent;)if(e=e.parent,e.isSingleton||(n=!1),e.keyName===t){if(!n)throw new y(t);return!0}return!1}flagResolved(e){this.resolvedInjectables.add(e)}};var m=class r{constructor(e){this.injectables=e}static createBuilder(){return new r({})}registerClass(e,t){let n=new o(e,t,!1);return this.register(e.name,n)}registerSingleton(e,t){let n=new o(e,t,!0);return this.register(e.name,n)}registerFunction(e,t){let n=new T(e,t);return this.register(e.name,n)}registerConstant(e,t){let n=new l(t);return this.register(e.name,n)}build(){return new K(this.injectables)}register(e,t){if(e in this.injectables)throw new a(e);let n=b(h({},this.injectables),{[e]:t});return new r(n)}};var v=class{constructor(e){this.name=e}asType(e){return e}};var f=class r{constructor(e){this.keys=e}static createKeys(){return new r({})}addKey(e){if(e in this.keys)throw new a(e);return{forType:()=>{let t=b(h({},this.keys),{[e]:new v(e)});return new r(t)}}}};function S(r,e){let t=(...n)=>r(...n);return t.__dependencies__=e,t}function P(r,e){let t=r;return t.__dependencies__=e,t}0&&(module.exports={ContainerBuilder,Keys,bind,inject});
//# sourceMappingURL=index.cjs.map