2.0.1 • Published 6 years ago

@tailored-apps/koa-middleware v2.0.1

Weekly downloads
14
License
ISC
Repository
gitlab
Last release
6 years ago

@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)