UNPKG

typedi

Version:

Dependency injection for TypeScript.

3 lines (2 loc) 9.79 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ClassTransformer={})}(this,(function(e){"use strict";var t,n=function(e){this.name=e},r=(t=function(e,n){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,n)},function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}),i=function(e){function t(t){var r,i,o=e.call(this)||this;return o.name="ServiceNotFoundError",o.normalizedIdentifier="<UNKNOWN_IDENTIFIER>","string"==typeof t?o.normalizedIdentifier=t:t instanceof n?o.normalizedIdentifier="Token<"+(t.name||"UNSET_NAME")+">":t&&(t.name||(null===(r=t.prototype)||void 0===r?void 0:r.name))&&(o.normalizedIdentifier="MaybeConstructable<"+t.name+">"||"MaybeConstructable<"+(null===(i=t.prototype)||void 0===i?void 0:i.name)+">"),o}return r(t,e),Object.defineProperty(t.prototype,"message",{get:function(){return'Service with "'+this.normalizedIdentifier+'" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set" function or using the "@Service()" decorator.'},enumerable:!1,configurable:!0}),t}(Error),o=function(){var e=function(t,n){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(t,n)};return function(t,n){function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),a=function(e){function t(t){var r,i,o=e.call(this)||this;return o.name="CannotInstantiateValueError",o.normalizedIdentifier="<UNKNOWN_IDENTIFIER>","string"==typeof t?o.normalizedIdentifier=t:t instanceof n?o.normalizedIdentifier="Token<"+(t.name||"UNSET_NAME")+">":t&&(t.name||(null===(r=t.prototype)||void 0===r?void 0:r.name))&&(o.normalizedIdentifier="MaybeConstructable<"+t.name+">"||"MaybeConstructable<"+(null===(i=t.prototype)||void 0===i?void 0:i.name)+">"),o}return o(t,e),Object.defineProperty(t.prototype,"message",{get:function(){return'Cannot instantiate the requested value for the "'+this.normalizedIdentifier+"\" identifier. The related metadata doesn't contain a factory or a type to instantiate."},enumerable:!1,configurable:!0}),t}(Error),s=Symbol("EMPTY_VALUE"),c=function(){return(c=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)},u=function(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var r=Array(e),i=0;for(t=0;t<n;t++)for(var o=arguments[t],a=0,s=o.length;a<s;a++,i++)r[i]=o[a];return r},f=function(){function e(e){this.services=[],this.id=e}return e.prototype.has=function(e){return!!this.findService(e)},e.prototype.get=function(e){var t=l.of(void 0),n=t.findService(e),r=this.findService(e);if(n&&!0===n.global)return this.getServiceValue(n);if(r)return this.getServiceValue(r);if(n&&this!==t){var o=c({},n);o.value=s,this.set(o);var a=this.getServiceValue(o);return this.set(c(c({},o),{value:a})),a}if(n)return this.getServiceValue(n);throw new i(e)},e.prototype.getMany=function(e){var t=this;return this.findAllServices(e).map((function(e){return t.getServiceValue(e)}))},e.prototype.set=function(e,t){var r=this;if(e instanceof Array)return e.forEach((function(e){return r.set(e)})),this;if("string"==typeof e||e instanceof n)return this.set({id:e,type:null,value:t,factory:void 0,global:!1,multiple:!1,eager:!1,transient:!1});if("function"==typeof e)return this.set({id:e,type:e,value:t,factory:void 0,global:!1,multiple:!1,eager:!1,transient:!1});var i=c({id:new n("UNREACHABLE"),type:null,factory:void 0,value:s,global:!1,multiple:!1,eager:!1,transient:!1},e),o=this.findService(i.id);return o&&!0!==o.multiple?Object.assign(o,i):this.services.push(i),i.eager&&this.get(i.id),this},e.prototype.remove=function(e){var t=this;return Array.isArray(e)?e.forEach((function(e){return t.remove(e)})):this.services=this.services.filter((function(n){return n.id!==e||(t.destroyServiceInstance(n),!1)})),this},e.prototype.reset=function(e){var t=this;switch(void 0===e&&(e={strategy:"resetValue"}),e.strategy){case"resetValue":this.services.forEach((function(e){return t.destroyServiceInstance(e)}));break;case"resetServices":this.services.forEach((function(e){return t.destroyServiceInstance(e)})),this.services=[];break;default:throw new Error("Received invalid reset strategy.")}return this},e.prototype.findAllServices=function(e){return this.services.filter((function(t){return t.id===e}))},e.prototype.findService=function(e){return this.services.find((function(t){return t.id===e}))},e.prototype.getServiceValue=function(e){var t,n=s;if(e.value!==s)return e.value;if(!e.factory&&!e.type)throw new a(e.id);if(e.factory)if(e.factory instanceof Array){var r=void 0;try{r=this.get(e.factory[0])}catch(t){if(!(t instanceof i))throw t;r=new e.factory[0]}n=r[e.factory[1]](this,e.id)}else n=e.factory(this,e.id);if(!e.factory&&e.type){var o=e.type,c=(null===(t=Reflect)||void 0===t?void 0:t.getMetadata("design:paramtypes",o))||[],f=this.initializeParams(o,c);f.push(this),n=new(o.bind.apply(o,u([void 0],f)))}if(e.transient||n===s||(e.value=n),n===s)throw new a(e.id);return e.type&&this.applyPropertyHandlers(e.type,n),n},e.prototype.initializeParams=function(e,t){var n=this;return t.map((function(t,r){var i=l.handlers.find((function(t){return(t.object===e||t.object===Object.getPrototypeOf(e))&&t.index===r}));return i?i.value(n):t&&t.name&&!n.isPrimitiveParamType(t.name)?n.get(t):void 0}))},e.prototype.isPrimitiveParamType=function(e){return["string","boolean","number","object"].includes(e.toLowerCase())},e.prototype.applyPropertyHandlers=function(e,t){var n=this;l.handlers.forEach((function(r){"number"!=typeof r.index&&(r.object.constructor===e||e.prototype instanceof r.object.constructor)&&r.propertyName&&(t[r.propertyName]=r.value(n))}))},e.prototype.destroyServiceInstance=function(e,t){if(void 0===t&&(t=!1),t||!!e.type||!!e.factory){if("function"==typeof(null==e?void 0:e.value).destroy)try{e.value.destroy()}catch(e){}e.value=s}},e}(),l=function(){function e(){}return e.of=function(e){if(void 0===e&&(e="default"),"default"===e)return this.globalInstance;var t=this.instances.find((function(t){return t.id===e}));return t||(t=new f(e),this.instances.push(t)),t},e.has=function(e){return this.globalInstance.has(e)},e.get=function(e){return this.globalInstance.get(e)},e.getMany=function(e){return this.globalInstance.getMany(e)},e.set=function(e,t){return this.globalInstance.set(e,t),this},e.remove=function(e){return this.globalInstance.remove(e),this},e.reset=function(e){if(void 0===e&&(e="default"),"default"==e)this.globalInstance.reset(),this.instances.forEach((function(e){return e.reset()}));else{var t=this.instances.find((function(t){return t.id===e}));t&&(t.reset(),this.instances.splice(this.instances.indexOf(t),1))}return this},e.registerHandler=function(e){return this.handlers.push(e),this},e.import=function(e){return this},e.handlers=[],e.globalInstance=new f("default"),e.instances=[],e}(),p=function(){var e=function(t,n){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(t,n)};return function(t,n){function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),y=function(e){function t(t,n){var r=e.call(this)||this;return r.target=t,r.propertyName=n,r.name="CannotInjectValueError",r}return p(t,e),Object.defineProperty(t.prototype,"message",{get:function(){return'Cannot inject value into "'+this.target.constructor.name+"."+this.propertyName+"\". Please make sure you setup reflect-metadata properly and you don't use interfaces without service tokens as injection value."},enumerable:!1,configurable:!0}),t}(Error);function d(e,t,r,i){var o;if((e&&"string"==typeof e||e instanceof n)&&(o={eagerType:e,lazyType:function(){return e}}),e&&"function"==typeof e&&(o={eagerType:null,lazyType:function(){return e()}}),!e&&r){var a=Reflect.getMetadata("design:type",t,r);o={eagerType:a,lazyType:function(){return a}}}if(!e&&"number"==typeof i&&Number.isInteger(i)){var s=Reflect.getMetadata("design:paramtypes",t,r),c=null==s?void 0:s[i];o={eagerType:c,lazyType:function(){return c}}}return o}e.CannotInjectValueError=y,e.CannotInstantiateValueError=a,e.Container=l,e.ContainerInstance=f,e.Inject=function(e){return function(t,n,r){var i=d(e,t,n,r);if(void 0===i||void 0===i.eagerType||i.eagerType===Object)throw new y(t,n);l.registerHandler({object:t,propertyName:n,index:r,value:function(e){var r=i.lazyType();if(void 0===r||r===Object)throw new y(t,n);return e.get(r)}})}},e.InjectMany=function(e){return function(t,n,r){var i=d(e,t,n,r);if(void 0===i||void 0===i.eagerType||i.eagerType===Object)throw new y(t,n);l.registerHandler({object:t,propertyName:n,index:r,value:function(e){var r=i.lazyType();if(void 0===r||r===Object)throw new y(t,n);return e.getMany(r)}})}},e.Service=function(e){return function(t){var r={id:t,type:t,factory:void 0,multiple:!1,global:!1,eager:!1,transient:!1,value:s};e instanceof n||"string"==typeof e?r.id=e:e&&(r.id=e.id||t,r.factory=e.factory||void 0,r.multiple=e.multiple||!1,r.global=e.global||!1,r.eager=e.eager||!1,r.transient=e.transient||!1),l.set(r)}},e.ServiceNotFoundError=i,e.Token=n,e.default=l,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=typedi.umd.min.js.map