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

io-middleware

v2.0.0

Published

Typed Express middleware for accumulating state across middleware

Downloads

17

Readme

io-middleware

Creates middleware that will accumulate state.

Example

import express from 'express'
import { ioMiddleware, FINISHED } from 'io-middleware'

express().get(
  '/user/:id',
  ioMiddleware(
    null, // initial value
    async (req, res) => fetchUser(req.params.id),
    async (req, res, user) => ({ name: user.name, email: user.email }),
    (req, res, state) => {
      res.json(state)
      return FINISHED // prevents any further middleware being called
    },
  ),
)

Typing state changes between middleware

One of the problems we face when writing reusable middleware is to make sure that particular state is available before they're used.

For example, lets say I have one piece of middleware that creates a "local":

function articleInfo() {
  return (req, res, next) => {
    res.locals.articleId = getArticleFromReferrer(req)
    if (!res.locals.articleId) next(new ServerError(404))
    else next()
  }
}

Then in another piece of middleware that wants use that "local":

function partitionKey() {
  return (req, res, next) => {
    res.locals.partitionKey = partitionLookUp(res.locals.articleId)
    if (!res.locals.partitionKey) next(new ServerError(404))
    else next()
  }
}

We can then join our middleware together:

express().get('/partition', article(), partitionKey(), (req, res) => {
  // handle response
})

If we were to create another route and forget to assign an articleId to our locals, before using the partitionKey() we'd have problem.

Strongly typing our middleware can help us ensure that the required state has been populated.

import { FINISH, ioMiddleware, IOMiddleware } from 'io-middleware'

function articleInfo<I>(): IOMiddleware<I, I & { articleId: string }> {
  return (req, res, state) => {
    const articleId = getArticleFromReferrer(req)
    if (!articleId) throw new ServerError(404)
    return { ...state, articleId }
  }
}

function partitionKey<I extends { articleId: string }>(): IOMiddleware<
  I,
  I & { partitionKey: string }
> {
  return (req, res, state) => {
    const partitionKey = partitionLookUp(state.articleId)
    if (!partitionKey) throw new ServerError(404)
    return { ...state, partitionKey }
  }
}

express().get(
  '/partition',
  ioMiddleware(
    {},
    articleInfo(), // If this were to be removed our project would not compile
    partitionKey(),
    (req, res, state) => {
      res.json(state)
      return FINISH
    },
  ),
)