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

umid

v4.0.0

Published

A generic and lightweight express like middleware with zero dependencies

Downloads

30

Readme

Universal Middleware

Fast, lightweight middleware framework.

Features

  • Lightweight - Badge size

  • Browser and Node - Use in browser and node

  • Async Await and Promise support - Support both async await and promise functions

  • No Dependency - No Bloating. No external dependencies

  • Express.js style middlware - Express.js like design

Version Badge size Badge size

yarn: yarn add umid

npm: npm i umid

cdn: https://unpkg.com/umid

module: https://unpkg.com/umid?module

Installation

To use this utility, simply import it into your project:

import { run } from "umid";

Usage

The run function accepts a mode and returns a function that accepts an array of functions to be executed asynchronously. The execution behavior depends on the mode specified.

Parameters

  • mode (optional): Specifies the execution mode. Default is 0.
  • ...fns: An array of functions to be executed.
  • ...args: Parameters to be passed to the functions.

Modes

  1. Mode 0: (Default) Without next
  2. Mode 1: With next

Examples

Mode 0: Without next

In this mode, the functions are called sequentially without a next callback.

const runWithoutNext = run(0);

const fn1 = async (param1, param2) => {
	console.log("fn1", param1, param2);
};

const fn2 = async (param1, param2) => {
	console.log("fn2", param1, param2);
	return "result2";
};

runWithoutNext(fn1, fn2)("hello", "world")
	.then((result) => console.log("Final result:", result))
	.catch((err) => console.error("Error:", err));

Mode 1: With next

In this mode, the functions are called sequentially with a next callback, which must be called to proceed to the next function.

const runWithNext = run(1);

const fn1 = async (param1, param2, next) => {
	console.log("fn1", param1, param2);
	next();
};

const fn2 = async (param1, param2, next) => {
	console.log("fn2", param1, param2);
	next();
	// next('not authorized'); // not authorized error
};

runWithNext(fn1, fn2)("hello", "world")
	.then(() => console.log("All functions executed"))
	.catch((err) => console.error("Error:", err));

Creating a express middleware utility

const expressMiddleware = run(1);

const fn1 = async (req) => {
	console.log('fn1');
};

const fn2 = async (req) => {
	console.log('fn2');
};

const expressMiddleware = (req, res, next) => {
	console.log('ex middleware', req, res, next);
	setTimeout(() => {
		next();
	}, 1000)
}
const fn3 = async (req) => "over";
const req = {};
const res = {};

run()(fn1, expressMiddleware(expressMiddleware), fn2, fn3)(req, res).then(console.log).catch(console.log);

API

run(mode) => (...fns) => async (...args) => Promise

Parameters:

  • mode: Number (optional) - Execution mode (0, 1). Default is 0.
  • ...fns: Array - Functions to be executed.
  • ...args: Array - Parameters to be passed to the functions.

Returns:

  • Promise: Resolves when all functions are executed or when a function returns a result (in mode 0).

License

This project is licensed under the MIT License. See the LICENSE file for details.