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

express-valued-middleware

v0.6.3

Published

Composable value-yielding middleware creator and combinators for Express.

Downloads

14

Readme

Non-polluting express middleware

Motivation

An express middleware often adds extra properties to the request object. For example:

const middleware = (req, res, next) => {
  // ... does something and obtain a value somehow
  req["some_property"] = some_value;
  next();
}

By doing this, you assume that some_property is going to be available. If you're using TypeScript, you also need to use some TypeScript magic such as interface augmentation to make some_property available on the request object. But it may or may not be there.

express-valued-middleware encourages you not to do this, but instead offers a different pattern where you can use middleware.get(req) to extract the value from your middleware.

Usage

Creation

To do this, you use the of function to "lift" a middleware implementation by provide the "value extractor" function. If you're lifting a third-party middleware, you can, in a localized manner, access their magic property on the request (say, req.body or whatever). If you're writing your own middleware, you could avoid polluting the request object altogether by using things like WeakMaps or privately scoped Symbol keys.

import * as mw from "express-valued-middleware";

const middlewareState = new WeakMap()
const middleware = mw.of(
  (req, res, next) => {
    // ... does something and obtain a value somehow
    // store a value, for instance, you could use a weakmap
    middlewareState.set(req, some_value)
    next();
  },
  req => middlewareState.get(req)
)

This creates a "valued middleware".

To get a value of the middleware (this will throw if the middleware was not used):

// in some downstream handler
middleware.get(req) // throws if the middleware was never used.

TypeScript-wise, this will also be typed correctly based on the extractor function you provided.

Transforming values

You can also derive a valued middleware's value by using map to create a new valued middleware. The map function relies on fp-ts's Either interface as a convention for returning success and failure

import { Either, left, right } from "fp-ts/lib/Either"
import * as mw from "express-valued-middleware";

// assume "middleware"

const middlewareAsNumber = mw.map(
  value => right(Number(value))
)(middleware)

map returns a middleware combinator that you can use with fp-ts's pipe pipeline:

import { pipe } from "fp-ts/lib/function"
import { Either, left, right } from "fp-ts/lib/Either"
import * as mw from "express-valued-middleware";

// assume "middleware"

const middlewareAsNumber = pipe(
  middleware,
  mw.map(value => right(Number(value)))
)

map can also accept the following Left values:

// 1. Sending a status code
value => left(400) // Equivalent to req.sendStatus(400)

// 2. Sending a status code and a response
value => left([400, `${value} is not valid`]) // Equivalent to req.status(400).send(...)

// 3. Custom error behavior
value => left((req, next) => {
  req.status(400).send(`{value is not valid}`)
  next()
})

Aggregating values

Multiple middlewares may also be aggregated using aggregate():

import * as mw from "express-valued-middleware";

// assume "middleware1" and "middleware2"
const aggregatedMiddleware = mw
  .aggregate(middleware1, middleware2)
  .all((result1, result2) => ({ result1, result2 }));

aggregate() creates an intermediate and with all() you specify how the values of the aggregated middleware should be combined. The resulting valued middleware runs the provided middlewares simultaneously. If any of the provided middleware throws with next(error), that error is propagated and the merge function is not called.

Future work

This package was created without incorporating other useful functional programming ideas such as sequencing and traversing. A future (likely breaking) change may include aligning the API of this package more closely with the conventions of fp-ts.