Skip to content

Commit

Permalink
Merge pull request #17 from samtgarson/new-format
Browse files Browse the repository at this point in the history
Introduce new format for specifying variables
  • Loading branch information
samtgarson authored Feb 15, 2019
2 parents 0afb120 + be62ced commit 372e284
Show file tree
Hide file tree
Showing 12 changed files with 13,988 additions and 6,466 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Nuxt currently provides a very [handy way of injecting environment variables](ht

This module allows you to read environment variables server side—at runtime—and inject them into your app, meaning your Nuxt bundle is decoupled from your environment variables.

⚠️ **WARNING**: As with the `config.env` option in Nuxt config, environment variables used in `nuxt-env` are exposed client side, so don't use it for storing secrets! ⚠️
⚠️ **WARNING**: As with the `config.env` option in Nuxt config, environment variables used in `nuxt-env` are exposed client side, so if you store secrets use the `secret` config option. Read more below. ⚠️

## Usage

Expand All @@ -33,11 +33,39 @@ yarn add nuxt-env
modules: [
'other-nuxt-module',
['nuxt-env', {
keys: ['TEST_VALUE']
keys: [
'TEST_ENV_VAR', // Basic usage—equivalent of { key: 'TEST_ENV_VAR' }
{ key: 'OTHER_ENV_VAR', default: 'defaultValue' } // Specify a default value
{ key: 'THIRD_ENV_VAR', secret: true } // Only inject the var server side
{ key: 'THIRD_ENV_VAR', name: 'MY_ENV_VAR' } // Rename the variable
]
}]
]
```

#### Options

Env vars can be injected in a basic way, just by specifying a string in the `keys` option.
When the provided var is an object, it can have the following attributes:

##### `key`
> required
The name of the environment variable by which it can be accessed in `process.env`

##### `default`

A default value for the env var in case it's not present in `process.env`.

##### `secret`
> default: `false`
When true, this key will only be present server side.

##### `name`

Change the name of the env var that gets injected. e.g.: `{ key: 'API_URL', name: 'API_ENDPOINT' }` will read `process.env.API_URL` and add it as `$env.API_ENDPOINT`


### Use in your application

Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const path = require('path')
const middleware = require('./lib/middleware')

const transform = keys => keys.map(key => typeof key === 'string'
? { name: key, key }
: { ...key, name: key.name || key.key }
)

module.exports = function NuxtEnv ({ keys }) {
this.addServerMiddleware(middleware(keys))
const transformedKeys = transform(keys)
this.addServerMiddleware(middleware(transformedKeys))

const src = path.resolve(__dirname, 'lib/plugin.js')
this.addPlugin({
Expand Down
18 changes: 13 additions & 5 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module.exports = (keys = []) => (req = {}, res, next = () => {}) => {
if (!req.env) req.env = {}
keys.forEach(key => req.env[key] = process.env[key])
next()
}
module.exports = (keys = []) =>
(req = {}, res, next = () => { }) => {
if (!req.env) req.env = {}
if (!req.clientEnv) req.clientEnv = {}

keys.forEach(k => {
const val = process.env[k.key] || k.default
req.env[k.name] = val
if (!k.secret) req.clientEnv[k.name] = val
})

next()
}
22 changes: 13 additions & 9 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const strategies = {
server (ctx, inject) {
const {
env: ctxEnv = {},
req: { env: reqEnv = {} } = {}
env: envFromNuxtConfig = {},
req: {
env: envForServer = {},
clientEnv: envForClient = {}
} = {}
} = ctx

const env = {
...ctxEnv,
...reqEnv
}

inject('env', env)
inject('env', {
...envFromNuxtConfig,
...envForServer
})

ctx.beforeNuxtRender(({ nuxtState }) => {
nuxtState.env = env
nuxtState.env = {
...envFromNuxtConfig,
...envForClient
}
})
},

Expand Down
Loading

0 comments on commit 372e284

Please sign in to comment.