{"_id":"@alstradocs/service-objects","_rev":"5-24e5e7e4d86eaddd6248d4d73ada9c84","time":{"created":"2021-01-18T09:52:46.212Z","1.0.0":"2021-01-18T09:52:46.377Z","modified":"2022-04-04T13:39:12.074Z"},"name":"@alstradocs/service-objects","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@alstradocs/service-objects","version":"1.0.0","description":"> A small TypeScript library for defining, looking up and executing user defined service objects.","main":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"dev":"webpack --mode development","start":"webpack-dev-server --mode development","build":"webpack --mode production  && npm pack","test":"jest"},"repository":{"type":"git","url":"git+https://github.com/alstradocs/service-objects.git"},"keywords":[],"author":"","license":"ISC","bugs":{"url":"https://github.com/alstradocs/service-objects/issues"},"homepage":"https://github.com/alstradocs/service-objects#readme","devDependencies":{"@types/jest":"^26.0.20","awesome-typescript-loader":"^5.2.1","copy-webpack-plugin":"^6.1.1","css-loader":"^4.3.0","file-loader":"^6.1.0","html-webpack-plugin":"^4.5.0","jest":"^26.6.3","mini-css-extract-plugin":"^0.11.2","source-map-loader":"^1.1.0","ts-jest":"^26.4.4","typescript":"^4.0.3","webpack":"^4.44.2","webpack-cli":"^3.3.12","webpack-dev-server":"^3.11.0"},"dependencies":{},"gitHead":"7053188f14625698b5a95266cb8c21ffebdb2ca7","_id":"@alstradocs/service-objects@1.0.0","_nodeVersion":"10.19.0","_npmVersion":"6.14.4","dist":{"integrity":"sha512-PtwOfU22iwfpJZSTvGsQwAb7mluFgNO4JyeRfw+7qTDtDNtbatDSfy90Vk7HN+imhh4BWHhEu0zXGyou5TExAA==","shasum":"034a6c16ceefa4bc257eea20894862bfb85d232d","tarball":"https://registry.npmjs.org/@alstradocs/service-objects/-/service-objects-1.0.0.tgz","fileCount":14,"unpackedSize":18031,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgBVpuCRA9TVsSAnZWagAAqPgP/0ND0BfCx7Mt7q9jy2bo\nheuuNhiGH93nIdge1scoRU7hI7BaGW2hx7QYDw0c8jW/zHAViaZ1DkqoIpHx\nd1/kDvQH5BVIusgEA9nNArQCQUSjHyeFBSR+GxzA3BM039oJTLJJYm9Ipwup\n9IeSegvjcn/34lKPgUl5UH4jbyQEvriSDfUU5DhGwkDEBaaIovoXcDS3skJK\n5rW4fpfeFKNviCbF575tRG30eeX8XLny3Z0b5/9tIaMgcS4psiYZRfMLRtHl\nDvYK2RYm9peCACA8JtpB0nmKj0glLDBpATgkc0JCe8MLAnqKgFXk1P9WCb0/\nPG94c39sMmPpmbVi6dHqn6cbFxRdRhicBHjVhTnYmXJtLl6w3lvgrCE91c6L\nMoO7oensy4k2HD4cKl1sAVCCBaTZCIZpiZkoXjWjPYJIi5Dc4AC/97mROnSP\nporJtFLJV/vJrjS50GJbsywjoXfZS5v4jlJOY3l7zKK123zBLwkoq6fomhlW\nBnTOGeDcnn7si7G/6XDARyXjWHlmNb4Ewj7jhCo90CTwM2J258queA7MSYjX\n1k2MgkKJpcKP7svL1T3kdffqnSe1wmdpZFNqOYOoT2IMHDxKQR/LqWxSmtoB\ngal3Q1uYatAjZ+Ra+jQ6STp6oXUcVRBH1xW2UNwJi3BKTsPnffLgK35zUNf8\nmggo\r\n=Ac0z\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCyPXenfrEhTEUodMJuT/W2IgnWhs6G1vIeY+G7nNZmRgIhAN7IjqAXuwWJMu6mhf7EAY17bgSSUYHrDKSON+oDXXmc"}]},"_npmUser":{"name":"alstradocs","email":"alstradocs@gmail.com"},"directories":{},"maintainers":[{"name":"alstradocs","email":"alstradocs@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/service-objects_1.0.0_1610963566256_0.34821846822974845"},"_hasShrinkwrap":false}},"maintainers":[{"name":"alstradocs","email":"alstradocs@gmail.com"}],"description":"> A small TypeScript library for defining, looking up and executing user defined service objects.","homepage":"https://github.com/alstradocs/service-objects#readme","keywords":[],"repository":{"type":"git","url":"git+https://github.com/alstradocs/service-objects.git"},"bugs":{"url":"https://github.com/alstradocs/service-objects/issues"},"license":"ISC","readme":"# Service Objects\n> A small TypeScript library for defining, looking up and executing user defined service objects.\n\n[![NPM Version][npm-image]][npm-url]\n<!-- [![Build Status][travis-image]][travis-url] -->\n[![Downloads Stats][npm-downloads]][npm-url]\n\n![](header.png)\n\nA service is a user defined class that implements the IService interface.\n\n```typescript\nexport interface IService<T, U> {\n\n    execute(context: IServiceContext<T>): U;\n}\n```\n\nAdditionally a service has a constructor interface.\n\n```typescript\nexport interface IServiceConstructor {\n\n    new(...args: any[]): IServiceInterface; \n\n    /**\n     * Every service class has a static \n     * serviceName property\n     */   \n    serviceName: string;\n}\n```\nThis library defines an API for creating, storing, looking up and executing services. This is achieved with the help of service executors, service repositories and decorators.\n\n```typescript\nimport { IExecutionContext, IServiceInterface, IServiceConstructor } from \"@alstradocs/service-objects\";\n\nexport interface IServiceExecutor {\n\n    executeService<T, U>(context: IExecutionContext<T>): U\n}\n\nexport interface IServiceRepository {\n\n    get(serviceName: string): IServiceInterface;\n\n    register(serviceName: string, serviceConstructor: IServiceConstructor): void;\n}\n```\n\n## Installation\n\nNPM:\n\n```sh\nnpm install @alstradocs/service-objects --save\n```\n\n## Usage example\n\n### 1. Define Services\nFirst you need a service. You can simply annotate a class with the @IsService decorator.\n\n```typescript\nimport { IsService, IServiceContext } from \"@alstradocs/service-objects\";\n\ninterface BinaryOperands {\n    firstOperand: number;\n    secondOperand: number;\n}\n\n@IsService()\nclass AdditionService {\n\n    public static serviceName = 'AdditionService';\n\n    execute(context: IServiceContext<BinaryOperands>): number {\n        let { firstOperand, secondOperand } = context.data;\n        return firstOperand + secondOperand;\n    }\n}\n```\n### 2. Instantiate/Define Service Repository \nNext you add your shining new service to an instance of IServiceRepository. A service repository is just a fancy name for a service store. You can use the default repository \n```typescript\nimport { ServiceRepository } from \"@alstradocs/service-objects\";\n\n// Initialize service repo with just a single service ...\nlet serviceInfo = { \n        serviceName:AdditionService.serviceName, \n        serviceContructor: AdditionService \n};\nlet repository = new ServiceRepository([serviceInfo]);\n\n// ... or add to existing repo\nrepository.register(AdditionService.serviceName, AdditionService);\n```\nOr you can implement your own repository \n\n```typescript\nimport { IsServiceRepository, IServiceConstructor } from \"@alstradocs/service-objects\";\n\n@IsServiceRepository()\nexport class MyServiceRepository {\n\n    get(serviceName: string): IServiceInterface {\n        ...\n    }\n\n    register(serviceName: string, serviceConstructor: IServiceConstructor): void {\n        ...\n    }\n}\n// ... Instantiate repo and add service\nlet repository = new MyServiceRepository();\nrepository.register(AdditionService.serviceName, AdditionService);\n```\n\n### 3. Define/Instanstiate A Service Execcutor\nTo execute your service, you need an instance of IServiceExecutor. The service executor is a object that knows how to find and execute services. It uses the service repository for this. You can use the default executor.\n\n```typescript\nimport { ServiceRepository } from \"@alstradocs/service-objects\";\n\nlet repository = // initialize repo\n// instantiate executor with given repo\nlet serviceExecutor = new ServiceExecutor(repository);\n```\nOr you can implement your own executor \n\n```typescript\nimport { IsServiceExecutor, IExecutionContext } from \"@alstradocs/service-objects\";\n\n@IsServiceExecutor()\nexport class MyServiceExecutor {\n    \n    executeService<T, U>(context: IExecutionContext<T>): U {\n        ...\n    }\n}\n// ... instantiate and execute (context contains all info required to execute a service)\nlet serviceExecutor = new MyServiceExecutor();\n```\n### 4. Execute Service\nThe final step is executing the service.\n```typescript\nserviceExecutor.executeService(context);\n```\n\nTypically all the entire setup above (apart from step 4) will be done once before or during app/script inititialization. You can then store the executor app/script wide and\nmake it available to execute services as needed.\n\n_For more examples and usage, please refer to the source code._\n\n## Release History\n\n* 1.0.0\n    * Work in progress\n\n## Meta\n\nAlstraDocs – [@AlstraDocs](https://twitter.com/alstradocs) – alstradocs@gmail.com\\\nEdward Banfa – [@EdwardBanfa](https://twitter.com/edwardbanfa) – ebanfa@gmail.com\n\nDistributed under the Apache license. See ``LICENSE`` for more information.\n\n[https://github.com/alstradocs/service-objects](https://github.com/alstradocs/)\n\n## Readme Image\n<a href='https://pngtree.com/so/robot-clipart'>robot clipart png from pngtree.com</a>\n<!-- Markdown link & img dfn's -->\n[npm-image]: https://img.shields.io/npm/v/@alstradocs/service-objects\n[npm-url]: https://npmjs.org/package/@alstradocs/service-objects\n[npm-downloads]: https://img.shields.io/npm/dw/@alstradocs/service-objects\n[wiki]: https://github.com/alstradocs/service-objects/wiki\n","readmeFilename":"README.md"}