Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Releases: Vin65/npm-middlewares

1.0.1

07 Mar 21:22
Compare
Choose a tag to compare

Updates npm-middlewares to be compiled using webpack into a main dist/index.js file which takes care of other projects using webpack to bundle their projects.

1.0.0

07 Mar 18:58
Compare
Choose a tag to compare

Installation

  • npm install --save npm-middlewares

Middlewares documentation

Available middlewares

httpErrorHandler

Automatically handles uncaught errors that are created with
http-errors and creates a proper HTTP response
for them (using the message and the status code provided by the error object).

Supports ajv validation errors by
looking for errors with the error message: Event object failed validation.

Will call the function ajv.errorsText passing in the error.details.

It should be set as the last error handler.

Sample usage

const middy = require('middy')
const { httpErrorHandler } = require('npm-middlewares')

const handler = middy((event, context, cb) => {
  throw new createError.UnprocessableEntity()
})

handler
  .use(httpErrorHandler())

// when Lambda runs the handler...
handler({}, {}, (_, response) => {
  expect(response).toEqual({
    statusCode: 422,
    body: '{"errorMessage":"Unprocessable Entity"}'
  })
})

httpResponseHandler

Automatically handles returned objects from the callback or returned when using an async function and creates a proper HTTP response
for them (using the object as the response body and a 200 status code).

Sample usage

const middy = require('middy')
const { httpResponseHandler } = require('npm-middlewares')

// When using an async/await syntax

const handler = middy(async(event, context) => {
  return {
    foo: "bar"
  }
})

// Or when using a non async function, use the callback

const handler = middy((event, context, callback) => {
  return callback(null, {
    foo: "bar"
  });
})

handler
  .use(httpResponseHandler())

// when Lambda runs the handler...
handler({}, {}, (_, response) => {
  expect(response).toEqual({
    statusCode: 200,
    body: '{"foo":"bar"}'
  })
})