Influential's Functional, Promise-based Express Middleware
npm install jigawatt
Jigawatt Middleware must consist of at least one of three properties:
-
awesomize
:(Validator -> AwesomizeSpec) -> Request -> Object a
- validates/sanitizes the request to initialize the
data
object - for more info on
awesomize
, see https://www.npmjs.com/package/awesomize
- validates/sanitizes the request to initialize the
-
io
:Request, Data -> Object a
- returns an object that is merged into
req.data
- returns an object that is merged into
-
transform
:Request, Data -> Object a
- returns an object to become the new
req.data
value
- returns an object to become the new
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
}
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
))