{"id":13624032,"url":"https://github.com/annojs/inject","last_synced_at":"2025-04-15T20:33:04.454Z","repository":{"id":13186810,"uuid":"15870321","full_name":"annojs/inject","owner":"annojs","description":"Injects dependencies to JavaScript modules and packages (MIT)","archived":false,"fork":false,"pushed_at":"2018-03-17T12:48:43.000Z","size":10,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T18:48:22.245Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/annoinject","language":"JavaScript","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/annojs.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}},"created_at":"2014-01-13T14:23:52.000Z","updated_at":"2018-09-20T14:15:49.000Z","dependencies_parsed_at":"2022-09-10T19:01:17.027Z","dependency_job_id":null,"html_url":"https://github.com/annojs/inject","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/annojs%2Finject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/annojs%2Finject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/annojs%2Finject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/annojs%2Finject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/annojs","download_url":"https://codeload.github.com/annojs/inject/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248359229,"owners_count":21090498,"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":[],"created_at":"2024-08-01T21:01:38.039Z","updated_at":"2025-04-15T20:33:04.165Z","avatar_url":"https://github.com/annojs.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![build status](https://secure.travis-ci.org/annojs/inject.svg)](http://travis-ci.org/annojs/inject)\n# inject - Injects dependencies to JavaScript modules and packages\n\nDefault `require` provided by Node.js isn't that flexible. Let's say you want to test a module and inject a mock database driver and configuration there. What to do? One alternative is to use a dependency injection pattern like this:\n\n**main.js:**\n\n```javascript\nconst db = require(\"./db\");\nconst config = require(\"./config\");\nconst api = require(\"./api\")({ db, config });\n```\n\n**api.js:**\n\n```javascript\nmodule.exports = imports =\u003e () =\u003e {\n    // do something with imports.db, imports.config\n    // ...\n};\n```\n\n## Module Injection\n\n`annoinject` provides a set of utilities that build upon this idea and make sure all imports needed actually have been injected. In `inject`'s case we would write the following:\n\n**api.js:**\n\n```javascript\nmodule.exports = require(\"annoinject\")([\"db\", \"config\"], imports =\u003e {\n    // do something with imports.db, imports.config\n});\n```\n\nYes, there's more to write but at the same time it is more explicit. In addition `annoinject` performs the extra check I mentioned about. It will give you a nice error in case some dependency hasn't been satisfied.\n\n## Package Injection\n\nThere are time when you would you like to inject the same dependencies for the whole package. You could for instance want to use the same configuration for each module included. In this case we can use a package level injector like this:\n\n**api/main.js:**\n\n```javascript\nconst config = {\n    apikey: 'foobar'\n};\nconst api = require(\"./api\")({ config });\n\n// then we can do\napi.countries();\n```\n\n**api/index.js:**\n\n```javascript\nmodule.exports = require(\"annoinject\")(\"config\");\n```\n\n**api/countries.js:**\n\n```javascript\nmodule.exports = imports =\u003e {\n    // do something with imports.config now\n\n    return () =\u003e console.log('get countries now');\n};\n```\n\nJust like the module injector, the package injector will make sure all required modules will get injected and give an Error in case they are not.\n\n## License\n\n`annoinject` is available under MIT. See LICENSE for more details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fannojs%2Finject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fannojs%2Finject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fannojs%2Finject/lists"}