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

@tailored-apps/koa-middleware

v2.0.1

Published

A small library providing various utility functions used in tailored apps backends (propably not very useful for anyone else)

Downloads

48

Readme

@tailored-apps/koa-middleware

js-standard-style

@tailored-apps/koa-middleware contains some helper-functions and is part of the api-skeleton-2.

Installation and Updating

npm install @tailored-apps/koa-middleware

Functions

cacheLifetimeHelper

Provides a "setCacheLifetime" function on ctx.state

function cacheLifetimeHelper (logger, { defaultExpiresHeader = false, defaultLifetime = 0, expiresHeader = 'Expires', cacheControlHeader = 'Cache-control' } = { })

This function can be used by downstream middleware to easily set cache lifetime for the current request. Does not support any advanced caching configuration on purpose, set headers manually if you need anything more than a simple "Cache-control" or "Expires" header.

API:

setCacheLifetime(lifetime[, useExpiresHeader = false]) => void

Example:

app.use(cacheLifetime(logger))
app.use(async (ctx, next) => {
  // do stuff here
  
  ctx.state.setCacheLifetime(60) // -> response will include "Cache-control: max-age=60" header
  // or
  // ctx.state.setCacheLifetime(60, true) // -> response will contain "Expires: Mon, 17 Apr 2017 01:44:58 GMT" header
  
  await next()
})

errorHandler

Returns an error handler middleware function for use in koa 2.x

function errorHandler (logger, getResponse = ({ message, error, body }) => body || error || message, getStatus = (err) => err.status || err.statusCode || 500)

This function requires a logger instance being passed to it - response body and response status code will be determined via the provided getResponse and getStatus functions

Example:

app.use(getErrorHandlerMiddleware(myLoggerInstance, (err, ctx) => `${ctx.status} ERROR: ${err.message}`), (err) => err.status)

requestDigester

Returns a request digester middleware function for use in koa 2.x

function requestDigester (logger, methodPropName = 'digestMethod')

The basic idea here is for the router to do nothing but call a method that assigns a route specific function to the request state (ctx.state.digestMethod = someAsyncFunc) - this method will subsequently be called by this middleware, with the koa context object passed in. The function is expected to return an object, which will be used as the response body. If the function doesn't return anything, a 204 No Content response will automatically be created.

Example:

async function respond ({ request: { body: { someBodyProp } } }) {
  return {
    someResponseProp: `Request body contained ${someBodyProp}.`
  }
}

async function deleteSomething ({ params: { id } }) {
  await deleteThisIdFromDatabase(id)
}

const router = new KoaRouter()
 
router.post('/foo', async function setFooDigester (ctx, next) {
  ctx.state.digestMethod = respond
 
  await next()
})
 
router.delete('/bar/:id', async function setBarDigester (ctx, next) {
  ctx.state.digestMethod = deleteThings
 
  await next()
})

POST /foo generates 200 OK with a response body containing:

{ 
  someResponseProp: "Request body contained some request body value"
}

DELETE /bar/420 will delete the item "420" and return a 204 No Content response.

requestProfiler

Returns a request profiler middleware function for use in koa 2.x

function requestProfiler (logger)