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!');
});
Add chain-functions to your package.json
file using npm
.
npm install chain-functions
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) => {...}
It takes the following parameters:
- the middleware functions; each middleware function should have the following structure:
(req, res, next) => next()
- 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.
Each middleware function can return a value or a Promise.
Express middleware are also supported, since they follow the same middleware function signature.
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.