A library to create pipelines with contexts and strong type checking.

Overview

TypePipe

A library to create pipelines with contexts and strong type checking.

Installation

With Node.js and npm installed in your computer run:

npm install typepipe

Documentation

Documentation autogenerated with TypeDoc can be found at https://afresquet.github.io/typepipe/.

Usage

JavaScript:

const { Pipeline } = require("typepipe");

const pipeline = new Pipeline()
	.pipe((value, context, global) => global.effect(value, context.foo))
	.pipe((value, context, global) => value.toString())
	.pipe((value, context, global) => string.padStart(2, "0"));

pipeline.run(1, { foo: 2 }, { effect: (a, b) => a + b }); // "03"

const fn = pipeline.compose();

fn(5, { foo: 4 }, { effect: (a, b) => a - b }); // "01"

TypeScript:

import { Pipeline } from "typepipe";

interface Context {
	foo: number;
}

interface Global {
	effect: (a: number, b: number) => number;
}

const pipeline = new Pipeline<number, Context, Global>()
	.pipe((value, { foo }, { effect }) => effect(value, foo))
	.pipe((value, context, global) => value.toString())
	.pipe((value, context, global) => string.padStart(2, "0")); // TypeScript knows `value` is a string now

pipeline.run(1, { foo: 2 }, { effect: (a, b) => a + b }); // "03"
pipeline.run("1", { foo: 2 }, { effect: (a, b) => a + b }); // Type Error: value should be a number
pipeline.run(1, "context", { effect: (a, b) => a + b }); // Type Error: context should be type Context

const fn = pipeline.compose();

fn(5, { foo: 4 }, { effect: (a, b) => a - b }); // "01"
fn("5", { foo: 4 }, { effect: (a, b) => a + b }); // Type Error: value should be a number
fn(5, { foo: 4 }, "global"); // Type Error: global should be type Global

Context and Global

As you can see in the examples above, the functions have two other parameters: context and global.

You can use them for anything you want really, but the idea is that context is something (often an object) containing relevant values for the current execution, and global has constant values that are shared between all executions.

For example, you could use this on an Express.js server (or any other server library/framework), and have the context be the current req and res objects, and global could be database models and/or libraries.

This makes unit testing really easy, as you can mock both those contexts very easily.

Another example could be a chatbot, where you have context containing data about the new message, and global containing the data about the whole conversation.

This is useful in anything that gets executed multiple times with different inputs, and has to be processed the same way.

Type Checking

This library was created with type checking as the first priority.

It works best with TypeScript, but you can still use it in JavaScript with JSDoc (click to see how).

You should only need to fill in the generics of Pipeline when instanciating it. Then everything will get inferred automatically, step by step, no matter how many funtions you pipe in.

But if you want to extract the functions you pass to your pipelines (and you should in order to unit test them), you can give them a type TypePipe.Function and pass your generics to the type like this:

import { TypePipe } from "typepipe";
import { MyContext, MyGlobal } from "../types";

export const concatenateContexts: TypePipe.Function<
	string,
	string,
	MyContext,
	MyGlobal
> = (value, context, global) => {
	return value + context + global;
};

As you can imagine, this can become very repetitive as you use the library more and more. Not only you would need to pass your Context and Global generics to each TypePipe.Function, but you would also need to pass them to each Pipeline you create.

And what happens if at some point you want to change the generics? It would be better if we could have a centralized place where to control all of this.

Luckily this is very simple to solve. For Pipeline it would be as easy as extending from it and passing the generics to the extends declaration. And for TypePipe.Function it's not any different, we can make an interface that extends from it, again passing the generics to the extends declaration. We can even keep the input value generic and give it a default type.

import { Pipeline, TypePipe } from "typepipe";
import { MyContext, MyGlobal } from "../types";

export default class MyPipeline<Input = string> extends Pipeline<
	Input,
	MyContext,
	MyGlobal
> {}

export interface MyFunction<Input = string, Output = string>
	extends TypePipe.Function<Input, Output, MyContext, MyGlobal> {}

