Skip to content

Commit

Permalink
Merge pull request #20 from dottorblaster/add-asyncawait-plugin-support
Browse files Browse the repository at this point in the history
Add async/await plugin support
  • Loading branch information
delvedor committed Oct 26, 2017
2 parents dfc9832 + b03216c commit 467672b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ You can easily run it with:
```bash
$ fastify plugin.js
```
You can use `async` functions too, and make your plugin more concise:
```js
// async-await-plugin.js
module.exports = async function (fastify, options) {
fastify.get('/', async function (req, reply) {
return { hello: 'world' }
})
}
```

CLI options:
```bash
Expand Down
8 changes: 7 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ function runFastify (opts) {
return module.exports.stop(e)
}

if (file.length !== 3) {
console.log(file.constructor.name)

if (file.length !== 3 && file.constructor.name === 'Function') {
return module.exports.stop(new Error('Plugin function should contain 3 arguments. Refer to ' +
'documentation for more information.'))
}
if (file.length !== 2 && file.constructor.name === 'AsyncFunction') {
return module.exports.stop(new Error('Async/Await plugin function should contain 2 arguments.' +
'Refer to documentation for more information.'))
}

const options = {
logger: {
Expand Down
7 changes: 7 additions & 0 deletions examples/async-await-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (fastify, options) {
fastify.get('/', async function (req, reply) {
return { hello: 'world' }
})
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"update-notifier": "^2.1.0"
},
"devDependencies": {
"fastify": "^0.28.2",
"fastify": "^0.30.2",
"pre-commit": "^1.2.2",
"request": "^2.81.0",
"standard": "^10.0.2",
Expand Down
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ test('should throw on parsing error', t => {
})
})

test('should start the server with an async/await plugin', t => {
if (Number(process.versions.node[0]) < 7) {
t.pass('Skip because Node version < 7')
return t.end()
}

t.plan(5)

const fastify = cli.start({
port: 3000,
_: ['./examples/async-await-plugin.js']
})

t.tearDown(() => fastify.close())

fastify.ready(err => {
t.error(err)

request({
method: 'GET',
uri: 'http://localhost:3000'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-length'], '' + body.length)
t.deepEqual(JSON.parse(body), { hello: 'world' })
})
})
})

test('should exit without error on help', t => {
t.plan(1)

Expand Down

0 comments on commit 467672b

Please sign in to comment.