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

flow-compose

v1.0.16

Published

All purpose composer

Downloads

1,397

Readme

flow-compose

NPM version License Downloads TypeScript

Flow control for any asynchronous function utilising a onion-like compose middleware returning a fully valid middleware comprised of all those which are passed.

The flow acts in a stack-like manner, allowing consumer to perform actions downstream then after actions on the response upstream.

This is based on popular koajs/koa-compose rewritten in Typescript using reduce with

  • ability to specify own context type and
  • optional passing of a value from previous middleware into the next as a parameter. ( so that it can be used as a I/O pipeline )
type Middleware<T = any> = (context: T, next: NextFunction, valueFromPrev?: any) => Promise<any>;
type NextFunction = (valueFromPrev?: any) => Promise<any>;

koa-compose pattern is very powerful and in fact it can be used beyond Koa framework. ( the namespace, the context API )

Ultimately this gives the consumer the ability to modularise different steps in a process and control the flow.

Installation

npm install flow-compose

# or using yarn
yarn add flow-compose

Usage

Middleware example:
import { compose, Middleware } from 'flow-compose';

type MyContext = {
    logger: { log: (...args: any[]) => void };
    service: { get: () => Promise<string> };
    eventSender: { send: (data: string) => void };
};

const handleError: Middleware<MyContext> = async (context, next) => {
    try {
        return await next();
    } catch (err) {
        // handle error
        return null;
    }
};

const logRunningTime: Middleware<MyContext> = async (context, next) => {
    const start = Date.now();
    const text = await next();
    const end = Date.now();

    context.logger.log('Total time:', end - start);

    return text;
};

const fireEvent: Middleware<MyContext> = async (context, next, valueFromPrev) => {
    context.eventSender.send(valueFromPrev);
    return next(valueFromPrev);
};

const transform: Middleware<MyContext> = async (context, next, valueFromPrev) => valueFromPrev.toUpperCase();

const fetch: Middleware<MyContext> = async (context, next) => {
    const rawData = await context.service.get();
    return next(rawData);
};

const context: MyContext = {
    logger: { log: console.log },
    service: { get: async () => 'data' },
    eventSender: { send: (data) => {} }
};

const result = await compose<MyContext>([handleError, logRunningTime, fetch, fireEvent, transform])(context);
Basic input/output pipeline example:
import { compose, Middleware, parallel } from 'flow-compose';

type MyContext = {
    person: string
}

const ateCandies: Middleware<MyContext> = async (context, next, valueFromPrev) => {
    return next(context.person + ' ate ' + valueFromPrev.join(','));
};

const drankOrangeJuice: Middleware<MyContext> = async (context, next, valueFromPrev) => {
    return next(valueFromPrev + ' and drank orange juice');
};

const chocolate: Middleware<MyContext> = async () => 'chocolate';
const jellyBean: Middleware<MyContext> = async () => 'jelly bean';
const getCandies: Middleware<MyContext> = parallel([chocolate, jellyBean]);

const result = await compose<MyContext>([getCandies, ateCandies, drankOrangeJuice])({ person: 'Tom' });