0.2.2 • Published 7 years ago

flux-capacitor-boot v0.2.2

Weekly downloads
13
License
-
Repository
github
Last release
7 years ago

Flux Capacitor - Bootstrapper

JavaScript Style Guide NPM Version

Bootstrapping utils for the Flux Capacitor. Setting up a flux capacitor HTTP service in a simple, yet customizable way by using feature middlewares.

Usage

This will set up a flux capacitor service with RESTful routes for reading data, dispatching events and pushing realtime updates using a websocket:

const authorize = require('flux-capacitor-boot/authorize')
const { connectTo } = require('flux-capacitor-sequelize')
const {
  bootstrap, createExpressApp, createDispatcher, createStore, createWebSocket, use
} = require('flux-capacitor-boot/express')

const commands = require('./commands')
const createCollections = require('./collections')
const rootReducer = require('./reducers')

bootstrap([
  use.app(createExpressApp()),
  use.store(createStore(rootReducer, connectTo(process.env.DB_CONNECTION, createCollections))),
  use.route('/dispatch/:commandName', createDispatcher(commands), (req) => authorize.allow()),
  use.route('/events(/:id)?', createReadRoute('events', { sortBy: 'timestamp', sortOrder: 'DESC' })),
  use.route('/notes(/:id)?', createReadRoute('notes')),
  use.route('/websocket', createWebSocket())
]).listen(process.env.PORT)

Customize

const { bootstrap, use } = require('flux-capacitor-boot/express')

bootstrap([
  ...

  // Optionally add custom express middlewares you need:
  use.expressMiddleware([ someMiddleware ]),

  // `use.*` just returns a function of this type:
  ({ app, store }) => Promise.resolve({ app, store })
])

If you want to use an existing express app or want to use some custom app then just pass it to use.app() instead of using createExpressApp():

const bodyParser = require('body-parser')
const express = require('express')
const { bootstrap, use } = require('flux-capacitor-boot/express')

const app = express()

bootstrap([
  use.app(app),
  use.expressMiddleware([ bodyParser.json() ]),   // the dispatcher needs this middleware, createExpressApp() would have set it up for us
  ...
])

Authorization

const authorize = require('flux-capacitor-boot/authorize')
const { bootstrap, createWebSocket, use } = require('flux-capacitor-boot/express')

bootstrap([
  use.route('/some/route', someHandler, authorizeRoute),
  use.route('/websocket', createWebSocket(authorizeEventPropagation))
])

// Must return authorize.allow() or authorize.deny(Error) or a promise.
function authorizeRoute (req) {
  if (!req.user) {
    return authorize.deny('You are not logged in or there is no express middleware for authentication set.')
  }

  if (req.user === 'John') {
    return authorize.allow()
  } else {
    // Can also pass an error object to `authorize.deny()`
    return authorize.deny('Only John is allowed to use this route.')
  }
}

function authorizeEventPropagation (event, websocket) {
  return authorize.allow()
}

Flux Capacitor

Find it here 👉 Flux Capacitor

License

Released under the terms of the MIT license.

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

8 years ago