Skip to content

Influential's Functional, Promise-based Express Middleware

License

Notifications You must be signed in to change notification settings

jxbadam/jigawatt

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status codecov Code Climate

jigawatt

Influential's Functional, Promise-based Express Middleware

Installation

npm install jigawatt

Middleware Structure

Properties

Jigawatt Middleware must consist of at least one of three properties:

  • awesomize: (Validator -> AwesomizeSpec) -> Request -> Object a

  • io: Request, Data -> Object a

    • returns an object that is merged into req.data
  • transform: Request, Data -> Object a

    • returns an object to become the new req.data value

Example Middleware

const _         = require('ramda')
const Bluebird  = require('bluebird')

const Order = require('../domain/order.js')

const getById = {
  awesomize: (v) => ({
    order_id: {
      read: _.path(['params', 'orderId'])
    , validation: [ v.required ]
    }
  })

  io: (req, data) => {
    return Bluebird.props({
      order : Order.getById(data.order_id)
    })
  }
};

const uniteDetails = {
  transform: (req, data) => {
    return {
      order     : data.order
    , customer  : data.customer
    , product   : data.product
    , shipping  : data.shipping  
    }
  }
}

module.exports = {
  getById
, uniteDetails
}

Example Usage

const router     = require('express').Router()
const JW         = require('jigawatt')

// MIDDLEWARE
const Order      = require('../middleware/order.js')
const Customer   = require('../middleware/customer.js')
const Product    = require('../middleware/product.js')
const Shipping   = require('../middleware/shipping.js')


// ROUTES
router.get('order/:orderId', JW(Order.getById));

router.get('order/:orderId/detail', JW(

  [ Order.getById             // call all of these promise
  , Customer.getByOrderId     // functions at the same time
  , Product.getByOrderId      // and merge request.data when
  , Shipping.getByOrderId     // all are complete
  ]

, Order.uniteDetails          // transform-only middleware
                              // to aggregate details and
                              // present to user
))

About

Influential's Functional, Promise-based Express Middleware

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 98.8%
  • Makefile 1.2%