{"id":25837268,"url":"https://github.com/devtin/pleasure-di","last_synced_at":"2025-03-01T02:48:06.737Z","repository":{"id":57325874,"uuid":"336623307","full_name":"devtin/pleasure-di","owner":"devtin","description":"a simple dependency injection module","archived":false,"fork":false,"pushed_at":"2021-02-09T18:32:59.000Z","size":83,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"dev","last_synced_at":"2023-09-20T05:14:38.006Z","etag":null,"topics":[],"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/devtin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2021-02-06T19:52:20.000Z","updated_at":"2021-02-09T18:33:02.000Z","dependencies_parsed_at":"2022-09-21T02:01:07.831Z","dependency_job_id":null,"html_url":"https://github.com/devtin/pleasure-di","commit_stats":null,"previous_names":[],"tags_count":2,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devtin%2Fpleasure-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devtin%2Fpleasure-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devtin%2Fpleasure-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devtin%2Fpleasure-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devtin","download_url":"https://codeload.github.com/devtin/pleasure-di/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241309091,"owners_count":19941726,"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":"2025-03-01T02:48:06.135Z","updated_at":"2025-03-01T02:48:06.725Z","avatar_url":"https://github.com/devtin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv\u003e\u003ch1\u003epleasure-di\u003c/h1\u003e\u003c/div\u003e\n\n\u003cp\u003e\n    \u003ca href=\"https://www.npmjs.com/package/pleasure-di\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/pleasure-di.svg\" alt=\"Version\"\u003e\u003c/a\u003e\n\u003ca href=\"http://opensource.org/licenses\" target=\"_blank\"\u003e\u003cimg src=\"http://img.shields.io/badge/License-MIT-brightgreen.svg\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp\u003e\n    a simple dependency injection module\n\u003c/p\u003e\n\n## Installation\n\n```sh\n$ npm i pleasure-di --save\n# or\n$ yarn add pleasure-di\n```\n\n## Features\n\n- [register containers](#register-containers)\n- [injects dependencies](#injects-dependencies)\n- [provides an interface to customize the container resolution](#provides-an-interface-to-customize-the-container-resolution)\n\n\n\u003ca name=\"register-containers\"\u003e\u003c/a\u003e\n\n## register containers\n\n\n```js\nconst container = pleasureDi({\n  Gateway (name) {\n    if (name === 'NotFoundGateway') {\n      return\n    }\n\n    return () =\u003e {}\n  }\n})\n\nt.throws(() =\u003e container.SomeService, {\n  message: 'No suffixed container name registered matching SomeService'\n})\n\nt.throws(() =\u003e container.NotFoundGateway, {\n  message: 'Resource NotFoundGateway not found'\n})\nt.notThrows(() =\u003e container.SomeGateway)\n```\n\n\u003ca name=\"injects-dependencies\"\u003e\u003c/a\u003e\n\n## injects dependencies\n\n\n```js\nconst container = pleasureDi({\n  Gateway (gatewayName) {\n    // holds the logic that resolves given gatewayName\n    // returns a function that will come with the container\n    return (/* container */) =\u003e {\n      return {\n        methodA (caller) {\n          return `methodA from ${gatewayName} called by ${caller}`\n        },\n        methodB (caller) {\n          return `methodB from ${gatewayName} called by ${caller}`\n        }\n      }\n    }\n  },\n  Service (serviceName) {\n    // holds the logic that resolves given serviceName\n    // returns a function that will come with the container\n    return ({ FreakingGateway }) =\u003e {\n      return {\n        methodC () {\n          return FreakingGateway.methodA(serviceName)\n        },\n        methodD () {\n          return FreakingGateway.methodB(serviceName)\n        }\n      }\n    }\n  }\n})\n\nconst { PaymentService } = container\n\nt.is(PaymentService.methodC(), 'methodA from FreakingGateway called by PaymentService')\nt.is(PaymentService.methodD(), 'methodB from FreakingGateway called by PaymentService')\n```\n\n\u003ca name=\"provides-an-interface-to-customize-the-container-resolution\"\u003e\u003c/a\u003e\n\n## provides an interface to customize the container resolution\n\n\n```js\nconst resolveFixture = (...fixturePath) =\u003e {\n  return path.join(__dirname, '__tests__/fixtures', ...fixturePath)\n}\nconst container = pleasureDi({\n  Gateway (gatewayName) {\n    return require(resolveFixture('gateways', gatewayName) + '.js')\n  },\n  Repository (respositoryName) {\n    return require(resolveFixture('repositories', respositoryName) + '.js')\n  },\n  Service (serviceName) {\n    return require(resolveFixture('services', serviceName) + '.js')\n  }\n})\n\nconst { OrderService } = container\n\nt.deepEqual(OrderService.findByUserId(123), {\n  id: 123\n})\nt.is(OrderService.pay(1111), 'amount 1111 paid')\nt.throws(() =\u003e container.SomeService, {\n  code: 'MODULE_NOT_FOUND'\n})\n```\n\n\n\u003cbr\u003e\u003ca name=\"pleasureDi\"\u003e\u003c/a\u003e\n\n### pleasureDi(resolvers) ⇒ \u003ccode\u003eObject\u003c/code\u003e \\| \u003ccode\u003e\\*\u003c/code\u003e\n\n| Param | Type | Description |\n| --- | --- | --- |\n| resolvers | \u003ccode\u003eObject\u003c/code\u003e | resolvers map |\n\n\n\u003cbr\u003e\u003ca name=\"Resolved\"\u003e\u003c/a\u003e\n\n### Resolved ⇒ \u003ccode\u003eObject\u003c/code\u003e\n\n| Param | Type |\n| --- | --- |\n| container | \u003ccode\u003eObject\u003c/code\u003e | \n\n\n\u003cbr\u003e\u003ca name=\"Resolver\"\u003e\u003c/a\u003e\n\n### Resolver ⇒ [\u003ccode\u003eResolved\u003c/code\u003e](#Resolved)\n\n| Param | Type |\n| --- | --- |\n| containerName | \u003ccode\u003eString\u003c/code\u003e | \n\n\n* * *\n\n### License\n\n[MIT](https://opensource.org/licenses/MIT)\n\n\u0026copy; 2020-present Martin Rafael Gonzalez \u003ctin@devtin.io\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevtin%2Fpleasure-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevtin%2Fpleasure-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevtin%2Fpleasure-di/lists"}