That's it! Now we can use MyPipeline and MyFunction and we can forget about passing the MyContext and MyGlobal generics to each Pipeline and TypePipe.Function we create.

import MyPipeline, { MyFunction } from "./MyPipeline";

const concatenateContexts: MyFunction = (value, context, global) => {
	return value + context + global;
};

const pipeline = new MyPipeline().pipe(concatenateContexts);

const numberToString: MyFunction<number, string> = (value, context, global) => {
	return value.toString();
};

const pipelineNumber = new MyPipeline<number>().pipe(numberToString);

Examples

You can find some examples in the examples

Contribute

  1. Fork the repository and clone it to your computer.
  2. Create a branch: git checkout -b my-new-feature
  3. Install dependencies: npm install
  4. Commit your changes: git commit -am "Add some feature"
  5. Push to the branch: git push origin my-new-feature
  6. Open a pull request :D
You might also like...

Continuous Deployment Pipelines (CI/CD) with Dozer

Continuous deployment pipelines are sets of scripts and tools helping you get your project from your workstation, or version control system, to somewhere your target audience can reach it.

Jan 2, 2023

Fix for Object.hasOwnProperty, which normally just returns a boolean, which is not good when you care about strong typing.

Fix for Object.hasOwnProperty, which normally just returns a boolean, which is not good when you care about strong typing.

Welcome to ts-has-own-property 👋 Fix for Object.hasOwnProperty, which normally just returns a boolean, which is not good when you care about strong t

Jul 4, 2022

Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Welcome to ts-object-keys 👋 Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Jul 4, 2022

🔥 A Powerful JavaScript Module for Generating and Checking Discord Nitro 🌹

🔥 A Powerful JavaScript Module for Generating and Checking Discord Nitro 🌹

DANG: Dreamy's Awesome Nitro Generator Join Our Discord Getting Started Before, We start please follow these Steps: Required* ⭐ Give a Star to this dr

Jan 5, 2023

ToDo list app is a simple web app that helps you organize your day, by adding, checking and deleting daily tasks

ToDo list app is a simple web app that helps you organize your day, by adding, checking and deleting daily tasks

TODO List App "To-do list" is a WebApp tool that helps to organize your day. It simply lists the tasks that you need to do and allows you to mark them

Oct 18, 2022

Babel-plugin-amd-checker - Module format checking plugin for Babel usable in both Node.js the web browser environments.

babel-plugin-amd-checker A Babel plugin to check the format of your modules when compiling your code using Babel. This plugin allows you to abort the

Jan 6, 2022

This package is for developers to be able to easily integrate bad word checking into their projects.\r This package can return bad words in array or regular expression (regex) form.

Vietnamese Bad Words This package is for developers to be able to easily integrate bad word checking into their projects. This package can return bad

Nov 3, 2022

The ultimate parity checking as-a-service platform.

Astro Starter Kit: Minimal npm create astro@latest -- --template minimal 🧑‍🚀 Seasoned astronaut? Delete this file. Have fun! 🚀 Project Structure I

Nov 28, 2022

Snipes Test Flight apps. Configurable & has the ability to use a burner account for checking the status to avoid bans.

Snipes Test Flight apps. Configurable & has the ability to use a burner account for checking the status to avoid bans.

TestFlight Sniper Snipes TestFlight beta apps. Configurable & has the ability to use a burner account for checking the status to avoid bans. Features

Dec 20, 2022
Comments
  • 1.0.1

    1.0.1

    • Added examples
      • Express.js
      • Fastify
      • tmi.js
      • Discord.js
      • JavaScript
    • Fixed Pipeline.catch() and Pipeline.context() not being handled correctly if they were a Promise
    • Fixed Pipeline.context() not persisting the context for more than one call after it.
    • Bumped TypeScript's target to ES2017.
    bug documentation 
    opened by afresquet 0
  • New context doesn't persist

    New context doesn't persist

    When we use the .context method of Pipeline, only the next immediate piped function receives the new context, afterwards it goes back to the original one.

    bug 
    opened by afresquet 0
  • ChangeContext and ErrorHandler not being handled when wrapped in a Promise

    ChangeContext and ErrorHandler not being handled when wrapped in a Promise

    If we use .context or .catch on a Pipeline after piping some asynchronous function, the following function we pipe will receive a Promise with ChangeContext or ErrorHandler wrapped.

    bug 
    opened by afresquet 0
  • Pipeline.context() method

    Pipeline.context() method

    • Added a context method to Pipeline to change the Context of the current execution
    • Removed Pipeline and Match class declarations from TypePipe namespace
    opened by afresquet 0
