{"id":19032502,"url":"https://github.com/rokkit-ts/rokkit.ts-dependency-injection","last_synced_at":"2026-05-15T18:09:05.314Z","repository":{"id":35094139,"uuid":"205663438","full_name":"rokkit-ts/rokkit.ts-dependency-injection","owner":"rokkit-ts","description":"A Dependency Injection Library for the Rokkit.ts Framework based on TypeScript and Node.js","archived":false,"fork":false,"pushed_at":"2023-01-05T18:18:31.000Z","size":556,"stargazers_count":1,"open_issues_count":11,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-03T04:04:14.198Z","etag":null,"topics":["backend","dependency-injection","framework","microservice","microservices","nodejs","rokkit","typescript"],"latest_commit_sha":null,"homepage":"https://rokkit.dev","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rokkit-ts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-01T10:52:36.000Z","updated_at":"2024-10-11T13:32:16.000Z","dependencies_parsed_at":"2023-01-15T13:42:16.784Z","dependency_job_id":null,"html_url":"https://github.com/rokkit-ts/rokkit.ts-dependency-injection","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rokkit-ts%2Frokkit.ts-dependency-injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rokkit-ts%2Frokkit.ts-dependency-injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rokkit-ts%2Frokkit.ts-dependency-injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rokkit-ts%2Frokkit.ts-dependency-injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rokkit-ts","download_url":"https://codeload.github.com/rokkit-ts/rokkit.ts-dependency-injection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240082658,"owners_count":19745260,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backend","dependency-injection","framework","microservice","microservices","nodejs","rokkit","typescript"],"created_at":"2024-11-08T21:29:03.386Z","updated_at":"2026-05-03T16:30:18.395Z","avatar_url":"https://github.com/rokkit-ts.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rokkit.ts-dependency-injection\n\n[![Build Status](https://travis-ci.com/rokkit-ts/rokkit.ts-dependency-injection.svg?branch=master)](https://travis-ci.com/rokkit-ts/rokkit.ts-dependency-injection)\n\nTypeScript dependency injection library using decorators for the rokkit.ts framework and other projects.\nIt provides a simple API to register classes and create instances of them.\nThe instances are handled in one container called `RokkitDI`.\n\n## Install and Build\n\nTo install the package:\n\n```bash\nnpm install @rokkit.ts/dependency-injection\n```\n\n## Usage\n\nYou can use two different ways of using the whole package.\n\nThe first way is to register your classes by decorators.\nThe second way is to use the native API of the RokkitDI container.\n\n### Using Decorators\n\nThe following example show a simple usage of the package.\nThe first listing shows the class that you want to be injected.\n\n```typescript\nimport { Injectable, Inject } from '@rokkit.ts/dependency-injection'\n\n@Injectable()\nclass DecoratedClass {\n  public foo: string\n  public bar: number\n\n  constructor(@Inject('test') foo: string, @Inject(0.11) public bar: number) {\n    this.foo = foo\n    this.bar = bar\n  }\n}\n```\n\nAfter annotating the class, we can retrieve an singleton or an instance by the `RokkitDI`.\nWith this injector we could now create an instance of the corresponding class.\n\n```typescript\nimport { RokkitDI } from '@rokkit.ts/dependency-injection'\nconst instance: DecoratedClass = RokkitDI.singletonOf('DecoratedClass')\n\nconsole.log(`Foo: ${instance.foo}, Bar: ${instance.bar}`)\n// Output: Foo: test, Bar: 0.11\n```\n\n#### Automatic Constructor Argument scanning\n\nThe package provides the ability to scan classes arguments automatically. If any constructor argument is not decorated with the \u003ccode\u003e@Inject\n(value:\nany)\u003c/code\u003e annotation, the base container tries to create automatically an instance for it.\n\n#### Code Example\n\nThe first listing shows two classes the \"FirstDecoratedClass\" class has a dependency for the second class\n\"SecondDecoratedClass\". Note that both class are annotated as \u003ccode\u003e@Injectable\u003c/code\u003e, but only the second class\nhas \u003ccode\u003e@Inject\u003c/code\u003e\nannotation present on the constructor. The package will now scan all user source code files for the constructor\nargument and automatically adds the found arguments. This allows you to create an instance of \"FirstDecoratedClass\"\nwithout annotating the constructor with a specific value for user object.\n\nBy default the source code scan will look for the source code directory \"./src\". If you want to change this behavior\nset the environment variable \u003ccode\u003eSRC_SCAN_DIR\u003c/code\u003e to you preferable directory before starting your application.\n\n```typescript\nimport { Injectable, Inject } from '@rokkit.ts/dependency-injection'\n\n@Injectable()\nclass FirstDecoratedClass {\n  public classDependency: SecondDecoratedClass\n\n  constructor(classDependency: SecondDecoratedClass) {\n    this.classDependency = classDependency\n  }\n}\n\n@Injectable()\nclass SecondDecoratedClass {\n  public foo: string\n  public bar: number\n\n  constructor(@Inject('test') foo: string, @Inject(0.11) public bar: number) {\n    this.foo = foo\n    this.bar = bar\n  }\n}\n```\n\n### Use the native API\n\nThe native API let's you easy register an injectable class on the `RokkitDi` container.\nIn order to register a class you need to provide its arguments.\n\n```typescript\nimport { RokkitDI } from '@rokkit.ts/dependency-injection'\n\nclass AClass {\n  constructor(public foo: string, bar: number)\n}\n\nRokkitDI.registerInjectable(AClass, [\n  { index: 0, type: 'string', value: 'test' },\n  { index: 1, type: 'number', value: 0.11 }\n])\n\nconst instance: AClass = RokkitDI.singletonOf('AClass')\n\nconsole.log(`Foo: ${instance.foo}, Bar: ${instance.bar}`)\n// Output: Foo: test, Bar: 0.11\n```\n\n## API Description\n\n| Decorators |                                                |\n| :--------: | :--------------------------------------------- |\n|  Methods:  | \u003ccode\u003e@Injectable(contextName?: string)\u003c/code\u003e |\n|            | \u003ccode\u003e@Inject(value: any)\u003c/code\u003e               |\n\n|  Class:  | RokkitDI                                    |\n| :------: | :------------------------------------------ |\n| Methods: | \u003ccode\u003eregisterInjectable(Class)\u003c/code\u003e      |\n|          | \u003ccode\u003esingletonOf(className: string)\u003c/code\u003e |\n|          | \u003ccode\u003einstanceOf(className: string)\u003c/code\u003e  |\n\n## Contribution\n\nAll kinds of contributions are welcome, no matter how big or small.\nBefore you start to contribute please read our [Code of Conduct](./CODE_OF_CONDUCT.md).\n\nIn order to submit any contribution check out our [contribution guidelines](./CONTRIBUTION.md).\n\n## License\n\nRokkit.ts-dependency-injection is Open Source software released under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frokkit-ts%2Frokkit.ts-dependency-injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frokkit-ts%2Frokkit.ts-dependency-injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frokkit-ts%2Frokkit.ts-dependency-injection/lists"}