{"id":19738305,"url":"https://github.com/dani8art/rest-hapi-gen","last_synced_at":"2026-05-12T08:35:07.801Z","repository":{"id":36073788,"uuid":"221076203","full_name":"dani8art/rest-hapi-gen","owner":"dani8art","description":"A @hapijs/hapi plugin to generate a RESTful API from a given model.","archived":false,"fork":false,"pushed_at":"2023-08-30T22:38:18.000Z","size":1955,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-10T19:29:18.942Z","etag":null,"topics":["api","hapi","hapi-plugin","joi","rest","rest-api"],"latest_commit_sha":null,"homepage":null,"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/dani8art.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":"2019-11-11T21:47:49.000Z","updated_at":"2022-11-18T16:59:31.000Z","dependencies_parsed_at":"2023-01-16T12:50:24.251Z","dependency_job_id":null,"html_url":"https://github.com/dani8art/rest-hapi-gen","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dani8art%2Frest-hapi-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dani8art%2Frest-hapi-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dani8art%2Frest-hapi-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dani8art%2Frest-hapi-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dani8art","download_url":"https://codeload.github.com/dani8art/rest-hapi-gen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241067818,"owners_count":19903899,"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","hapi","hapi-plugin","joi","rest","rest-api"],"created_at":"2024-11-12T01:13:51.771Z","updated_at":"2026-05-12T08:35:02.775Z","avatar_url":"https://github.com/dani8art.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RESTHapi Gen\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) [![npm version](https://img.shields.io/npm/v/rest-hapi-gen.svg?style=flat)](https://www.npmjs.com/package/rest-hapi-gen) [![CircleCI](https://circleci.com/gh/dani8art/rest-hapi-gen.svg?style=svg)](https://circleci.com/gh/dani8art/rest-hapi-gen) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)]() [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) [![jest](https://facebook.github.io/jest/img/jest-badge.svg)](https://github.com/facebook/jest)\n\n\u003e RESTHapi Gen is a [@hapijs/hapi](https://github.com/hapijs/hapi) plugin which generates a CRUD RESTful API from a given [joi](https://github.com/hapijs/joi) model.\n\n### Compatibility\n\n| RESTHapi Gen version | Hapi JS version       | Joi Version        |\n| -------------------- | --------------------- | ------------------ |\n| `2.x.x` and `3.x.x`  | `20.x.x` and `21.x.x` | `joi^17.x.x`       |\n| `1.x.x`              | `19.x.x`              | `@hapi/joi^17.x.x` |\n| `0.x.x`              | `18.x.x`              | `@hapi/joi^15.x.x` |\n\n## TL;DR;\n\n```\n$ npm i @hapi/hapi joi mongoose rest-hapi-gen\n```\n\n```javascript\nconst Hapi = require('@hapi/hapi');\nconst Joi = require('joi');\nconst Mongoose = require('mongoose');\n\nconst RestHapiGen = require('rest-hapi-gen');\n\n(async () =\u003e {\n  await Mongoose.connect('mongodb://localhost:27017/testdb', { useNewUrlParser: true, useUnifiedTopology: true });\n\n  const server = Hapi.server({ port: 4000 });\n\n  const petsCollectionConf = {\n    collection: { name: 'pets' },\n    schema: Joi.object({\n      name: Joi.string().required(),\n      tags: Joi.array().items(Joi.string()).default([]),\n    }),\n  };\n\n  await server.register([{ plugin: RestHapiGen, options: petsCollectionConf }]);\n  await server.start();\n\n  console.log('Server running on %s', server.info.uri);\n})();\n```\n\n## Plugin configuration\n\n| Option                            | Type       | Description                                                                                                                        |\n| --------------------------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------- |\n| auth.enabled                      | `boolean`  | `Optional` Whether to activate auth or not. Default: `false`                                                                       |\n| auth.client.id                    | `string`   | `Required` OAuth 2.0 client id. It is required if `$.auth.enabled === true`                                                        |\n| auth.client.secret                | `string`   | `Required` OAuth 2.0 client secret. It is required if `$.auth.enabled === true`                                                    |\n| auth.client.kind                  | `string`   | `Optional` OAuth 2.0 server kind. Default: `keycloak`                                                                              |\n| auth.scope.read                   | `string[]` | `Optional` OAuth 2.0 required scope list to access read actions. Default `false`                                                   |\n| auth.scope.write                  | `string[]` | `Optional` OAuth 2.0 required scope list to access write actions. Default `false`                                                  |\n| auth.server.url                   | `string`   | `Required` OAuth 2.0 server. It is required if `$.auth.enabled === true`                                                           |\n| auth.server.realm                 | `string`   | `Optional` OAuth 2.0 server realm. Default: `master`                                                                               |\n| auth.session.cookie.name          | `string`   | `Optional` Name for the session cookie. Default: `rest-hapi-gen-session`                                                           |\n| auth.session.enabled              | `boolean`  | `Optional` Whether to enable session or just bearer only auth mechanism. Default: `true`                                           |\n| auth.session.password             | `string`   | `Optional` The session encryption password. Default: Random generated                                                              |\n| basePath                          | `string`   | `Optional` Base path where the CRUD endpoints are attached. Default: `'/'`                                                         |\n| collection.name                   | `string`   | `Required` Name for the collection that is going to be created.                                                                    |\n| collection.pages.limit            | `number`   | `Optional` Default max limit for collection queries, it will be overrided if the API user uses `collection?limit=x`. Default: `10` |\n| health.enabled                    | `boolean`  | `Optional` Whether to enable a health endpoint. Default: `true`                                                                    |\n| health.path                       | `string`   | `Optional` Target path where the health endpoint will be set, it must start with `/`. Default: `/_healthz`                         |\n| overrides.actions.GET_COLLECTION  | `Function` | `Optional` Async function that will override the default handler for GET_COLLECTION action                                         |\n| overrides.actions.GET_RESOURCE    | `Function` | `Optional` Async function that will override the default handler for GET_RESOURCE action                                           |\n| overrides.actions.CREATE_RESOURCE | `Function` | `Optional` Async function that will override the default handler for CREATE_RESOURCE action                                        |\n| overrides.actions.UPDATE_RESOURCE | `Function` | `Optional` Async function that will override the default handler for UPDATE_RESOURCE action                                        |\n| overrides.actions.DELETE_RESOURCE | `Function` | `Optional` Async function that will override the default handler for DELETE_RESOURCE action                                        |\n| rootPathRedirect                  | `boolean`  | `Optional` Whether redirect from root path (`/`) to `basePath` path. Default: `false`                                              |\n| schema                            | `Joi`      | `Required` Joi schema for the collection that is created.                                                                          |\n| tls                               | `boolean`  | `Optional` Whether the server is using TLS externally/internally or not. Default: `false`                                          |\n\n### Override an action\n\nIf an action needs to be overrided, you must provide an `async function` that will be executed instead of the default one. This function will receive three args: `request` that will be a @hapijs/hapi [request object](https://hapi.dev/api/?v=20.0.2#request), `h` that will be a @hapijs/hapi [request toolkit](https://hapi.dev/api/?v=20.0.2#response-toolkit) and a `model` that will be a [mongoose model](https://mongoosejs.com/docs/models.html) which is based on the given schema.\n\n\u003e NOTE: Your custom function must return an object that must to be valid against the **Joi** schema otherwise the server will return an internal server error.\n\n```js\n...\nconst { ActionType } = RestHapiGen;\n...\n  const petsCollectionConf = {\n    collection: { name: 'pets' },\n    schema: Joi.object({\n      name: Joi.string().required(),\n      tags: Joi.array().items(Joi.string()).default([])\n    }),\n    // Override actions\n    overrides: {\n      actions: {\n        // Override GET collection action\n        [ActionType.GET_COLLECTION]: async (request, h, model) =\u003e {\n          return await model.find();\n        },\n      },\n    },\n  };\n...\n```\n\nIn addition, you could configure Hapi server adding `debug.request` so you can see schema validation errors, to do so you must apply the following configuration to your Hapi server\n\n```js\n...\nconst server = Hapi.server({\n  port: 4000,\n  debug: {\n    request: ['*'],\n  },\n});\n...\n```\n\n### Configure authentication\n\nCurrently, RESTHapi Gen only support `keycloak` as authentication provider, a generated resource can be protected using OAuth 2.0 and keycloak server, see the following example.\n\n```javascript\nconst petsCollectionConf = {\n  collection: { name: 'pets' },\n  schema: Joi.object({\n    name: Joi.string().required(),\n    tags: Joi.array().items(Joi.string()).default([]),\n  }),\n  auth: {\n    enabled: true,\n    server: { url: 'https://auth.example.io', realm: 'pets' },\n    client: { id: 'example-client', secret: 'example-client-secret' },\n    scope: { read: ['pets:ro'], write: ['pets:rw'] },\n  },\n};\n```\n\n## Deploy MongoDB\n\n**Docker**\n\n```shell\n$ docker run -d --rm --name mymongo -p 27017:27017 mongo\n```\n\n**Docker Compose**\n\n```yaml\nversion: '3.7'\n\nservices:\n  mongo:\n    image: mongo\n    ports:\n      - 27017:27017\n```\n\n```shell\n$ curl -sSL https://raw.githubusercontent.com/dani8art/rest-hapi-gen/master/docker-compose.yaml \u003e docker-compose.yaml\n$ docker-compose up -d\n```\n\n## License\n\nRESTHapi Gen is [MIT licensed](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdani8art%2Frest-hapi-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdani8art%2Frest-hapi-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdani8art%2Frest-hapi-gen/lists"}