Skip to content

Commit

Permalink
Merge pull request #29 from joebartels/FEATURE-support-dotenv
Browse files Browse the repository at this point in the history
[FEATURE] support dotenv library [resolves #27]
  • Loading branch information
allevo authored May 13, 2018
2 parents c334371 + 6cb4efe commit be73dd6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ typings/

# dotenv environment variables file
.env

!test/.env
package-lock.json
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const optsSchema = {
],
default: {}
},
env: { type: 'boolean', default: true }
env: { type: 'boolean', default: true },
dotenv: { type: ['boolean', 'object'], default: false }
}
}
const optsSchemaValidator = ajv.compile(optsSchema)
Expand All @@ -38,6 +39,11 @@ function loadAndValidateEnvironment (fastify, opts, done) {
if (!Array.isArray(opts.data)) {
data = [data]
}

if (opts.dotenv) {
require('dotenv').config(Object.assign({}, opts.dotenv))
}

if (opts.env) {
data.push(process.env)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"homepage": "https://github.com/fastify/fastify-env#readme",
"dependencies": {
"ajv": "^6.0.0",
"dotenv": "5.0.1",
"fastify-plugin": "^1.0.0",
"xtend": "^4.0.1"
},
Expand Down
1 change: 1 addition & 0 deletions test/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VALUE_FROM_DOTENV=look ma
36 changes: 29 additions & 7 deletions test/fastify-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ const t = require('tap')
const Fastify = require('fastify')
const fastifyEnv = require('../index')

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

const fastify = Fastify()
fastify.register(fastifyEnv, {
confKey: 'config',
schema: schema,
data: data
})
fastify.register(fastifyEnv, options)
.ready(err => {
if (isOk) {
t.notOk(err)
Expand Down Expand Up @@ -183,6 +180,24 @@ const tests = [
VALUE_FROM_ENV: 'pippo'
}
},
{
name: 'simple object - ok - load only from .env',
schema: {
type: 'object',
required: [ 'VALUE_FROM_DOTENV' ],
properties: {
VALUE_FROM_DOTENV: {
type: 'string'
}
}
},
data: undefined,
isOk: true,
dotenv: { path: `${__dirname}/.env` },
confExpected: {
VALUE_FROM_DOTENV: 'look ma'
}
},
{
name: 'simple object - KO',
schema: {
Expand Down Expand Up @@ -217,6 +232,13 @@ const tests = [

tests.forEach(function (testConf) {
t.test(testConf.name, t => {
makeTest(t, testConf.schema, testConf.data, testConf.isOk, testConf.confExpected, testConf.errorMessage)
const options = {
schema: testConf.schema,
data: testConf.data,
dotenv: testConf.dotenv,
dotenvConfig: testConf.dotenvConfig
}

makeTest(t, options, testConf.isOk, testConf.confExpected, testConf.errorMessage)
})
})

0 comments on commit be73dd6

Please sign in to comment.