tpipe
v0.0.8
Published
T piper for functions
Downloads
5
Readme
TPipe
TPipe is a simple, yet powerful, tool to easily organize and orchestrate your functions. It optimizes code reutilization and readability.
It allows you to chain input, output, error and finally mappings with you handler function in a framework/protocol agnostic way, so you can switch frameworks and formats by just writing new mappings.
All pipes have a collections of mappings and a handler, which is the core method.
By using just one rule for structure -> messages inside mappers and handlers are objects with this structure (names are configurable via options, but these are the defaults):
- a parameters object (meta information)
- a body object (information)
Execution pipeline
A T Pipe will accumulate an input via a promise-aware reduce function within its input mappers, the it will call the handler with it, and then it will execute the output/error mappings accumulating an output/error object respectively, and then the finally mappings.
Mappers
There are four kinds of mappings allowed in a tpipe. Input, output, error and finally. All mappings are functions that returns values or promises.
Input mappers
An input mapping receives the input message to accumulate and the other arguments sent by the caller.
// value example
function myInputMapping(input, req, res, next) {
return { parameters: { page: req.query.page }, body: { name: req.body.name } };
}
// promise example
function myInputMapping(input, req, res, next) {
return Promise.resolve(someResult);
}
Handler
This is your core method. It receives the input accumulated from the input mappings, and all the other arguments. A handler must return a message which will be the initial value for the output mappings or reject/throw, case in which the error mappings will be triggered.
// value example
function myHandler(input, req, res, next) {
return { parameters: { page: input.parameters.page }, body: { name: 'NewName', oldName: input.body.name } };
}
// promise example
function myHandler(input, req, res, next) {
return Promise.resolve(someResult);
}
Output mappers
An output mapping receives the output message to accumulate, the input, and the other arguments sent by the caller. This mappings are triggered after the handler finishes and the initial output will be its returned value / promise resolution value.
// value example
function myOutputMapping(output, input, req, res, next) {
return { parameters: { page: input.parameters.page }, body: { _name: output.body.name } };
}
// promise example
function myOutputMapping(output, input, req, res, next) {
return Promise.resolve(someResult);
}
Error mappers
An error mapping receives the error message to accumulate initialized with a message with the error object on the body section, the input, and the other arguments sent by the caller. It is triggered when some other mapping throws an error or reject the returned promise.
// value example
function myErrorMapping(error, input, req, res, next) {
return { parameters: {}, body: { newError: new Error('My error'), originalError: error.body } };
}
// promise example
function myErrorMapping(error, input, req, res, next) {
return Promise.resolve(someResult);
}
Finally mappers
A finally mapping receives the output/error message accumulated, the input, and the other arguments sent by the caller. It is triggered on both scenarios, after the output mappings but also after the error mappings.
// value example
function myFinallyMapping(outputOrErrorMessage, input, req, res, next) {
res.status(200).send(outputOrErrorMessage)
next()
}
// promise example
function myFinallyMapping(outputOrErrorMessage, input, req, res, next) {
return Promise.resolve(someResult);
}
Currently defaults to express (built in basic mappings). It supports error matching to values with regexes.
import piper from 'tpipe'
import expressPipeSet from 'tpipe-express'
export function renderView(output, input, req, res) {
if (output.parameters.view) {
res.render(output.parameters.view, output.body);
} else {
res.status(output.parameters.status || 200).send(output.body);
}
return output;
}
export function traceErrors(error) {
console.trace('Error: ', { error });
return error;
}
const customPipeSet = {
...expressPipeSet,
errorMappings: [traceErrors],
finallyMappings: [renderView]
};
// piper returns an object with a pipe inside to be injected in express
const { pipe } = piper(
input =>
(
{
parameters: {
view: 'index'
},
body: {
...input.body
}
}
)
)
.incorporate(customPipeSet) //put the mappings around the handler and prepare methods for express (getHandler)
this.app.get('/myRoute', pipe.getHandler());
Quality and Compatibility
Every build and release is automatically tested on the following platforms:
Installation
Copy and paste the following command into your terminal to install TPipe:
npm install tpipe --save
How to Contribute
You can submit your ideas through our issues system, or make the modifications yourself and submit them to us in the form of a GitHub pull request.
Running Tests
It's easy to run the test suite locally, and highly recommended if you're using tpipe on a platform we aren't automatically testing for.
npm test