diff --git a/index.js b/index.js index 7acb83d..b622405 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/test/fastify-env.test.js b/test/fastify-env.test.js index cc9b88f..45644e3 100644 --- a/test/fastify-env.test.js +++ b/test/fastify-env.test.js @@ -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) @@ -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 }) + }) +})