Miss any of our Open RFC calls?Watch the recordings here! »

syncano-middleware

0.1.2 • Public • Published

XO code style CircleCI codecov

Syncano Middleware

Syncano middleware provide a convenient mechanism for filtering HTTP requests entering your socket. For example validate passed parameters, check if user is authenticated, assign default parameter values and many more.

For full list of middlewares check NPM.

Getting started

Installing from NPM

npm install syncano-middleware --save

Usage

import middleware from 'syncano-middleware';
 
// Example middlewares.
const steps = {
  auth: () => new Promise((resolve, reject) => {
    // Your user authentication logic...
    resolve(user)
  }),
  validate: () => new Promise((resolve, reject) => {
    // Your validation logic...
    resolve(formFields)
  })
}
 
middleware([ steps.auth, steps.validate ])
  .then(data => {
    // All middlewares passed.
    // Now you have an authenticated user and validated form.
 
    const [ user, fields ] = data
  })
  .const(err => {
    // An error occurred in one of middlewares.
    // You can handle that here.
  })
 
 

Custom middlewares

Middleware must be a function that returns a Promise:

  function MyAwesomeMiddleware() {
    return new Promise((resolve, reject) => {
      // Your awesome middleware implmentation...
      // ...
      // resolve(data)
      // or
      // reject('An error message or data')
    })
  }

Install

npm i syncano-middleware

DownloadsWeekly Downloads

0

Version

0.1.2

License

MIT

Last publish

Collaborators

  • avatar