@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 CHANGED
@@ -2,3 +2,4 @@
2
2
  .DS_Store
3
3
  yarn-error.log
4
4
  /.nyc_output
5
+ /skeleton/yarn.lock
package/README.md CHANGED
@@ -68,6 +68,11 @@ $ make logs
68
68
 
69
69
  ## Changelog
70
70
 
71
+ ### v0.3.0
72
+
73
+ - upgraded to fastify 3.x
74
+ - upgraded to node 12.x (LTS)
75
+
71
76
  ### v0.2.0
72
77
 
73
78
  - added instructions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uscreen.de/create-fastify-app",
3
- "version": "0.2.14",
3
+ "version": "0.3.0",
4
4
  "description": "cli to create a new @uscreen.de/fastify-app",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -1,12 +1,12 @@
1
1
  module.exports = {
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
- }
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
  }
@@ -4,7 +4,7 @@ const fastifyApp = require('@uscreen.de/fastify-app')
4
4
  const fp = require('fastify-plugin')
5
5
  const schemas = require('./schemas')
6
6
 
7
- module.exports = fp(async (fastify, opts, next) => {
7
+ module.exports = fp((fastify, opts, next) => {
8
8
  /**
9
9
  * add schemas
10
10
  */
@@ -6,21 +6,11 @@ const envSchema = require('env-schema')
6
6
  const schema = {
7
7
  type: 'object',
8
8
  properties: {
9
- httpPort: {
10
- default: 3000
11
- },
12
- httpBind: {
13
- default: '127.0.0.1'
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.healthCheck = {
33
+ config.health = {
44
34
  exposeStatusRoute: `${config.prefix}/health`
45
35
  }
46
36
 
@@ -3,7 +3,7 @@
3
3
  const fp = require('fastify-plugin')
4
4
 
5
5
  module.exports = fp(
6
- async (fastify, opts, next) => {
6
+ (fastify, opts, next) => {
7
7
  fastify.decorate('noop', () => {
8
8
  return 'Hello Universe'
9
9
  })
@@ -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(async (fastify, opts, next) => {
10
- fastify.addSchema({
11
- $id: 'http200',
12
- type: 'object',
13
- properties: {
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
- fastify.addSchema({
22
- $id: 'http404',
23
- type: 'object',
24
- properties: {
25
- statusCode: {
26
- type: 'integer',
27
- example: 404
28
- },
29
- error: {
30
- type: 'string',
31
- example: 'Not Found'
32
- },
33
- message: {
34
- type: 'string',
35
- example: 'Not Found'
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
  })
@@ -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 = async fastify => {
4
- fastify.get('/noop', async (req, res) => {
5
- return { noop: 'Hello world', plugin: fastify.noop() }
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
  }
@@ -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.4.3",
6
+ "@uscreen.de/fastify-app": "^0.5.0",
7
7
  "env-schema": "^1.0.0",
8
- "fastify": "^2.15.3",
9
- "fastify-plugin": "^1.6.0"
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('./helper')
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
- await fastify.ready()
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: '/api/health'
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
- await fastify.ready()
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: '/api/noop'
37
+ url: `${prefix}/noop`
38
38
  },
39
39
  (e, response) => {
40
40
  t.error(e)
@@ -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
- function build(t, ConfigOverwrite = {}) {
15
- const app = Fastify()
18
+ const build = async (t, featureSwitches = {}, ConfigOverwrite = {}) => {
19
+ return new Promise((resolve, reject) => {
20
+ const app = Fastify()
16
21
 
17
- // setup to register YOUR app
18
- app.register(fp(App), { ...Config, ...ConfigOverwrite })
22
+ // setup to register YOUR app
23
+ app.register(fp(App), { ...Config, ...ConfigOverwrite })
19
24
 
20
- // tear down our app after we are done
21
- t.tearDown(app.close.bind(app))
25
+ // tear down our app after we are done
26
+ t.tearDown(app.close.bind(app))
22
27
 
23
- return app
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
  }