{"id":20354354,"url":"https://github.com/hemerajs/hapi-hemera","last_synced_at":"2025-06-11T17:38:21.481Z","repository":{"id":57260913,"uuid":"75234008","full_name":"hemerajs/hapi-hemera","owner":"hemerajs","description":"Hapi plugin to integrate Hemera.","archived":false,"fork":false,"pushed_at":"2019-04-17T08:39:24.000Z","size":131,"stargazers_count":16,"open_issues_count":2,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T02:32:37.211Z","etag":null,"topics":["api-gateway","hapi","hemera","rest-microservice"],"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/hemerajs.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":"2016-11-30T22:47:59.000Z","updated_at":"2021-04-14T19:35:07.000Z","dependencies_parsed_at":"2022-09-08T10:51:55.101Z","dependency_job_id":null,"html_url":"https://github.com/hemerajs/hapi-hemera","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fhapi-hemera","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fhapi-hemera/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fhapi-hemera/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fhapi-hemera/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hemerajs","download_url":"https://codeload.github.com/hemerajs/hapi-hemera/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248506936,"owners_count":21115512,"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":["api-gateway","hapi","hemera","rest-microservice"],"created_at":"2024-11-14T23:08:19.972Z","updated_at":"2025-04-12T02:33:14.809Z","avatar_url":"https://github.com/hemerajs.png","language":"JavaScript","readme":"# hapi-hemera\n\n[![Build Status](https://travis-ci.org/hemerajs/hapi-hemera.svg?branch=master)](https://travis-ci.org/hemerajs/hapi-hemera)\n[![NPM Downloads](https://img.shields.io/npm/dt/hapi-hemera.svg?style=flat)](https://www.npmjs.com/package/hapi-hemera)\n[![npm](https://img.shields.io/npm/v/hapi-hemera.svg?maxAge=3600)](https://www.npmjs.com/package/hapi-hemera)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com)\n\n**hapi-hemera** is a [**Hemera**](https://github.com/hemerajs/hemera) micro-services plugin\nfor [Hapi](https://github.com/hapijs/hapi) **17+**. The plugin integrates the **Hemera** functionality into\n**hapi**.\n\n## Plugin Registration\n\n```js\nconst server = new Hapi.Server()\nawait server.register({\n  plugin: require('hapi-hemera'),\n  options: {\n    hemera: {\n      name: 'test',\n      logLevel: 'info'\n    },\n    nats: 'nats://localhost:4242',\n    // If you want to add hemera plugins\n    plugins: [require('hemera-joi')]\n  }\n})\n```\n\n## Plugin registration with a custom Hemera instance\n\n```js\nconst server = new Hapi.Server()\nconst hemeraInstance = new Hemera()\nawait server.register({\n  plugin: require('hapi-hemera'),\n  options: {\n    hemeraInstance: hemeraInstance,\n    nats: 'nats://localhost:4242'\n  }\n})\n```\n\n## Use toolkit decorator\n\n```js\nserver.route({\n  method: 'POST',\n  path: '/add',\n  handler: function (request, h) {\n    return h.hemera().act({ topic: 'math', cmd: 'add', a: 2, b: 2 })\n  }\n}\n```\n\n## Use server decorator\n\n```js\nserver.route({\n  method: 'POST',\n  path: '/add',\n  handler: async function (request, h) {\n    let resp = await server.hemera.act({ topic: 'math', cmd: 'add', a: 2, b: 2 })\n    // access result\n    resp.data\n    // retain parent context\n    resp = resp.context.act(...)\n  }\n}\n```\n\n## Use request decorator\n\n```js\nserver.route({\n  method: 'POST',\n  path: '/add',\n  handler: function (request, h) {\n    return request.hemera.act({ topic: 'math', cmd: 'add', a: 2, b: 2 })\n  }\n}\n```\n\n## Server methods\n\n```js\nserver.action('generate', {\n  topic: 'generator',\n  cmd: 'id'\n})\nconst result = await server.methods.generate()\n```\n\n## Use handler decorator and accept `params`, `query` and `payload` as pattern\n\n```js\nserver.route({\n  method: 'GET',\n  path: '/api/add',\n  handler: {\n    hemera: {\n      pattern: {\n        topic: 'math',\n        cmd: 'add'\n      }\n    }\n  }\n})\n```\n\n## Gracefully shutdown\n\nWe hook into Hapi `onPostStop` event to gracefully shutdown hemera.\n\n## Enrich pattern with contextual data\n\n```js\nserver.register({\n    plugin: HapiHemera,\n    options: {\n      basePattern: function (request) {\n        return {\n          trace$: {\n            traceId: request.headers['x-request-id']\n          }\n        }\n      },\n      nats: {\n        url: noAuthUrl\n      }\n    }\n})\n\n// The basePattern is merged with the pattern\nhemera.act({ a: 1 })\n\n// Results in following pattern\n { a: 1, trace$: { traceId: 123 } }\n```\n\n### Example for [zipkin-instrumentation-hapi](https://github.com/openzipkin/zipkin-js/tree/master/packages/zipkin-instrumentation-hapi)\n\n```js\nbasePattern: function(request) {\n  return {\n    trace$: {\n      traceId: request.plugins.zipkin.traceId.traceId,\n      spanId: request.plugins.zipkin.traceId.spanId,\n      sampled: request.plugins.zipkin.traceId.sampled,\n      flags: request.plugins.zipkin.traceId.flags\n    }\n  }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemerajs%2Fhapi-hemera","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhemerajs%2Fhapi-hemera","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemerajs%2Fhapi-hemera/lists"}