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 { return parameters: page: reqquerypage body: name: reqbodyname ;}// promise example { return Promise;}
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 { return parameters: page: inputparameterspage body: name: 'NewName' oldName: inputbodyname ;}// promise example { return Promise;}
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 { return parameters: page: inputparameterspage body: _name: outputbodyname ;}// promise example { return Promise;}
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 { return parameters: {} body: newError: 'My error' originalError: errorbody ;}// promise example { return Promise;}
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 { resstatus200 }// promise example { return Promise;}
Currently defaults to express (built in basic mappings). It supports error matching to values with regexes.
{ if outputparametersview res; else resstatusoutputparametersstatus || 200; return output;} { console; return error;} const customPipeSet = ...expressPipeSet errorMappings: traceErrors finallyMappings: renderView; // piper returns an object with a pipe inside to be injected in expressconst pipe = //put the mappings around the handler and prepare methods for express (getHandler) thisapp;
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