{"id":21291827,"url":"https://github.com/testingrequired/ioc","last_synced_at":"2025-03-15T16:40:16.720Z","repository":{"id":41782884,"uuid":"167492302","full_name":"testingrequired/ioc","owner":"testingrequired","description":"A simple dependency injection implementation.","archived":false,"fork":false,"pushed_at":"2023-01-04T02:06:50.000Z","size":622,"stargazers_count":0,"open_issues_count":21,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-22T06:28:22.912Z","etag":null,"topics":["dependency-injection","inversion-of-control"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/testingrequired.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":"2019-01-25T05:49:05.000Z","updated_at":"2020-02-06T20:36:34.000Z","dependencies_parsed_at":"2023-02-01T17:35:12.902Z","dependency_job_id":null,"html_url":"https://github.com/testingrequired/ioc","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testingrequired%2Fioc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testingrequired%2Fioc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testingrequired%2Fioc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testingrequired%2Fioc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/testingrequired","download_url":"https://codeload.github.com/testingrequired/ioc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243762227,"owners_count":20343972,"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":["dependency-injection","inversion-of-control"],"created_at":"2024-11-21T13:46:19.703Z","updated_at":"2025-03-15T16:40:16.696Z","avatar_url":"https://github.com/testingrequired.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/testingrequired/ioc.svg?branch=master)](https://travis-ci.org/testingrequired/ioc)\n\n# @testingrequired/ioc\n\nA simple dependency injection implementation.\n\n## Notice\n\nThis is not intended for production use.\n\n## Installation\n\n```bash\n$ npm i @testingrequired/ioc\n```\n\n## Getting Started\n\n### Components\n\nDefine components with the `component` decorator.\n\n#### Example\n\n```javascript\nimport { component, register } from \"@testingrequired/ioc\";\n\n@component\nclass Component {}\n```\n\n#### Register Example\n\nComponents can also be defined using the `register` function for more control.\n\n```javascript\nimport { component, register, instance } from \"@testingrequired/ioc\";\n\nclass RandomEachTime {}\n\nregister(\n  RandomEachTime,\n  () =\u003e {\n    const component = new RandomEachTime();\n    component.value = Math.floor(Math.random() * (10000 - 0) + 0);\n    return component;\n  },\n  { lifetime: instance }\n);\n```\n\n### Dependencies\n\nThe `inject` function defines a dependency to a component.\n\n```javascript\nimport { component, inject, resolve } from \"@testingrequired/ioc\";\n\n@component\nclass Child {}\n\n@component\nclass Parent {\n  @inject(Child) child;\n}\n\nconst parent = resolve(Parent);\n\nparent.child instanceof Child === true;\n```\n\n## Example\n\nSee the [working example](example/README.md)\n\n## Testing\n\nA container's register function can be passed a custom factory even after dependency resolution. This factory can return a mock/spy/stub.\n\n```javascript\nimport { makeContainer } from \"@testingrequired/ioc\";\n\nconst container = makeContainer();\n\n@container.component\nclass Child {}\n\nclass Parent {\n  @container.inject(Child) child;\n}\n\nclass Sibling {}\n\ncontainer.register(Child, () =\u003e new Sibling());\n\nconst parent = containter.resolve(Parent);\n\nparent.child instanceof Sibling === true;\n```\n\n## Development\n\n### build\n\nBuild project in to `dist`\n\n```bash\n$ npm run build\n```\n\n#### watch\n\nBuild project when files change.\n\n```bash\n$ npm run build -- -w\n```\n\n### test\n\nRun tests\n\n```bash\nnpm run test\n```\n\n### example\n\nRun example app.\n\nThis will use current build in `dist`.\n\n```bash\n$ npm run example\n```\n\n## API\n\n### register(componentKey, [factory, [options = {}]])\n\nRegister a class as a component.\n\n```javascript\nimport { register } from \"@testingrequired/ioc\";\n\nclass Foo {}\n\nregister(Foo);\n```\n\n#### component\n\nThe class, string or symbol used to identify the component.\n\n#### factory\n\nFunction to be called when creating instance of component. This defaults to calling `new` on component.\n\n#### options\n\n##### lifetime\n\nThere are two lifetime options: `session` (one instance for every resolve) \u0026 `instance` (new instance on each resolve).\n\n```javascript\nimport { register, session, instance } from \"@testingrequired/ioc\";\n\nclass Singleton {}\n\nregister(Singleton, null, { lifetime: session });\n\nclass NewEveryTime {}\n\nregister(NewEveryTime, null, { lifetime: instance });\n```\n\n### register.fn(componentKey, fn)\n\nRegister a functional component.\n\n```javascript\nimport { component, register } from \"@testingrequired/ioc\";\n\n@component\nclass ComponentA {}\n\n@component\nclass ComponentB {}\n\nexport default register.fn(\n  [ComponentA, ComponentB],\n  (componentA, componentB) =\u003e /* ...implementation */\n);\n```\n\n### resolve(componentKey)\n\nReturn instance of component.\n\n```javascript\nimport { resolve } from \"@testingrequired/ioc\";\n\nconst foo = resolve(Foo);\n```\n\n### component\n\nRegister a class as an injectable component.\n\n```javascript\nimport { component } from \"@testingrequired/ioc\";\n\n@component\nclass Foo {}\n```\n\n#### component(options = {})\n\nPass options while registering a component.\n\n```javascript\nimport { component } from \"@testingrequired/ioc\";\n\n@component({})\nclass Foo {}\n```\n\n##### Options\n\n###### Lifetime\n\nThere are two lifetime options: `session` (one instance for every resolve) \u0026 `instance` (new instance on each resolve).\n\n```javascript\nimport { component, session, instance } from \"@testingrequired/ioc\";\n\n@component({ lifetime: session })\nclass Singleton {}\n\n// same as:\n\n@component.session\nclass Singleton {}\n\n// ---\n\n@component({ lifetime: instance })\nclass NewEveryTime {}\n\n// same as:\n\n@component.instance\nclass NewEveryTime {}\n```\n\n### inject(componentKey)\n\nInjects initialized component.\n\n```javascript\nimport { inject } from \"@testingrequired/ioc\";\n\nclass Bar {\n  @inject(Foo) foo;\n}\n```\n\n### makeContainer\n\nCreate a container to manage components.\n\n```javascript\nimport { makeContainer } from \"@testingrequired/ioc\";\n\nconst container = makeContainer();\n\n@container.component\nclass Foo {}\n\nclass Bar {\n  @container.inject(Foo) foo;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestingrequired%2Fioc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftestingrequired%2Fioc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestingrequired%2Fioc/lists"}