{"id":16181810,"url":"https://github.com/ericclemmons/diez","last_synced_at":"2025-03-19T02:30:32.207Z","repository":{"id":25459136,"uuid":"28889409","full_name":"ericclemmons/diez","owner":"ericclemmons","description":"Incredibly simple, Dependency Injection for isomorphic Javascript applications.","archived":false,"fork":false,"pushed_at":"2016-07-05T17:42:22.000Z","size":126,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-11T06:27:42.696Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ericclemmons.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":"2015-01-06T23:56:04.000Z","updated_at":"2016-07-09T03:15:55.000Z","dependencies_parsed_at":"2022-08-19T18:30:14.120Z","dependency_job_id":null,"html_url":"https://github.com/ericclemmons/diez","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fdiez","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fdiez/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fdiez/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fdiez/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ericclemmons","download_url":"https://codeload.github.com/ericclemmons/diez/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221720251,"owners_count":16869446,"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-10-10T06:28:01.464Z","updated_at":"2024-10-27T18:59:43.570Z","avatar_url":"https://github.com/ericclemmons.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Diez ![https://img.shields.io/npm/v/diez.svg](https://img.shields.io/npm/v/diez.svg?style=flat-square)\n\n\u003e Incredibly simple, Dependency Injection for isomorphic Javascript applications.\n\n[![](https://img.shields.io/github/issues-raw/ericclemmons/diez.svg?style=flat-square)](https://github.com/ericclemmons/diez/issues)\n[![](https://img.shields.io/travis/ericclemmons/diez/master.svg?style=flat-square)](https://travis-ci.org/ericclemmons/diez)\n[![](https://img.shields.io/david/ericclemmons/diez.svg?style=flat-square)](https://david-dm.org/ericclemmons/diez#info=dependencies)\n[![](https://img.shields.io/david/dev/ericclemmons/diez.svg?style=flat-square)](https://david-dm.org/ericclemmons/diez#info=devDependencies)\n\n\n## The Problem\n\nWhen using libraries such as [React][1], [React Router][2], \u0026 [RefluxJS][3],\nyou'll find that **singletons don't work well on the server-side**\nfor Actions, Routes, Stores, \u0026 Views, as they share state between all requests.\n\n_On the client-side, each user's browser serves as a container to scope\nfunctions.  Similarly, Diez introduces a container for each request on the server._\n\n\n## The Solution\n\nLuckily, all it takes is to turn your _singletons_ into _factories_ by wrapping\nthem with `function(...) { return ...; }` \u0026 registering them via `diez.register`.\n\nDiez will retrieve them while **isolating instances to a single container-per-request**.\n\n- - -\n\n## Demo\n\n- [Server-side React with Diez \u0026 Express][4] ([source][5])\n\n- - -\n\n## Getting Started\n\nSuppose you're rendering your [React][1] view on the server with [React Router][2],\nbut rely on `request`-specific data, such as the user's `ip`, for some reason.\n\n\n### Step 0 - Install Diez\n\n```shell\n$ npm install --save diez\n```\n\n### Step 1 - Create a container per request\n\nIn your application middleware:\n\n```javascript\n// server.js\napp.use(function(req, res, next) {\n  // This container \u0026 it's references are sandboxed to this request.\n  req.container = diez.container();\n  req.container.register('request', req);\n});\n```\n\nNow, every call to `req.container.get('request')` returns `req`.\n\n\n### Step 2 - Inject [React][1] components with dependencies\n\n**Before**, your view is a singleton:\n\n```javascript\n// MyView.js\nvar MyView = React.createClass({\n  render: function() {\n    \u003cp\u003e\n      Hello!\n    \u003c/p\u003e\n  }\n});\n\nmodule.exports = MyView;\n```\n\n**After**, your view is a factory with dependencies defined:\n\n```javascript\n// MyView.js\nvar diez = require('diez');\n\n// Convert singleton to factory\nvar MyView = function(request) {\n  return React.createClass({\n    render: function() {\n      \u003cp\u003e\n        Hello, {request.ip}!\n      \u003c/p\u003e\n    }\n  });\n};\n\n// Register component with dependencies\ndiez.register(MyView, ['request']);\n\nmodule.exports = MyView;\n```\n\n\n### Step 3 - Retrieve injected components\n\n```javascript\napp.get('/', function(req, res) {\n  // Get instantiated MyView with dependencies injected\n  var component = req.container.get(MyView);\n  var element   = React.createElement(view);\n\n  res.send(React.renderToString(element));\n});\n```\n\nThat's it!\n\n- - -\n\n## [License][6]\n\n\u003e Internet Systems Consortium license\n\u003e ===================================\n\u003e\n\u003e Copyright (c) 2015 Eric Clemmons\n\u003e\n\u003e Permission to use, copy, modify, and/or distribute this software for any purpose\n\u003e with or without fee is hereby granted, provided that the above copyright notice\n\u003e and this permission notice appear in all copies.\n\u003e\n\u003e THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\n\u003e REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND\n\u003e FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\n\u003e INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS\n\u003e OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\n\u003e TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF\n\u003e THIS SOFTWARE.\n\n\n[1]: http://facebook.github.io/react/\n[2]: https://github.com/rackt/react-router\n[3]: https://github.com/spoike/refluxjs/\n[4]: https://protected-castle-7387.herokuapp.com/\n[5]: https://github.com/ericclemmons/diez/tree/master/examples/express\n[6]: https://github.com/ericclemmons/diez/blob/master/LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericclemmons%2Fdiez","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fericclemmons%2Fdiez","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericclemmons%2Fdiez/lists"}