npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tpipe

v0.0.8

Published

T piper for functions

Downloads

5

Readme

TPipe npm version license type npm downloads ECMAScript 6 & 5 js-standard-style

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

Build Status Coverage Status bitHound Score Dependency Status Dev Dependency Status

Every build and release is automatically tested on the following platforms:

node 5.x node 6.x

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