{"id":32120411,"url":"https://github.com/brmcerqueira/deninject","last_synced_at":"2026-02-19T02:02:03.905Z","repository":{"id":62421710,"uuid":"298818769","full_name":"brmcerqueira/deninject","owner":"brmcerqueira","description":"A dependency injection module for Deno.","archived":false,"fork":false,"pushed_at":"2021-09-02T20:33:22.000Z","size":92,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-13T08:47:44.757Z","etag":null,"topics":["decorators","deninject","deno","dependency-injection","injection","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brmcerqueira.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-26T13:20:47.000Z","updated_at":"2025-01-25T08:37:59.000Z","dependencies_parsed_at":"2022-11-01T17:32:35.142Z","dependency_job_id":null,"html_url":"https://github.com/brmcerqueira/deninject","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/brmcerqueira/deninject","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brmcerqueira%2Fdeninject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brmcerqueira%2Fdeninject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brmcerqueira%2Fdeninject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brmcerqueira%2Fdeninject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brmcerqueira","download_url":"https://codeload.github.com/brmcerqueira/deninject/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brmcerqueira%2Fdeninject/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29600844,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T00:59:38.239Z","status":"online","status_checked_at":"2026-02-19T02:00:07.702Z","response_time":117,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["decorators","deninject","deno","dependency-injection","injection","typescript"],"created_at":"2025-10-20T19:44:41.215Z","updated_at":"2026-02-19T02:02:03.899Z","avatar_url":"https://github.com/brmcerqueira.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deninject\n\nA dependency injection module for Deno.\n\nAvailable at Deno Land: [Deninject](https://deno.land/x/deninject) \n\n## Contents\n- [Injector](#injector)\n- [Decorators](#decorators)\n  - [Transient](#transient)\n  - [Singleton](#singleton)\n  - [Scope](#scope)\n  - [Token](#token)\n    - [Inject](#inject)\n    - [Dynamic Token](#dynamic-token)\n- [Symbols](#symbols)\n  - [Scope Symbol](#scope-symbol)\n  - [Token Symbol](#token-symbol)  \n- [Setup](#setup)\n\n## Injector\n\nThe main feature of this module is the `Injector`. This is a class that contains all the dependencies and will be resolvable based on the settings of the injection modules using some specific decorators. Let's see a simple example:\n\n```ts\nimport { Injector, Singleton, Transient } from \"https://deno.land/x/deninject/mod.ts\"; \n\nclass ClassA {\n    constructor() {\n        console.log(\"buildA\");\n    }\n}\n\nclass ClassB {\n    constructor(a: ClassA) {\n        console.log(\"buildB: \", a);\n    }\n}\n\nclass MyModule {\n    @Singleton()\n    public buildA(): ClassA {\n        return new ClassA();\n    }\n\n    @Transient()\n    public buildB(a: ClassA): ClassB {\n        return new ClassB(a);\n    }\n}\n\nconst injector = new Injector(new MyModule());\nconst b = injector.get(ClassB);\n```\n\n## Decorators\n\nThe decorators are responsible for making all the configuration of the dependencies. Below are their definitions:\n\n### Transient\n\nThe decorator `Transient` serves to mark a class or method of a module as a transient, this means that the `Injector` will generate a new instance for each request.\n\nAs a class:\n```ts\n@Transient()\nclass ClassA {}\n```\n\nAs a module:\n```ts\nclass ClassA {}\n\nclass MyModule {\n    @Transient()\n    public buildA(): ClassA {\n        return new ClassA();\n    }\n}\n```\n\n### Singleton\n\nThe decorator `Singleton` serves to mark a class or method of a module as a singleton, this means that the `Injector` will generate just one instance and store it in the cache, each request will return the same instance.\n\nAs a class:\n```ts\n@Singleton()\nclass ClassA {}\n```\n\nAs a module:\n```ts\nclass ClassA {}\n\nclass MyModule {\n    @Singleton()\n    public buildA(): ClassA {\n        return new ClassA();\n    }\n}\n```\n\n### Scope\n\nThe decorator `Scope` is used to mark a class or method of a module with a specific scope, this is useful for specific configurations for the `SubInjectors`. Every `SubInjector` works with a specific scope.\n\nAs a class:\n```ts\n@Transient()\n@Scope(\"scopeA\")\nclass ClassA {}\n```\n\nAs a module:\n```ts\nclass ClassA {}\n\nclass MyModule {\n    @Transient()\n    @Scope(\"scopeA\")\n    public buildA(): ClassA {\n        return new ClassA();\n    }\n}\n```\n\nTo use a `SubInjector`:\n```ts\nconst subInjector = injector.sub(\"scopeA\", new MyScopeModule());\nconst a = subInjector.get(ClassA);\n```\n\n### Token\n\nThe `Token` decorator is used to mark a class or method of a module with a specific token, this is useful for creating different constructors for the same class.\n\nAs a class:\n```ts\n@Transient()\n@Token(\"tokenA\")\nclass ClassA {}\n```\n\nAs a module:\n```ts\nclass ClassA {}\n\nclass MyModule {\n    @Transient()\n    @Token(\"tokenA\", true /*If you want use ignoreType*/)\n    public buildA(): ClassA {\n        return new ClassA();\n    }\n}\n```\n\nTo retrieve an instance:\n```ts\nconst a = injector.get(ClassA, \"tokenA\");\n```\n\n#### Inject\n\nTo inject a specific instance using a token.\n\n```ts\nclass ClassB {\n    constructor(@Inject(\"tokenA\", true /*If you want use ignoreType*/) a: ClassA) {}\n}\n```\n\n#### Dynamic Token\n\nFrom time to time we need to make a more elaborate logic when instantiating classes, for this purpose `DynamicToken` was made, to provide more freedom in the code. Here is an example:\n\n```ts\nconst tokenB = new TokenSymbol();\nconst tokenC = new TokenSymbol();\n\nclass ClassA {}\n\nclass ClassB extends ClassA {}\n\nclass ClassC extends ClassA {}\n\nclass MyModule {\n    @Transient()\n    public buildA(@DynamicToken() token: TokenSymbol): ClassA {\n        if (token == tokenB) {\n            return new ClassB();\n        }\n        else if (token == tokenC) {\n            return new ClassC();\n        }\n        else {\n            return new ClassA();\n        }\n    }\n}\n```\n\n## Symbols\n\nThe `Symbols` serve to facilitate the use of scopes and tokens in the application, it is just another option of use.\n\n### Scope Symbol\n\nIt has the same purpose as the `Scope`.\n\n```ts\nconst scopeA = new ScopeSymbol();\n\n@Transient()\n@scopeA.apply()\nclass ClassA {}\n```\n\n### Token Symbol\n\nIt has the same purpose as the `Token`.\n\n```ts\nconst tokenA = new TokenSymbol(true /*If you want use ignoreType*/);\n\n@Transient()\n@tokenA.apply()\nclass ClassA {}\n\nclass ClassB {\n    constructor(@tokenA.inject() a: ClassA) {}\n}\n```\n\n## Setup\n\nThis modules requires the following options to be set in the tsconfig:\n\n```json\n{\n  \"compilerOptions\": {\n    \"experimentalDecorators\": true,\n    \"emitDecoratorMetadata\": true\n  }\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrmcerqueira%2Fdeninject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrmcerqueira%2Fdeninject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrmcerqueira%2Fdeninject/lists"}