{"_id":"@apihub24/ts_dependency_injection","_rev":"5-9609a3d4563ef4bce29d325d370b7ebe","name":"@apihub24/ts_dependency_injection","dist-tags":{"latest":"0.4.0-alpha.0"},"versions":{"0.2.0":{"name":"@apihub24/ts_dependency_injection","version":"0.2.0","keywords":[],"author":"","license":"Apache-2.0","_id":"@apihub24/ts_dependency_injection@0.2.0","maintainers":[{"name":"apihub24","email":"markusmorgenstern87@outlook.de"}],"dist":{"shasum":"2a8dbf4807a3379b2b832d5ec52b28f2ae375b96","tarball":"https://registry.npmjs.org/@apihub24/ts_dependency_injection/-/ts_dependency_injection-0.2.0.tgz","fileCount":5,"integrity":"sha512-x2smQD3jZOVJkuMklGa5hAX8ov2Gq0Rd9X78BmG08A3u3MvS6U96M7xBk3ebBcCidlyOqMAkW5tdRSXdgrE6kw==","signatures":[{"sig":"MEQCIBcT+C4Sk+eT9o5VUpZ93CxXpM0bw45XevcbtBNuAardAiAZ9DbShVi0fl+/QjMwz/ntmH6M1dtPpp8wbISmVgRDag==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":18565},"jest":{"roots":["<rootDir>/src/"],"rootDir":".","testRegex":".*\\.spec\\.ts$","transform":{"^.+\\.(t|j)s$":"ts-jest"},"testEnvironment":"node","moduleNameMapper":{},"coverageDirectory":"./coverage","collectCoverageFrom":["**/*.(t|j)s"],"moduleFileExtensions":["js","json","ts"]},"main":"dist/index.js","types":"dist/index.d.ts","gitHead":"20b32af72baa64332aa8101bd127427851334224","scripts":{"test":"jest","build":"npm run clean && tsc","clean":"rimraf dist","test:cov":"jest --coverage","test:watch":"jest --watch"},"_npmUser":{"name":"apihub24","email":"markusmorgenstern87@outlook.de"},"_npmVersion":"10.9.3","description":"This project provides a lightweight and flexible dependency injection (DI) container for TypeScript applications. It simplifies the management of class dependencies, making your code more modular, testable, and maintainable. The container supports transie","directories":{},"_nodeVersion":"22.18.0","dependencies":{"reflect-metadata":"^0.2.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^30.0.0","rimraf":"^6.0.1","ts-jest":"^29.4.1","typescript":"^5.9.2","@types/jest":"^30.0.0","@types/node":"^24.5.2"},"_npmOperationalInternal":{"tmp":"tmp/ts_dependency_injection_0.2.0_1758221299231_0.11742332037085923","host":"s3://npm-registry-packages-npm-production"}},"0.2.1":{"name":"@apihub24/ts_dependency_injection","version":"0.2.1","keywords":[],"author":"","license":"Apache-2.0","_id":"@apihub24/ts_dependency_injection@0.2.1","maintainers":[{"name":"apihub24","email":"markusmorgenstern87@outlook.de"}],"dist":{"shasum":"f101454820205b31e7e22d31ecd518ff8d04a011","tarball":"https://registry.npmjs.org/@apihub24/ts_dependency_injection/-/ts_dependency_injection-0.2.1.tgz","fileCount":15,"integrity":"sha512-zPvyFZA2fjnF8Ww3vIyK/az1/cUPDa07y0QXRtH4hW/XEcXqyLh3+n3RVpDUqpOR9dMGtyQYflQFfB0wK0FenA==","signatures":[{"sig":"MEYCIQCOvnq2T3vdSpxVszQ9Im/SNJmIvvaaqgv1x8p2dPkuxAIhAIdYImaTBf5AcZaTRLz68nzKUgkpnIjyWsKfTWNlH7C0","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":103023},"jest":{"roots":["<rootDir>/src/"],"rootDir":".","testRegex":".*\\.spec\\.ts$","transform":{"^.+\\.(t|j)s$":"ts-jest"},"testEnvironment":"node","moduleNameMapper":{},"coverageDirectory":"./coverage","collectCoverageFrom":["**/*.(t|j)s"],"moduleFileExtensions":["js","json","ts"]},"main":"dist/index.js","types":"dist/index.d.ts","gitHead":"a1cacd502fd45653061c3e23337c40e92be38b2b","scripts":{"test":"jest","build":"npm run clean && tsc","clean":"rimraf dist","test:cov":"jest --coverage","test:watch":"jest --watch"},"_npmUser":{"name":"apihub24","email":"markusmorgenstern87@outlook.de"},"_npmVersion":"10.9.3","description":"This project provides a lightweight and flexible dependency injection (DI) container for TypeScript applications. It simplifies the management of class dependencies, making your code more modular, testable, and maintainable. The container supports transie","directories":{},"_nodeVersion":"22.18.0","dependencies":{"reflect-metadata":"^0.2.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^30.0.0","rimraf":"^6.0.1","ts-jest":"^29.4.1","typescript":"^5.9.2","@types/jest":"^30.0.0","@types/node":"^24.5.2"},"_npmOperationalInternal":{"tmp":"tmp/ts_dependency_injection_0.2.1_1758221444405_0.9040314184555858","host":"s3://npm-registry-packages-npm-production"}},"0.2.2":{"name":"@apihub24/ts_dependency_injection","version":"0.2.2","keywords":[],"author":"","license":"Apache-2.0","_id":"@apihub24/ts_dependency_injection@0.2.2","maintainers":[{"name":"apihub24","email":"markusmorgenstern87@outlook.de"}],"dist":{"shasum":"97a36a3da99fead0f32b1f58e8a3fdfd852514ff","tarball":"https://registry.npmjs.org/@apihub24/ts_dependency_injection/-/ts_dependency_injection-0.2.2.tgz","fileCount":15,"integrity":"sha512-x3yYAo7i1lK57J5dYxpSYIYuLd7EaIHYGAPGV7nZ/EZJ0DKbshiveK9nvr1HMDG0aHSvQYzvpY+S2s0O18gHJQ==","signatures":[{"sig":"MEYCIQDfkzh/eYmoA71xRrD7acwGzazEXpo8KBNny0i8YhHCrwIhAMgjlqj61Ndo0VVLmcSZYtLBZOMK21T7Er6Y36neQ30q","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":103907},"jest":{"roots":["<rootDir>/src/"],"rootDir":".","testRegex":".*\\.spec\\.ts$","transform":{"^.+\\.(t|j)s$":"ts-jest"},"testEnvironment":"node","moduleNameMapper":{},"coverageDirectory":"./coverage","collectCoverageFrom":["**/*.(t|j)s"],"moduleFileExtensions":["js","json","ts"]},"main":"dist/index.js","types":"dist/index.d.ts","gitHead":"345607d36eea3b1ffd52d772abe2c8540558547d","scripts":{"test":"jest","build":"npm run clean && tsc","clean":"rimraf dist","release":"npm run build && npm run test && npm publish","test:cov":"jest --coverage","test:watch":"jest --watch"},"_npmUser":{"name":"apihub24","email":"markusmorgenstern87@outlook.de"},"_npmVersion":"10.9.3","description":"[![npm version](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection.svg?icon=si%3Anpm)](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection)\r [![License: Apache2.0](https://img.shields.io/badge/License-Apache2.0-green.svg)](https://opens","directories":{},"_nodeVersion":"22.18.0","dependencies":{"reflect-metadata":"^0.2.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^30.0.0","rimraf":"^6.0.1","ts-jest":"^29.4.1","typescript":"^5.9.2","@types/jest":"^30.0.0","@types/node":"^24.5.2"},"_npmOperationalInternal":{"tmp":"tmp/ts_dependency_injection_0.2.2_1758223789119_0.37539577893198994","host":"s3://npm-registry-packages-npm-production"}},"0.3.0":{"name":"@apihub24/ts_dependency_injection","version":"0.3.0","keywords":[],"author":{"url":"https://github.com/codeus-node/apihub24_ts_dependency_injection","name":"Markus Morgenstern","email":"git@apihub24.de"},"license":"Apache-2.0","_id":"@apihub24/ts_dependency_injection@0.3.0","maintainers":[{"name":"apihub24","email":"markusmorgenstern87@outlook.de"}],"dist":{"shasum":"3293642fc1bad18cb38ce71e516b312c8f7adf2e","tarball":"https://registry.npmjs.org/@apihub24/ts_dependency_injection/-/ts_dependency_injection-0.3.0.tgz","fileCount":15,"integrity":"sha512-UYhXKGp6JTxJxdYHbIBcD6jkdC5y5krwbgEV4S5KKMDr0F+pYxC/FZMaS12O/HAMo9BjuBJKuxFCZFVEKgKlPw==","signatures":[{"sig":"MEUCIHd77I/43q8HR9IpGQIJuXsQINS2aEvO5unL3Gtdc5+jAiEAqkvV2VurMg++q82pkGmzlSwwykPmRgUUUvg2fMrSOP0=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":106374},"jest":{"roots":["<rootDir>/src/"],"rootDir":".","testRegex":".*\\.spec\\.ts$","transform":{"^.+\\.(t|j)s$":"ts-jest"},"testEnvironment":"node","moduleNameMapper":{},"coverageDirectory":"./coverage","collectCoverageFrom":["**/*.(t|j)s"],"moduleFileExtensions":["js","json","ts"]},"main":"dist/index.js","types":"dist/index.d.ts","gitHead":"3c3b563bcdf503cbc8adbf75151de33393d913a2","scripts":{"test":"jest","build":"npm run clean && tsc","clean":"rimraf dist","release":"npm run build && npm run test && npm publish","test:cov":"jest --coverage","test:watch":"jest --watch"},"_npmUser":{"name":"apihub24","email":"markusmorgenstern87@outlook.de"},"_npmVersion":"10.9.3","description":"[![npm version](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection.svg?icon=si%3Anpm)](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection)\r [![License: Apache2.0](https://img.shields.io/badge/License-Apache2.0-green.svg)](https://opens","directories":{},"_nodeVersion":"22.18.0","dependencies":{"reflect-metadata":"^0.2.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^30.0.0","rimraf":"^6.0.1","ts-jest":"^29.4.1","typescript":"^5.9.2","@types/jest":"^30.0.0","@types/node":"^24.5.2"},"_npmOperationalInternal":{"tmp":"tmp/ts_dependency_injection_0.3.0_1758288152791_0.9904159580553338","host":"s3://npm-registry-packages-npm-production"}},"0.4.0-alpha.0":{"name":"@apihub24/ts_dependency_injection","version":"0.4.0-alpha.0","description":"[![npm version](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection.svg?icon=si%3Anpm)](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection)\r [![License: Apache2.0](https://img.shields.io/badge/License-Apache2.0-green.svg)](https://opens","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"npm run clean && tsc","clean":"rimraf dist","test":"jest","test:watch":"jest --watch","test:cov":"jest --coverage","release":"npm run build && npm run test && npm publish"},"dependencies":{"reflect-metadata":"^0.2.2"},"devDependencies":{"typescript":"^5.9.2","rimraf":"^6.0.1","@types/node":"^24.5.2","@types/jest":"^30.0.0","jest":"^30.0.0","ts-jest":"^29.4.1"},"keywords":[],"author":{"name":"Markus Morgenstern","email":"git@apihub24.de","url":"https://github.com/codeus-node/apihub24_ts_dependency_injection"},"license":"Apache-2.0","publishConfig":{"access":"public"},"jest":{"moduleFileExtensions":["js","json","ts"],"rootDir":".","testRegex":".*\\.spec\\.ts$","transform":{"^.+\\.(t|j)s$":"ts-jest"},"collectCoverageFrom":["**/*.(t|j)s"],"coverageDirectory":"./coverage","testEnvironment":"node","roots":["<rootDir>/src/"],"moduleNameMapper":{}},"_id":"@apihub24/ts_dependency_injection@0.4.0-alpha.0","gitHead":"8fb2879f5324c7ceeb67fe5fc4c84d53216f34a2","_nodeVersion":"22.18.0","_npmVersion":"10.9.3","dist":{"integrity":"sha512-I4mkvRpBICtYHMbUMNEod4fDjcf/oC7ydXYVel9k7KPKMF+sKJfv6OThl8D0tK6ZDpmztqV3kmpinnI0EuY0QQ==","shasum":"a313fb95364107d60fce419a53c1d33e09377f94","tarball":"https://registry.npmjs.org/@apihub24/ts_dependency_injection/-/ts_dependency_injection-0.4.0-alpha.0.tgz","fileCount":15,"unpackedSize":107143,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCpefY98bvoVwQtBvBiB3HXxCeu0GE4XSaf9GQ7GvobOQIhANj33E+jnVKggD1U6joCirwzU6D1cTGlfQF8VK8+36t5"}]},"_npmUser":{"name":"apihub24","email":"markusmorgenstern87@outlook.de"},"directories":{},"maintainers":[{"name":"apihub24","email":"markusmorgenstern87@outlook.de"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/ts_dependency_injection_0.4.0-alpha.0_1758310691761_0.033821809933439306"},"_hasShrinkwrap":false}},"time":{"created":"2025-09-18T18:48:19.127Z","modified":"2025-09-19T19:38:12.188Z","0.2.0":"2025-09-18T18:48:19.415Z","0.2.1":"2025-09-18T18:50:44.566Z","0.2.2":"2025-09-18T19:29:49.292Z","0.3.0":"2025-09-19T13:22:32.971Z","0.4.0-alpha.0":"2025-09-19T19:38:12.005Z"},"author":{"name":"Markus Morgenstern","email":"git@apihub24.de","url":"https://github.com/codeus-node/apihub24_ts_dependency_injection"},"license":"Apache-2.0","keywords":[],"description":"[![npm version](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection.svg?icon=si%3Anpm)](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection)\r [![License: Apache2.0](https://img.shields.io/badge/License-Apache2.0-green.svg)](https://opens","maintainers":[{"name":"apihub24","email":"markusmorgenstern87@outlook.de"}],"readme":"[![npm version](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection.svg?icon=si%3Anpm)](https://badge.fury.io/js/@apihub24%2Fts_dependency_injection)\r\n[![License: Apache2.0](https://img.shields.io/badge/License-Apache2.0-green.svg)](https://opensource.org/licenses/Apache2.0)\r\n[![CI](https://github.com/codeus-node/apihub24_ts_dependency_injection/actions/workflows/ci.yml/badge.svg)](https://github.com/codeus-node/apihub24_ts_dependency_injection/actions/workflows/ci.yml)\r\n[![Codecov](https://codecov.io/gh/codeus-node/apihub24_ts_dependency_injection/branch/main/graph/badge.svg)](https://codecov.io/gh/codeus-node/apihub24_ts_dependency_injection)\r\n\r\n# Typescript Dependency Injection\r\n\r\nThis project provides a lightweight and flexible dependency injection (DI) container for TypeScript applications. It simplifies the management of class dependencies, making your code more modular, testable, and maintainable. The container supports transient services, singletons, and constructor-based dependency resolution.\r\n\r\n## Features\r\n\r\n- Dependency Resolution: Automatically resolves and injects class dependencies through the constructor.\r\n- Scoped Services: Register services under different keys to create distinct application contexts (e.g., global, test).\r\n- Singletons: Easily manage services that should only have one instance throughout their lifecycle.\r\n- Testing Support: The replaceWith function allows you to quickly mock dependencies for unit testing.\r\n- Minimalistic: A small and easy-to-understand codebase.\r\n\r\n## Installation\r\n\r\nTo use this container in your project, you'll need to install the reflect-metadata package, which is required for dependency resolution via decorators.\r\n\r\n```bash\r\nnpm install --save @apihub24/ts_dependency_injection\r\n```\r\n\r\nMake sure to enable emitDecoratorMetadata and experimentalDecorators in your tsconfig.json file.\r\n\r\n```json\r\n{\r\n  \"compilerOptions\": {\r\n    \"target\": \"es2017\",\r\n    \"module\": \"commonjs\",\r\n    \"lib\": [\"es2017\"],\r\n    \"emitDecoratorMetadata\": true,\r\n    \"experimentalDecorators\": true\r\n  }\r\n}\r\n```\r\n\r\n## Usage\r\n\r\n1. Registering Services\r\n\r\n   Use the provided decorators to register your classes with the container.\r\n\r\nBasic Service (Singleton)\r\n\r\nA service creates a new instance every time it's injected when the instance not exists.\r\n\r\n```ts\r\nimport { Service, inject } from \"@apihub24/ts_dependency_injection\";\r\n\r\n// register a Singleton Logger\r\n@Service()\r\nclass Logger {\r\n  log(message: string) {\r\n    console.log(message);\r\n  }\r\n}\r\n\r\n// inject the Logger Instance\r\nconst logger = inject(Logger);\r\n```\r\n\r\nAbstraction to Implementation\r\n\r\nRegister a concrete implementation for an abstract class or interface. This is a common pattern for writing testable code.\r\n\r\n```ts\r\nimport { ServiceFor, inject } from \"@apihub24/ts_dependency_injection\";\r\n\r\nabstract class AuthService {\r\n  abstract login(): void;\r\n}\r\n\r\n@ServiceFor(AuthService)\r\nclass BasicAuthService extends AuthService {\r\n  login() {\r\n    console.log(\"Logging in with basic authentication...\");\r\n  }\r\n}\r\n\r\nconst authService = inject(AuthService);\r\nauthService.login();\r\n```\r\n\r\nUse Scoped Service\r\n\r\nPass some string to Scope the Service so you can have Multiple Instances of a Abstract Service. This can be used for Service Decorator also.\r\n\r\n```ts\r\nimport { ServiceFor, inject, destroy } from \"@apihub24/ts_dependency_injection\";\r\n\r\nabstract class AuthService {\r\n  abstract login(): void;\r\n}\r\n\r\n@ServiceFor(AuthService, \"1\")\r\nclass AuthService1 extends AuthService {\r\n  login() {\r\n    console.log(\"Logging in with basic authentication...\");\r\n  }\r\n}\r\n\r\n@ServiceFor(AuthService, \"2\")\r\nclass AuthService2 extends AuthService {\r\n  login() {\r\n    console.log(\"Logging in with basic authentication...\");\r\n  }\r\n}\r\n\r\nconst authService1 = inject(AuthService, \"1\");\r\nconst authService2 = inject(AuthService, \"2\");\r\n\r\n// Be sure to Destroy the Instances when not used anymore!!!!\r\ndestroy(AuthService, \"1\");\r\ndestroy(AuthService, \"2\");\r\n```\r\n\r\n2. Constructor Injection\r\n\r\n   The container automatically resolves and provides dependencies to a class's constructor.\r\n\r\n```ts\r\nimport { ServiceFor, inject } from \"@apihub24/ts_dependency_injection\";\r\n\r\n@Service()\r\nclass Engine {}\r\n\r\n@Service()\r\nclass Car {\r\n  constructor(private engine: Engine) {}\r\n}\r\n\r\nconst car = inject(Car);\r\nconsole.log(car instanceof Car); // true\r\nconsole.log(car.engine instanceof Engine); // true\r\n```\r\n\r\nIf you want Inject a scoped Service use the @Inject Decorator.\r\n\r\n```ts\r\nimport { ServiceFor, inject } from \"@apihub24/ts_dependency_injection\";\r\n\r\nabstract class Engine {}\r\n@ServiceFor(Engine, \"petrol\")\r\nclass PetrolEngine implements Engine {}\r\n@ServiceFor(Engine, \"electro\")\r\nclass ElectroEngine implements Engine {}\r\n\r\n@Service()\r\nclass ElectroCar {\r\n  constructor(@Inject(\"electro\") private engine: Engine) {}\r\n}\r\n\r\n@Service()\r\nclass PetrolCar {\r\n  constructor(@Inject(\"petrol\") private engine: Engine) {}\r\n}\r\n\r\n@Service()\r\nclass HybridCar {\r\n  constructor(\r\n    private electroEngine: ElectroEngine,\r\n    private petrolEngine: PetrolEngine\r\n  ) {}\r\n}\r\n\r\nconst car1 = inject(ElectroCar);\r\nconst car2 = inject(PetrolCar);\r\nconst car3 = inject(HybridCar);\r\n\r\nconsole.log(car1 instanceof ElectroCar); // true\r\nconsole.log(car1.engine instanceof ElectroEngine); // true\r\n\r\nconsole.log(car2 instanceof PetrolCar); // true\r\nconsole.log(car2.engine instanceof PetrolEngine); // true\r\n\r\nconsole.log(car3 instanceof ElectroCar); // true\r\nconsole.log(car3.electroEngine instanceof ElectroEngine); // true\r\nconsole.log(car3.petrolEngine instanceof PetrolEngine); // true\r\n```\r\n\r\n3. Testing and Mocking\r\n\r\n   Use replaceWith to substitute a dependency's implementation for testing purposes.\r\n\r\n```ts\r\nimport { ServiceFor, inject } from \"@apihub24/ts_dependency_injection\";\r\n\r\n// Production Service\r\nabstract class DatabaseService {\r\n  abstract query(): string;\r\n}\r\n\r\n@ServiceFor(DatabaseService)\r\nclass MySQLService extends DatabaseService {\r\n  query() {\r\n    return \"Querying MySQL...\";\r\n  }\r\n}\r\n\r\n// A mock service for testing\r\nclass MockDatabaseService extends DatabaseService {\r\n  query() {\r\n    return \"Mocking the database...\";\r\n  }\r\n}\r\n\r\n// In your test:\r\nreplaceWith(DatabaseService, MockDatabaseService);\r\n\r\nconst dbService = inject(DatabaseService);\r\nconsole.log(dbService.query()); // \"Mocking the database...\"\r\n```\r\n","readmeFilename":"README.md"}