Socket
Socket
Sign inDemoInstall

herbs2rest

Package Overview
Dependencies
2
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    herbs2rest

Create a REST API based on HerbsJS


Version published
Maintainers
2
Created

Readme

Source

Node.js CIcodecov

herbs2rest

Create a REST API based on herbs entities (gotu) and usecases (buchu).

Installing

$ npm install herbs2rest

Using

Use the method generateRoutes to generate api rest routes based on usecases.

herbs2rest works with express in version 4.x.

Controller List

The method needs a list of controllers like the example below:

const controllerList = [
  {
    name: 'lists',
    getAll: { usecase: require('../usecases/getLists'), controller: require('../controller') },
    getById: { usecase: require('../usecases/getLists'), id: 'listId' },
    post: { usecase: require('../usecases/createList') },
    put: { usecase: require('../usecases/updateList') },
    delete: { usecase: require('../usecases/deleteList') }
  }
]

The name field is the name of the route.

The id field is the param of the route.

The controller field is to replace the default controller.

The other fields refer to http methods using usecases (GetAll, GetById, Post, Put and Delete).

Custom Controller

To create a custom controller, it is necessary to follow this pattern.

const controller = async (usecase, req, user, res, next) => {
  // Implementation
}

Each method parameter has different data:

  • usecase: usecase in (buchu) pattern.
  • req: body, query and params of route.
  • user: parameter passed in the request.
  • res: response object of express.
  • next: allows the next queued route handler/middleware to handle the request.
Generate Routes

Generating and using new express routes:

const express = require('express')
const { generateRoutes } = require('herbs2rest')

const app = express()
const routes = new express.Router()

generateRoutes(controllerList, routes, true)  // true = console info endpoints

app.use(routes)
Authorization

All use cases must implement the authorization method and receive a user for authentication if using the default controller.

Example:

const { Ok, Err, usecase } = require('buchu')

const testUseCase = (injection) =>
  usecase('Test UseCase', {
    authorize: async (user) => {
      if (user === 'admin')
        return Ok()
      else
        return Err('Invalid user')
    }
  })

Example

Additionally you can view a simple demo application of this library in todolist-on-herbs.

How to contribute

If you would like to help contribute to this repository, please see CONTRIBUTING


License

Keywords

FAQs

Last updated on 08 Jun 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc