@uscreen.de/create-fastify-app 0.2.14 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +1 -0
- package/README.md +5 -0
- package/package.json +1 -1
- package/skeleton/.eslintrc.js +10 -10
- package/skeleton/app/app.js +1 -1
- package/skeleton/app/config.js +6 -16
- package/skeleton/app/plugins/noop.js +1 -1
- package/skeleton/app/schemas.js +24 -29
- package/skeleton/app/server.js +13 -2
- package/skeleton/app/services/noop.js +21 -4
- package/skeleton/package.json +5 -3
- package/skeleton/test/{noop.test.js → app/noop.test.js} +12 -12
- package/skeleton/test/helper.js +20 -8
package/.gitignore
CHANGED
package/README.md
CHANGED
package/package.json
CHANGED
package/skeleton/.eslintrc.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
root: true,
|
|
3
|
+
env: {
|
|
4
|
+
node: true
|
|
5
|
+
},
|
|
6
|
+
extends: ['standard', 'plugin:prettier/recommended'],
|
|
7
|
+
rules: {
|
|
8
|
+
'generator-star-spacing': 'off',
|
|
9
|
+
'space-before-function-paren': 'off',
|
|
10
|
+
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
|
|
11
|
+
}
|
|
12
12
|
}
|
package/skeleton/app/app.js
CHANGED
package/skeleton/app/config.js
CHANGED
|
@@ -6,21 +6,11 @@ const envSchema = require('env-schema')
|
|
|
6
6
|
const schema = {
|
|
7
7
|
type: 'object',
|
|
8
8
|
properties: {
|
|
9
|
-
httpPort: {
|
|
10
|
-
|
|
11
|
-
},
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
},
|
|
15
|
-
prefix: {
|
|
16
|
-
default: '/api'
|
|
17
|
-
},
|
|
18
|
-
logEnabled: {
|
|
19
|
-
default: true
|
|
20
|
-
},
|
|
21
|
-
logLevel: {
|
|
22
|
-
default: 'info'
|
|
23
|
-
}
|
|
9
|
+
httpPort: { default: 3000 },
|
|
10
|
+
httpBind: { default: '127.0.0.1' },
|
|
11
|
+
prefix: { default: '/api' },
|
|
12
|
+
logEnabled: { default: true },
|
|
13
|
+
logLevel: { default: 'info' }
|
|
24
14
|
}
|
|
25
15
|
}
|
|
26
16
|
|
|
@@ -40,7 +30,7 @@ config.swagger = {
|
|
|
40
30
|
addModels: true
|
|
41
31
|
}
|
|
42
32
|
|
|
43
|
-
config.
|
|
33
|
+
config.health = {
|
|
44
34
|
exposeStatusRoute: `${config.prefix}/health`
|
|
45
35
|
}
|
|
46
36
|
|
package/skeleton/app/schemas.js
CHANGED
|
@@ -1,41 +1,36 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const fp = require('fastify-plugin')
|
|
4
|
+
const S = require('fluent-schema')
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Usage of the Globaly Shared Schema feature
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
|
-
module.exports = fp(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
statusCode: {
|
|
15
|
-
type: 'integer',
|
|
16
|
-
example: 200
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
})
|
|
10
|
+
module.exports = fp((fastify, opts, next) => {
|
|
11
|
+
const addSchema = (schema) => {
|
|
12
|
+
fastify.addSchema(schema)
|
|
13
|
+
return schema
|
|
14
|
+
}
|
|
20
15
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
16
|
+
/**
|
|
17
|
+
* add generic schemas
|
|
18
|
+
*/
|
|
19
|
+
const noop = addSchema(S.object().id('noop').prop('noop'))
|
|
20
|
+
|
|
21
|
+
const plugin = addSchema(S.object().id('plugin').prop('plugin'))
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* combine and extend schemas
|
|
25
|
+
*/
|
|
26
|
+
addSchema(
|
|
27
|
+
S.object()
|
|
28
|
+
.id('noopNplugin')
|
|
29
|
+
.required(['noop'])
|
|
30
|
+
// .prop('property') // --> strip it
|
|
31
|
+
.extend(noop)
|
|
32
|
+
.extend(plugin)
|
|
33
|
+
)
|
|
39
34
|
|
|
40
35
|
next()
|
|
41
36
|
})
|
package/skeleton/app/server.js
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const fastify = require('fastify')
|
|
4
|
+
const hyperid = require('hyperid')
|
|
4
5
|
const { name, version } = require('../package.json')
|
|
5
6
|
const config = require('./config')
|
|
6
7
|
const app = require('./app')
|
|
7
8
|
|
|
9
|
+
const instance = hyperid({ urlSafe: true })
|
|
10
|
+
|
|
8
11
|
const server = fastify({
|
|
12
|
+
genReqId() {
|
|
13
|
+
return instance()
|
|
14
|
+
},
|
|
15
|
+
|
|
9
16
|
logger: config.logEnabled
|
|
10
17
|
? {
|
|
11
18
|
level: config.logLevel,
|
|
12
|
-
name: `${name} (v${version}) ${process.env.NODE_APP_INSTANCE}
|
|
19
|
+
name: `${name} (v${version}) ${process.env.NODE_APP_INSTANCE}`,
|
|
20
|
+
redact: {
|
|
21
|
+
paths: ['pattern'], // limit hemeras verbosity
|
|
22
|
+
remove: true
|
|
23
|
+
}
|
|
13
24
|
}
|
|
14
25
|
: false
|
|
15
26
|
})
|
|
@@ -19,7 +30,7 @@ server.register(app, config)
|
|
|
19
30
|
/**
|
|
20
31
|
* post-treatment
|
|
21
32
|
*/
|
|
22
|
-
server.ready(err => {
|
|
33
|
+
server.ready((err) => {
|
|
23
34
|
if (err) throw err
|
|
24
35
|
server.log.debug('server ready, routes are set:\n' + server.printRoutes())
|
|
25
36
|
})
|
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
module.exports =
|
|
4
|
-
fastify.get(
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
module.exports = (fastify, opts, next) => {
|
|
4
|
+
fastify.get(
|
|
5
|
+
'/noop',
|
|
6
|
+
{
|
|
7
|
+
schema: {
|
|
8
|
+
response: {
|
|
9
|
+
200: { $ref: 'noopNplugin#' } // fastest and json-schema spec compatiple way, or use 'fastify.getSchema()'
|
|
10
|
+
// 200: fastify.getSchema('noopNplugin')
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
async (req, res) => {
|
|
15
|
+
return {
|
|
16
|
+
noop: 'Hello world',
|
|
17
|
+
plugin: fastify.noop(),
|
|
18
|
+
property: 'should be stripped from response'
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
next()
|
|
7
24
|
}
|
package/skeleton/package.json
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
"version": "0.0.0",
|
|
4
4
|
"main": "app/server.js",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@uscreen.de/fastify-app": "^0.
|
|
6
|
+
"@uscreen.de/fastify-app": "^0.5.0",
|
|
7
7
|
"env-schema": "^1.0.0",
|
|
8
|
-
"fastify": "^2.
|
|
9
|
-
"fastify-plugin": "^
|
|
8
|
+
"fastify": "^3.2.0",
|
|
9
|
+
"fastify-plugin": "^2.2.0",
|
|
10
|
+
"fluent-schema": "^1.0.4",
|
|
11
|
+
"hyperid": "^2.0.5"
|
|
10
12
|
},
|
|
11
13
|
"devDependencies": {
|
|
12
14
|
"eslint": "^7.6.0",
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
const tap = require('tap')
|
|
2
|
-
const { build } = require('
|
|
2
|
+
const { build } = require('../helper')
|
|
3
3
|
|
|
4
|
-
tap.test('Test Setup', t => {
|
|
4
|
+
tap.test('Test Setup', (t) => {
|
|
5
5
|
t.strictEqual(true, true, 'Tests and assertions should work')
|
|
6
6
|
t.end()
|
|
7
7
|
})
|
|
8
8
|
|
|
9
|
-
tap.test('Healthcheck', async t => {
|
|
10
|
-
const fastify = build(t)
|
|
11
|
-
|
|
9
|
+
tap.test('Healthcheck', async (t) => {
|
|
10
|
+
const fastify = await build(t)
|
|
11
|
+
const { prefix } = fastify.config
|
|
12
12
|
|
|
13
|
-
t.test('a valid GET Request', t => {
|
|
13
|
+
t.test('a valid GET Request', (t) => {
|
|
14
14
|
fastify.inject(
|
|
15
15
|
{
|
|
16
16
|
method: 'GET',
|
|
17
|
-
url:
|
|
17
|
+
url: `${prefix}/health`
|
|
18
18
|
},
|
|
19
19
|
(e, response) => {
|
|
20
20
|
t.error(e)
|
|
@@ -26,15 +26,15 @@ tap.test('Healthcheck', async t => {
|
|
|
26
26
|
})
|
|
27
27
|
})
|
|
28
28
|
|
|
29
|
-
tap.test('Noop Service', async t => {
|
|
30
|
-
const fastify = build(t)
|
|
31
|
-
|
|
29
|
+
tap.test('Noop Service', async (t) => {
|
|
30
|
+
const fastify = await build(t)
|
|
31
|
+
const { prefix } = fastify.config
|
|
32
32
|
|
|
33
|
-
t.test('a valid GET Request', t => {
|
|
33
|
+
t.test('a valid GET Request', (t) => {
|
|
34
34
|
fastify.inject(
|
|
35
35
|
{
|
|
36
36
|
method: 'GET',
|
|
37
|
-
url:
|
|
37
|
+
url: `${prefix}/noop`
|
|
38
38
|
},
|
|
39
39
|
(e, response) => {
|
|
40
40
|
t.error(e)
|
package/skeleton/test/helper.js
CHANGED
|
@@ -10,19 +10,31 @@ const fp = require('fastify-plugin')
|
|
|
10
10
|
const App = require('../app/app')
|
|
11
11
|
const Config = require('../app/config')
|
|
12
12
|
|
|
13
|
+
// overwrite some config option(s) on tests
|
|
14
|
+
Config.hemeralogLevel = 'fatal'
|
|
15
|
+
Config.hemeraNS = `test-${process.env.TAP_CHILD_ID}`
|
|
16
|
+
|
|
13
17
|
// automatically build and tear down our instance
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
const build = async (t, featureSwitches = {}, ConfigOverwrite = {}) => {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const app = Fastify()
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
// setup to register YOUR app
|
|
23
|
+
app.register(fp(App), { ...Config, ...ConfigOverwrite })
|
|
19
24
|
|
|
20
|
-
|
|
21
|
-
|
|
25
|
+
// tear down our app after we are done
|
|
26
|
+
t.tearDown(app.close.bind(app))
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
app.ready((err) => {
|
|
29
|
+
if (err) throw err
|
|
30
|
+
resolve(app)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
24
33
|
}
|
|
25
34
|
|
|
35
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
36
|
+
|
|
26
37
|
module.exports = {
|
|
27
|
-
build
|
|
38
|
+
build,
|
|
39
|
+
wait
|
|
28
40
|
}
|