promise-filter-chain

1.0.2 • Public • Published

promise-filter-chain

Summary

A generic filter-chain with promises. Each filter returns a truthy value to continue the chain. Each filter can read and manipulate an object passed by reference as the first argument.

Usage

npm install --save promise-filter-chain
const FilterChain = require('promise-filter-chain');

// filter to log http events
const eventLogger = {
  process: event => {
    const { request } = event;
    console.log(`${request.httpMethod} ${request.path}`);
    return Promise.resolve(true);
  }
};

// filter to authenticate http events
const authenticator = {
  process: event => {
    if (event.request.headers.Cookie['user-id'] !== 'authenticated user') {
      event.response.statusCode = 403;
      return Promise.resolve(false);
    } else {
      return Promise.resolve(true);
    }
  }
};

// filter chain for all requests
const filterChain = new FilterChain([
  eventLogger,
  authenticator
]);

// handler for a some API endpoint
const controller = {
  process: event => {
    event.response.statusCode = 200;
    // retrieve some data from business logic
    // respond to http request:
    event.response.body = {
      name: 'Corey',
      awesome: true
    };
  }
}

// process an http event
return filterChain.wrapInChain(incomingHttpEvent, controller);

Package Sidebar

Install

npm i promise-filter-chain

Weekly Downloads

5

Version

1.0.2

License

MIT

Unpacked Size

9.21 kB

Total Files

5

Last publish

Collaborators

  • coreyferguson