Skip to content

Latest commit

 

History

History
69 lines (53 loc) · 1.84 KB

README.md

File metadata and controls

69 lines (53 loc) · 1.84 KB

chain-functions

npm version

A lightweight builder for Google Cloud Functions with middleware support.

const { simpleFunction } = require('chain-functions');
const cors = require('cors');

exports.helloWorld = simpleFunction(cors(), (req, res) => {
  res.send('Hello World!');
});

Installation

Add chain-functions to your package.json file using npm.

npm install chain-functions

Usage

simpleFunction is useful for http related google cloud functions & firebase functions.

simpleFunction returns a function that can be used as a Cloud Function.

(req, res) => {...}

Parameters

It takes the following parameters:

  1. the middleware functions; each middleware function should have the following structure:
    (req, res, next) => next()
  2. the function to be executed as parameters.
    (req, res) => res.send('Hello World!')

All the functions are executed in chaining mode; as a result each middleware function should call the next() function.

Promise Support

Each middleware function can return a value or a Promise.

Use Express middlewares

Express middleware are also supported, since they follow the same middleware function signature.

Example

const { simpleFunction } = require('chain-functions');
const cors = require('cors');

function auth(req, res, next) {
  if (!authorizationHeader) {
    res.status(401).send({ error: 'Unauthorized' });
    return;
  }
  return next();
}

function helloWorld(req, res) {
  res.send('Hello World!');
}

exports.helloWorld = simpleFunction(cors(), auth, helloWorld);

More examples can be found in examples folder.