syncano-middleware
v0.1.2
Published
Middleware provide a convenient mechanism for filtering HTTP requests entering your socket.
Downloads
4
Readme
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')
})
}