{"id":24087121,"url":"https://github.com/hapipal/schmervice","last_synced_at":"2025-04-28T11:26:39.080Z","repository":{"id":43217543,"uuid":"111245119","full_name":"hapipal/schmervice","owner":"hapipal","description":"A service layer for hapi","archived":false,"fork":false,"pushed_at":"2024-07-26T04:28:59.000Z","size":73,"stargazers_count":48,"open_issues_count":2,"forks_count":7,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-28T11:25:58.580Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hapipal.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-18T22:39:28.000Z","updated_at":"2023-07-27T12:03:01.000Z","dependencies_parsed_at":"2024-06-18T17:05:54.550Z","dependency_job_id":"623edd38-bee2-463b-886f-23c25d403446","html_url":"https://github.com/hapipal/schmervice","commit_stats":{"total_commits":55,"total_committers":7,"mean_commits":7.857142857142857,"dds":"0.21818181818181814","last_synced_commit":"2c12345ea5b095c71c8bced85b831ce54c0df300"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapipal%2Fschmervice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapipal%2Fschmervice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapipal%2Fschmervice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapipal%2Fschmervice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hapipal","download_url":"https://codeload.github.com/hapipal/schmervice/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251303791,"owners_count":21567755,"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-01-10T03:02:14.628Z","updated_at":"2025-04-28T11:26:39.030Z","avatar_url":"https://github.com/hapipal.png","language":"JavaScript","readme":"# schmervice\n\nA service layer for hapi\n\n[![Build Status](https://travis-ci.org/hapipal/schmervice.svg?branch=main)](https://travis-ci.org/hapipal/schmervice) [![Coverage Status](https://coveralls.io/repos/hapipal/schmervice/badge.svg?branch=main\u0026service=github)](https://coveralls.io/github/hapipal/schmervice?branch=main)\n\nLead Maintainer - [Devin Ivy](https://github.com/devinivy)\n\n## Installation\n```sh\nnpm install @hapipal/schmervice\n```\n\n## Usage\n\u003e See also the [API Reference](API.md)\n\u003e\n\u003e Schmervice is intended for use with hapi v20+ and nodejs v16+ (_see v2 for lower support_).\n\nServices are a nice way to organize related business logic or transactions into classes.  Schmervice is a service layer designed to integrate nicely with hapi.  It consists of two parts that can be used together or separately:\n\n  1. a base `Service` class that integrates your service with hapi by:\n      - giving it access to the relevant `server` and plugin `options`.\n      - allowing you to implement `async initialize()` and `async teardown()` methods that should run when the server initializes and stops.\n      - allowing you to configure certain methods as being cacheable, with all the server method configuration that you're accustomed to.\n\n  2. a hapi plugin that allows you to register services and access them where it is most convenient, such as in route handlers.  This registry respects plugin boundaries and is hierarchical, so unrelated plugins can safely register their own services without affecting each other.\n\n\n```js\nconst Schmervice = require('@hapipal/schmervice');\nconst Hapi = require('@hapi/hapi');\n\n(async () =\u003e {\n\n    const server = Hapi.server();\n\n    await server.register(Schmervice);\n\n    server.registerService(\n        class MathService extends Schmervice.Service {\n\n            add(x, y) {\n\n                this.server.log(['math-service'], 'Adding');\n\n                return Number(x) + Number(y);\n            }\n\n            multiply(x, y) {\n\n                this.server.log(['math-service'], 'Multiplying');\n\n                return Number(x) * Number(y);\n            }\n        }\n    );\n\n    server.route({\n        method: 'get',\n        path: '/add/{a}/{b}',\n        handler: (request) =\u003e {\n\n            const { a, b } = request.params;\n            const { mathService } = request.services();\n\n            return mathService.add(a, b);\n        }\n    });\n\n    await server.start();\n\n    console.log(`Start adding at ${server.info.uri}`);\n})();\n```\n\n### Functional style\n\nSchmervice allows you to write services in a functional style in additional to the class-oriented approach shown above.  [`server.registerService()`](API.md#serverregisterserviceservicefactory) can be passed a plain object or a factory function.  Just make sure to name your service using the [`Schmervice.name`](API.md#schmervicename) symbol or a `name` property.  Here's a functional adaptation of the example above:\n\n```js\nconst Schmervice = require('@hapipal/schmervice');\nconst Hapi = require('@hapi/hapi');\n\n(async () =\u003e {\n\n    const server = Hapi.server();\n\n    await server.register(Schmervice);\n\n    server.registerService(\n        (srv) =\u003e ({\n            [Schmervice.name]: 'mathService',\n            add: (x, y) =\u003e {\n\n                srv.log(['math-service'], 'Adding');\n\n                return Number(x) + Number(y);\n            },\n            multiply: (x, y) =\u003e {\n\n                srv.log(['math-service'], 'Multiplying');\n\n                return Number(x) * Number(y);\n            }\n        })\n    );\n\n    server.route({\n        method: 'get',\n        path: '/add/{a}/{b}',\n        handler: (request) =\u003e {\n\n            const { a, b } = request.params;\n            const { mathService } = request.services();\n\n            return mathService.add(a, b);\n        }\n    });\n\n    await server.start();\n\n    console.log(`Start adding at ${server.info.uri}`);\n})();\n```\n\n### Using existing libraries as services\n\nIt's also possible to use existing libraries as services in your application.  Here's an example of how we might utilize [Nodemailer](https://nodemailer.com/) as a service for sending emails.  This example features [`Schmervice.withName()`](API.md#schmervicewithname), which is a convenient way to name your service using the [`Schmervice.name`](API.md#schmervicename) symbol, similarly to the example above:\n\n```js\nconst Schmervice = require('@hapipal/schmervice');\nconst Nodemailer = require('nodemailer');\nconst Hapi = require('@hapi/hapi');\n\n(async () =\u003e {\n\n    const server = Hapi.server();\n\n    await server.register(Schmervice);\n\n    server.registerService(\n        Schmervice.withName('emailService', () =\u003e {\n\n            // Sendmail is a simple transport to configure for testing, but if you're\n            // not seeing the sent emails then make sure to check your spam folder.\n\n            return Nodemailer.createTransport({\n                sendmail: true\n            });\n        })\n    );\n\n    server.route({\n        method: 'get',\n        path: '/email/{toAddress}/{message*}',\n        handler: async (request) =\u003e {\n\n            const { toAddress, message } = request.params;\n            const { emailService } = request.services();\n\n            await emailService.sendMail({\n                from: 'no-reply@yoursite.com',\n                to: toAddress,\n                subject: 'A message for you',\n                text: message\n            });\n\n            return { success: true };\n        }\n    });\n\n    await server.start();\n\n    console.log(`Start emailing at ${server.info.uri}`);\n})();\n```\n\n## Extras\n##### _What is a service layer?_\n\"Service layer\" is a very imprecise term because it is utilized in all sorts of different ways by various developers and applications.  Our goal here is not to be prescriptive about how you use services.  But speaking generally, one might write code in a \"service\" as a way to group related business logic, data transactions (e.g. saving records to a database), or calls to external APIs.  Sometimes the service layer denotes a \"headless\" interface to all the actions you can take in an application, independent of any transport (such as HTTP), and hiding the app's data layer (or model) from its consumers.\n\nIn our case services make up a general-purpose \"layer\" or part of your codebase– concretely, they're just classes that are instanced once per server.  You can use them however you see fit!\n\nhapi actually has a feature deeply related to this concept of services: [server methods](https://github.com/hapijs/hapi/blob/master/API.md#server.methods).  We love server methods, but also think they work better as a low-level API than being used directly in medium- and large-sized projects.  If you're already familiar with hapi server methods, you can think of schmervice as a tool to ergonomically create and use server methods (plus some other bells and whistles).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhapipal%2Fschmervice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhapipal%2Fschmervice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhapipal%2Fschmervice/lists"}