Skip to content

Commit

Permalink
Fix a bug where no default config name set by default (#56)
Browse files Browse the repository at this point in the history
* Fix a bug where no default config name set by default

* Avoid using async/await (Node 6 support)
  • Loading branch information
lavir authored and mcollina committed Jul 2, 2019
1 parent a972cb7 commit 2b551c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const envSchema = require('env-schema')
function loadAndValidateEnvironment (fastify, opts, done) {
try {
const config = envSchema(opts)
const confKey = opts.confKey
const confKey = opts.confKey || 'config'
fastify.decorate(confKey, config)
done()
} catch (err) {
Expand Down
24 changes: 23 additions & 1 deletion test/fastify-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const fastifyEnv = require('../index')

function makeTest (t, options, isOk, confExpected, errorMessage) {
t.plan(isOk ? 2 : 1)
options = Object.assign({ confKey: 'config' }, options)

const fastify = Fastify()
fastify.register(fastifyEnv, options)
Expand Down Expand Up @@ -259,3 +258,26 @@ tests.forEach(function (testConf) {
makeTest(t, options, testConf.isOk, testConf.confExpected, testConf.errorMessage)
})
})

t.test('should use custom config key name', t => {
t.plan(1)
const schema = {
type: 'object',
required: [ 'PORT' ],
properties: {
PORT: {
type: 'integer',
default: 6666
}
}
}

const fastify = Fastify()
fastify.register(fastifyEnv, {
schema: schema,
confKey: 'customConfigKeyName'
})
.ready(() => {
t.strictSame(fastify.customConfigKeyName, { PORT: 6666 })
})
})

0 comments on commit 2b551c2

Please sign in to comment.