Releases(v1.0.1)
  • v1.0.1(May 18, 2022)

    • Added examples
      • Express.js
      • Fastify
      • tmi.js
      • Discord.js
      • JavaScript
    • Fixed Pipeline.catch() and Pipeline.context() not being handled correctly if they were a Promise.
    • Fixed Pipeline.context() not persisting the context for more than one call after it.
    • Bumped TypeScript's target to ES2017.
    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(May 18, 2022)

A tiny JavaScript library to easily toggle the state of any HTML element in any contexts, and create UI components in no time.

A tiny JavaScript library to easily toggle the state of any HTML element in any contexts, and create UI components in no time. Dropdown, navigation bu

Matthieu Bué 277 Nov 25, 2022
A type speed checking website which lets you check your typing speed and shows the real-tme leaderboards with mongodb as DB and express as backend

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Sreehari jayaraj 8 Mar 27, 2022
Simple, lightweight at-runtime type checking functions, with full TypeScript support

pheno Simple, lightweight at-runtime type checking functions, with full TypeScript support Features Full TypeScript integration: TypeScript understand

Lily Scott 127 Sep 5, 2022
Runtime type checking in pure javascript.

Install npm install function-schema Usage import { signature } from 'function-schema'; const myFunction = signature(...ParamTypeChecks)(ReturnValueCh

Jeysson Guevara 3 May 30, 2022
Password Generator - A fast, simple and powerful open-source utility tool for generating strong, unique and random passwords

A fast, simple and powerful open-source utility tool for generating strong, unique and random passwords. Password Generator is free to use as a secure password generator on any computer, phone, or tablet.

Sebastien Rousseau 11 Aug 3, 2022
An inheritable and strong logic template front-end mvvm framework.

Intact 文档 Documents 简介 Intact作为一个可继承,并且拥有强逻辑模板的前端MVVM框架,有着如下特色: 充分利用组合与继承的思想,来最高限度地复用代码 同时支持数据驱动和组件实例化调用,来最便捷地实现功能 强逻辑模板,赋予模板更多功能和职责,来完成业务逻辑和表现逻辑分离 安装

Javey 55 Oct 21, 2022
SEE-EYE is a collection of useful Github actions and workflows used to build CI pipelines for TypeScript applications

SEA-EYE ?? No frils collection of common actions and pre-made workflows for TypeScript project that uses yarn@v1 as package manager. Workflows Build -

Tino Thamjarat 10 Jun 6, 2022
Write Dockerfiles and CI pipelines in TypeScript.

Trellis Write Dockerfiles and CI pipelines in TypeScript. Trellis is a portable CI/CD tool. With Trellis, you can define your Dockerfiles and CI/CD pi

Charlie Marsh 12 Nov 3, 2022
Remix TypeScript monorepo with Turborepo pipelines, Prisma, PostgreSQL, Docker deploy to Fly.io, pnpm, TailwindCSS and Tsyringe for DI.

Remix template with Turborepo, TypeScript and pnpm. The remix app deploys to fly.io or build to Docker image. Example packages for Database with prisma, Tsyringe dependency injection, UI, and internal TypeScript packages.

Philippe L'ATTENTION 33 Dec 29, 2022
jQuery plugin to encourage strong user passwords

Naked Password¶ ↑ Simple jQuery plugin to improve security on passwords. Usage¶ ↑ Naked password is extremely easy to use. All thats needed is for you

Platform45 307 Nov 3, 2022