Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

middy

Package Overview
Dependencies
Maintainers
7
Versions
147
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

middy - npm Package Compare versions

Comparing version 0.3.5 to 0.4.0

2

package.json
{
"name": "middy",
"version": "0.3.5",
"version": "0.4.0",
"description": "The simple (but cool 😎) middleware engine for AWS lambda in Node.js",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -252,2 +252,39 @@ <div align="center">

### Async/Await support
Middy allows you to write *async/await handlers*. In *async/await handlers* you don't
have to invoke the callback but just return the output (in case of success) or
throw an error (in case of failure).
We believe that this feature makes handling asynchronous logic easier to reason about
and asynchronous code easier to read.
Take the following code as an example:
```javascript
middy(async (event, context) => {
await someAsyncStuff()
await someOtherAsyncStuff()
return ({foo: bar})
})
```
this code is equivalent to:
```javascript
middy(async (event, context, callback) => {
someAsyncStuff()
.then(() => {
return someOtherAsyncStuff()
})
.then(() => {
callback(null, {foo: bar})
})
})
```
Of course, since AWS lambda runs on Node.js 6.10, you will need to transpile your `async/await` code (e.g. using [babel](https://babeljs.io/)).
### Async Middlewares

@@ -273,3 +310,3 @@

Thanks to this behaviour you can define middlewares using `async` functions:
Thanks to this behavior you can define middlewares using `async` functions:

@@ -276,0 +313,0 @@ ```javascript

@@ -298,3 +298,3 @@ const middy = require('../middy')

handler({}, {}, (err, response) => {
expect(err).toBe(null)
expect(err).toBeNull()
expect(onErrorMiddleware1).toBeCalled()

@@ -329,3 +329,3 @@ expect(onErrorMiddleware2).toBeCalled()

handler({}, {}, (err, response) => {
expect(err).toBe(null)
expect(err).toBeNull()
expect(onErrorMiddleware1).toBeCalled()

@@ -410,2 +410,59 @@ expect(onErrorMiddleware2).toBeCalled()

test('It should support handlers that return promises instead of using the callback', (endTest) => {
const handler = middy((event, context) => {
return Promise.resolve({some: 'response'})
})
handler({}, {}, (err, response) => {
expect(err).toBeNull()
expect(response).toEqual({some: 'response'})
endTest()
})
})
test('It should support async handlers', (endTest) => {
const handler = middy(async (event, context) => {
return {some: 'response'}
})
handler({}, {}, (err, response) => {
expect(err).toBeNull()
expect(response).toEqual({some: 'response'})
endTest()
})
})
test('A handler that returns a rejected promise will behave as an errored execution', (endTest) => {
const handler = middy((event, context) => {
return Promise.reject(new Error('bad stuff happened'))
})
handler({}, {}, (err, response) => {
expect(err.message).toEqual('bad stuff happened')
endTest()
})
})
test('An async handler that throws an error is threated as a failed execution', (endTest) => {
const handler = middy(async (event, context) => {
throw new Error('bad stuff happened')
})
handler({}, {}, (err, response) => {
expect(err.message).toEqual('bad stuff happened')
endTest()
})
})
test('A handler that returns a non-promise should trigger an error', (endTest) => {
const handler = middy((event, context) => {
return 'this is not a promise'
})
handler({}, {}, (err, response) => {
expect(err.message).toBe('Unexpected return value in handler')
endTest()
})
})
test('It should handle async middlewares', (endTest) => {

@@ -429,3 +486,3 @@ const asyncBefore = async (handler) => {

handler({}, {}, (err, response) => {
expect(err).toBe(null)
expect(err).toBeNull()
expect(handler.event.asyncBefore).toBeTruthy()

@@ -453,3 +510,3 @@ expect(handler.event.asyncAfter).toBeTruthy()

handler({}, {}, (err, response) => {
expect(err).toBe(null)
expect(err).toBeNull()
expect(response).toEqual({result: 'The error is handled'})

@@ -456,0 +513,0 @@ endTest()

@@ -157,6 +157,4 @@ const isPromise = require('./isPromise')

const errorHandler = err => {
if (err) {
instance.error = err
return runErrorMiddlewares(errorMiddlewares, instance, terminate)
}
instance.error = err
return runErrorMiddlewares(errorMiddlewares, instance, terminate)
}

@@ -167,7 +165,9 @@

handler.call(instance, instance.event, context, (err, response) => {
const onHandlerError = (err) => {
instance.response = null
return errorHandler(err)
}
const onHandlerSuccess = (response) => {
instance.response = response
if (err) return errorHandler(err)
runMiddlewares(afterMiddlewares, instance, (err) => {

@@ -178,3 +178,19 @@ if (err) return errorHandler(err)

})
}
const handlerReturnValue = handler.call(instance, instance.event, context, (err, response) => {
if (err) return onHandlerError(err)
onHandlerSuccess(response)
})
// support for async/await promise return in handler
if (handlerReturnValue) {
if (!isPromise(handlerReturnValue)) {
throw new Error('Unexpected return value in handler')
}
handlerReturnValue
.then(onHandlerSuccess)
.catch(onHandlerError)
}
})

@@ -181,0 +197,0 @@ }

import { Callback, Context, Handler, ProxyResult } from 'aws-lambda';
type AsyncHandler = (event: any, context: Context) => Promise<ProxyResult | object>;
declare var middy: {
(handler: Handler): middy.IMiddy;
(handler: Handler | AsyncHandler): middy.IMiddy;
};

@@ -8,0 +8,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc