Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for async/await #51

Merged
merged 6 commits into from
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
["env", {
"targets": {
"node": "6.10"
}
}]
]
}
49 changes: 45 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
<a href="https://standardjs.com/">
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
</a>
<a href="https://gitter.im/middyjs">
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
</a>
<a href="https://greenkeeper.io/">
<img src="https://badges.greenkeeper.io/middyjs/middy.svg" alt="Greenkeeper badge" style="max-width:100%;">
</a>
<a href="https://gitter.im/middyjs">
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
</a>
</p>
</div>


## TOC

- [A little appetizer](#a-little-appetizer)
- [Install](#install)
- [Requirements](#requirements)
Expand Down Expand Up @@ -247,6 +249,45 @@ to the user.
If no middleware manages the error, the lambda execution fails reporting the unmanaged error.


### Async Middlewares

Middy supports middlewares that return promises instead that directly calling the callback:

```javascript
const asyncValidator = () => {
before: (handler) => {
if (handler.event.body) {
return new Promise((resolve, reject) => {
// async validation logic
})
}

return Promise.resolve()
}
}

handler.use(asyncValidator())
```

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

```javascript
const asyncValidator = () => {
before: async (handler) => {
if (handler.event.body) {
return await asyncValidate(handler.event.body)
}

return
}
}

handler.use(asyncValidator())
```

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/)).


## Writing a middleware

A middleware is an object that should contain at least 1 of 3 possible keys:
Expand Down Expand Up @@ -440,7 +481,7 @@ Middy factory function. Use it to wrap your existing handler to enable middlewar
| before | [<code>middlewareAttachFunction</code>](#middlewareAttachFunction) | attach a new *before-only* middleware |
| after | [<code>middlewareAttachFunction</code>](#middlewareAttachFunction) | attach a new *after-only* middleware |
| onError | [<code>middlewareAttachFunction</code>](#middlewareAttachFunction) | attach a new *error-handler-only* middleware |
| __middlewares | <code>Object</code> | contains the list of all the attached middlewares organised by type (`before`, `after`, `onError`). To be used only for testing and debugging purposes |
| __middlewares | <code>Object</code> | contains the list of all the attached middlewares organised by type (`before`, `after`, `onError`). To be used only for testing and debugging purposes |

<a name="useFunction"></a>

Expand Down
42 changes: 42 additions & 0 deletions README.md.hb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<a href="https://standardjs.com/">
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
</a>
<a href="https://greenkeeper.io/">
<img src="https://badges.greenkeeper.io/middyjs/middy.svg" alt="Greenkeeper badge" style="max-width:100%;">
</a>
<a href="https://gitter.im/middyjs">
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
</a>
Expand Down Expand Up @@ -246,6 +249,45 @@ to the user.
If no middleware manages the error, the lambda execution fails reporting the unmanaged error.


### Async Middlewares

Middy supports middlewares that return promises instead that directly calling the callback:

```javascript
const asyncValidator = () => {
before: (handler) => {
if (handler.event.body) {
return new Promise((resolve, reject) => {
// async validation logic
})
}

return Promise.resolve()
}
}

handler.use(asyncValidator())
```

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

```javascript
const asyncValidator = () => {
before: async (handler) => {
if (handler.event.body) {
return await asyncValidate(handler.event.body)
}

return
}
}

handler.use(asyncValidator())
```

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/)).


## Writing a middleware

A middleware is an object that should contain at least 1 of 3 possible keys:
Expand Down
Loading