{"id":26550135,"url":"https://github.com/swingcosmic/inject-it","last_synced_at":"2026-04-29T14:37:27.438Z","repository":{"id":277667868,"uuid":"933109982","full_name":"SwingCosmic/inject-it","owner":"SwingCosmic","description":"Strong-typed dependency injection for both node.js and browser","archived":false,"fork":false,"pushed_at":"2025-02-15T09:10:05.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-31T15:28:02.690Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/SwingCosmic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-15T06:54:55.000Z","updated_at":"2025-02-15T09:10:09.000Z","dependencies_parsed_at":"2025-02-15T10:20:26.373Z","dependency_job_id":"b2aca5f4-77cb-450a-9f32-b7a4b7045d1c","html_url":"https://github.com/SwingCosmic/inject-it","commit_stats":null,"previous_names":["swingcosmic/inject-it"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SwingCosmic/inject-it","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Finject-it","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Finject-it/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Finject-it/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Finject-it/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SwingCosmic","download_url":"https://codeload.github.com/SwingCosmic/inject-it/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Finject-it/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32429379,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T13:34:34.882Z","status":"ssl_error","status_checked_at":"2026-04-29T13:34:29.830Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":[],"created_at":"2025-03-22T07:30:27.629Z","updated_at":"2026-04-29T14:37:27.407Z","avatar_url":"https://github.com/SwingCosmic.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# inject-it\n\n基于TypeScript强类型的轻量级依赖注入容器，支持node.js和浏览器环境\n\n## 特性\n\n* 支持node.js和浏览器环境\n* 非常轻量，除了polyfill，没有其他依赖\n* 采用 [reflect-metadata](https://github.com/rbuckton/ReflectDecorators) 通过装饰器为类型声明注入依赖\n* 如果`tsconfig.json`中设置`emitDecoratorMetadata`为true，则支持根据tsc 编译器生成的元数据自动获取依赖信息\n* 可以通过依赖的接口名称来注入依赖，支持非构造函数和类的依赖，例如普通对象实例\n* 支持构造函数注入，通过`@service`装饰器\n* 支持属性注入，通过`@autowired`装饰器\n* 支持工厂方法和单实例\n* 支持模块（插件化）注入，暴露一个安装方法，批量注册依赖\n* 依赖异步初始化\n\n## 设计理念\n\ninject-it注册的依赖默认采用Singleton模式，即重复获取同一个类型（接口名或者构造函数），始终返回一个实例。这和一些通常在web服务中使用的IoC容器的默认方式不一样，它们可能默认采用的是Transient或者Scoped模式。\n\n这种设计是 **刻意** 的，目的是为了降低单用户/无用户体系的客户端和web应用使用的复杂度，并且更好地和vue等前端框架进行整合。通常在这些框架中，会有较多的在应用生命周期中始终存在的依赖，并且往往以全局状态的方式进行管理和使用，极少涉及作用域和资源释放。\n\ninject-it希望在这些应用中，执行业务逻辑和获取依赖的代码，交由容器来完成，不再依赖全局对象，同时把UI交互和数据逻辑解耦，使得逻辑可以模块化和封装，提升可维护性和扩展性。\n## 使用\n\n\u003e ⚠️ 同一个类，只支持构造函数注入和属性注入中的一种。\n  推荐使用构造函数注入，这样该类可以在容器以外的地方手动创建实例\n\n\u003e ⚠️ 注入的参数尽量避免包含值类型(Number, Boolean, BigInt), Array, Symbol和String，使用这些参数可能会带来无法预测的结果\n\n### 构造函数注入\n\n`@service`装饰器的参数为该类构造函数的参数对应的依赖类型的数组，可以是类构造函数或者接口名。数组元素的数量和顺序与构造函数参数一一对应，且参数不能是可选的\n\n```typescript\nimport { ServiceBuilder, service, autowired } from \"@lovekicher/inject-it\";\n\n\ninterface TimeoutConfig {\n  timeout: number;\n}\n\ninterface IDataService {\n  getData(): Promise\u003cnumber[]\u003e;\n}\n\n// 声明依赖，可以是接口\n@service([\"TimeoutConfig\"])\nclass DataService implements IDataService {\n\n  private readonly config: TimeoutConfig;\n  constructor(config: TimeoutConfig) {\n    this.config = config;\n  }\n\n  async getData(): Promise\u003cnumber[]\u003e {\n    await new Promise((resolve) =\u003e {\n      setTimeout(() =\u003e {\n        resolve();\n      }, this.config.timeout);\n    });\n    return [1, 2, 3];\n  }\n}\n\n```\n\n### 属性注入\n\n`@autowired`装饰器的参数为该属性对应的依赖类型的构造函数或者接口名\n\n可以将属性声明为`private readonly`。由于js的限制，真正的私有属性（以#开头）无法通过外部写入，请不要注入这些属性。\n\n\u003e ⚠️ 属性注入要求该类有无参构造函数，且构造函数中没有需要清理的副作用代码\n\n```typescript\n\nclass DataService implements IDataService {\n\n  @autowired(\"TimeoutConfig\")\n  private readonly config!: TimeoutConfig;\n\n  async getData(): Promise\u003cnumber[]\u003e {\n    await new Promise((resolve) =\u003e {\n      setTimeout(() =\u003e {\n        resolve();\n      }, this.config.timeout);\n    });\n    return [1, 2, 3];\n  }\n}\n\n```\n\n### 使用容器\n\n```typescript\n\n// 注册依赖\nconst container = new ServiceBuilder()\n  .instance({ timeout: 1000 }, \"TimeoutConfig\")\n  .constructorInject(DataService, \"IDataService\")\n  .build();\n\n// 使用容器\nconst service = container.resolve\u003cIDataService\u003e(\"IDataService\");\nawait service.getData();\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswingcosmic%2Finject-it","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswingcosmic%2Finject-it","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswingcosmic%2Finject-it/lists"}