Chapter 11: update example microservice

This commit is contained in:
Beth Griggs 2020-11-15 01:11:41 +00:00
parent 791aac76d7
commit cb0e52e541
No known key found for this signature in database
GPG Key ID: D7062848A1AB005C
11 changed files with 146 additions and 9 deletions

View File

@ -56,7 +56,3 @@ profile-*
profile*
*clinic*
*flamegraph*
# generated code
*/**/*.js
dist

View File

@ -16,11 +16,11 @@
"license": "ISC",
"dependencies": {
"fastify": "^3.0.0",
"fastify-plugin": "^2.0.0",
"fastify-plugin": "^3.0.0",
"fastify-autoload": "^3.0.2",
"fastify-cli": "^2.0.2"
"fastify-cli": "^2.5.1"
},
"devDependencies": {
"tap": "^14.0.0"
}
}
}

View File

@ -11,6 +11,6 @@ that will then be used in the rest of your application.
Check out:
* [The hitchhiker's guide to plugins](https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md)
* [The hitchhiker's guide to plugins](https://www.fastify.io/docs/latest/Plugins-Guide/)
* [Fastify decorators](https://www.fastify.io/docs/latest/Decorators/).
* [Fastify lifecycle](https://www.fastify.io/docs/latest/Lifecycle/).

View File

@ -0,0 +1,12 @@
'use strict'
const fp = require('fastify-plugin')
// the use of fastify-plugin is required to be able
// to export the decorators to the outer scope
module.exports = fp(async function (fastify, opts) {
fastify.decorate('someSupport', function () {
return 'hugs'
})
})

View File

@ -23,4 +23,5 @@ If you need to share functionality between services, place that
functionality into the `plugins` folder, and share it via
[decorators](https://www.fastify.io/docs/latest/Decorators/).
If you have a bit confused about using `async/await` write services, you would better take a look at [Promise resolution](https://github.com/fastify/fastify/blob/master/docs/Routes.md#promise-resolution) for more details.
If you're a bit confused about using `async/await` to write services, you would
better take a look at [Promise resolution](https://www.fastify.io/docs/latest/Routes/#promise-resolution) for more details.

View File

@ -0,0 +1,7 @@
'use strict'
module.exports = async function (fastify, opts) {
fastify.get('/', async function (request, reply) {
return 'this is an example'
})
}

View File

@ -0,0 +1,7 @@
'use strict'
module.exports = async function (fastify, opts) {
fastify.get('/', async function (request, reply) {
return { root: true }
})
}

View File

@ -0,0 +1,34 @@
'use strict'
// This file contains code that we reuse
// between our tests.
const Fastify = require('fastify')
const fp = require('fastify-plugin')
const App = require('../app')
// Fill in this config with all the configurations
// needed for testing the application
function config () {
return {}
}
// automatically build and tear down our instance
function build (t) {
const app = Fastify()
// fastify-plugin ensures that all decorators
// are exposed for testing purposes, this is
// different from the production setup
app.register(fp(App), config())
// tear down our app after we are done
t.tearDown(app.close.bind(app))
return app
}
module.exports = {
config,
build
}

View File

@ -0,0 +1,26 @@
'use strict'
const { test } = require('tap')
const Fastify = require('fastify')
const Support = require('../../plugins/support')
test('support works standalone', async (t) => {
const fastify = Fastify()
fastify.register(Support)
await fastify.ready()
t.equal(fastify.someSupport(), 'hugs')
})
// You can also use plugin with opts in fastify v2
//
// test('support works standalone', (t) => {
// t.plan(2)
// const fastify = Fastify()
// fastify.register(Support)
//
// fastify.ready((err) => {
// t.error(err)
// t.equal(fastify.someSupport(), 'hugs')
// })
// })

View File

@ -0,0 +1,27 @@
'use strict'
const { test } = require('tap')
const { build } = require('../helper')
test('example is loaded', async (t) => {
const app = build(t)
const res = await app.inject({
url: '/example'
})
t.equal(res.payload, 'this is an example')
})
// inject callback style:
//
// test('example is loaded', (t) => {
// t.plan(2)
// const app = build(t)
//
// app.inject({
// url: '/example'
// }, (err, res) => {
// t.error(err)
// t.equal(res.payload, 'this is an example')
// })
// })

View File

@ -0,0 +1,27 @@
'use strict'
const { test } = require('tap')
const { build } = require('../helper')
test('default root route', async (t) => {
const app = build(t)
const res = await app.inject({
url: '/'
})
t.deepEqual(JSON.parse(res.payload), { root: true })
})
// inject callback style:
//
// test('default root route', (t) => {
// t.plan(2)
// const app = build(t)
//
// app.inject({
// url: '/'
// }, (err, res) => {
// t.error(err)
// t.deepEqual(JSON.parse(res.payload), { root: true })
// })
// })