nuxt-envalid
Advanced tools
Comparing version 0.0.3 to 0.0.5
@@ -8,3 +8,3 @@ 'use strict'; | ||
var name = "nuxt-envalid"; | ||
var version = "0.0.3"; | ||
var version = "0.0.5"; | ||
@@ -11,0 +11,0 @@ const CONFIG_KEY = "envalid"; |
{ | ||
"name": "nuxt-envalid", | ||
"version": "0.0.3", | ||
"version": "0.0.5", | ||
"description": "A Nuxt.js module thats validates your env variables and loads them cleaned into your application context", | ||
@@ -10,2 +10,3 @@ "keywords": [ | ||
], | ||
"homepage": "https://nuxt-envalid.henkebyte.com", | ||
"bugs": { | ||
@@ -22,3 +23,3 @@ "url": "https://github.com/manuelhenke/nuxt-envalid/issues" | ||
"email": "contact@henkebyte.com", | ||
"url": "https://henkebyte.com/" | ||
"url": "https://henkebyte.com" | ||
}, | ||
@@ -34,7 +35,8 @@ "main": "./dist/module.js", | ||
"lint": "yarn lint:js && yarn lint:prettier", | ||
"lint:js": "eslint --ext \".js,.ts\" --ignore-path .gitignore .", | ||
"lint:prettier": "prettier --check --ignore-path .gitignore .", | ||
"lintfix": "yarn lint:js --fix && prettier --write --list-different --ignore-path .gitignore . ", | ||
"lint:js": "eslint --ext \".js,.ts\" .", | ||
"lint:prettier": "prettier --check .", | ||
"lintfix": "yarn lint:js --fix && prettier --write --list-different .", | ||
"prepare": "husky install", | ||
"prepublishOnly": "yarn build", | ||
"serve-docs": "cd docs/ && bundle exec jekyll serve", | ||
"test": "jest --coverage" | ||
@@ -55,3 +57,3 @@ }, | ||
"@nuxtjs/eslint-config-typescript": "^10.0.0", | ||
"@types/jest": "^28.1.7", | ||
"@types/jest": "^28.1.8", | ||
"@types/node": "^16.11.56", | ||
@@ -67,3 +69,3 @@ "eslint": "^8.22.0", | ||
"siroc": "^0.16.0", | ||
"typescript": "^4.7.4" | ||
"typescript": "^4.8.2" | ||
}, | ||
@@ -70,0 +72,0 @@ "publishConfig": { |
160
README.md
@@ -0,1 +1,5 @@ | ||
![Logo of nuxt-envalid](/docs/assets/images/banner_1.png) | ||
# nuxt-envalid | ||
[![CI](https://github.com/manuelhenke/nuxt-envalid/actions/workflows/ci.yml/badge.svg)](https://github.com/manuelhenke/nuxt-envalid/actions/workflows/ci.yml) | ||
@@ -6,19 +10,28 @@ [![CodeQL](https://github.com/manuelhenke/nuxt-envalid/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/manuelhenke/nuxt-envalid/actions/workflows/codeql-analysis.yml) | ||
# nuxt-envalid | ||
> Dead simple [Envalid](https://github.com/af/envalid) integration for [Nuxt 2](https://nuxtjs.org). | ||
A Nuxt.js module thats validates your env variables and loads them cleaned into your application context. Uses [envalid](https://github.com/af/envalid) under the hood. | ||
- ✨ [Release Notes](CHANGELOG.md) | ||
- 📖 [Documentation](https://nuxt-envalid.henkebyte.com) | ||
## Setup | ||
## Features | ||
1. Add `nuxt-envalid` dependency to your project | ||
- Define a required schema for your environment variables | ||
- Validates variables in the `env` property of the `nuxt.config.js` | ||
- Validates variables in `process.env` | ||
- Validates variables present in the `.env` file, if loaded together with [@nuxtjs/dotenv](https://github.com/nuxt-community/dotenv-module) | ||
- Fails the build process if a variable is missing | ||
- Loads them cleaned and enriched with default values into your application context (`process.env` and `context.env`) | ||
```bash | ||
## Getting Started | ||
1. Add `nuxt-envalid` as dev-dependency to your project via `yarn` or `npm`: | ||
```sh | ||
yarn add --dev nuxt-envalid # or npm install --save-dev nuxt-envalid | ||
``` | ||
2. Add `nuxt-envalid` to the `buildModules` section of `nuxt.config.js` | ||
2. Add `nuxt-envalid` to the `buildModules` section of `nuxt.config.js`: | ||
:warning: If you are using a Nuxt version previous than **v2.9** you have to install module as a `dependency` (No `--dev` or `--save-dev` flags) and also use `modules` section in `nuxt.config.js` instead of `buildModules`. | ||
```js | ||
// nuxt.config.js | ||
export default { | ||
@@ -29,5 +42,8 @@ buildModules: ['nuxt-envalid'], | ||
### Using inline options | ||
:warning: If you are using a Nuxt version previous than **v2.9** you have to install the module as a `dependency` (No `--dev` or `--save-dev` flags) and also use `modules` section in `nuxt.config.js` instead of `buildModules`. | ||
### Inline config | ||
```js | ||
// nuxt.config.js | ||
export default { | ||
@@ -38,3 +54,3 @@ buildModules: [ | ||
{ | ||
/* module options */ | ||
/* module config */ | ||
}, | ||
@@ -46,9 +62,10 @@ ], | ||
### Using top level options | ||
### Top level config | ||
```js | ||
// nuxt.config.js | ||
export default { | ||
buildModules: ['nuxt-envalid'], | ||
envalid: { | ||
/* module options */ | ||
/* module config */ | ||
}, | ||
@@ -58,5 +75,8 @@ }; | ||
### Using a function to provide options | ||
### Config function | ||
If you need to use a function to provide the module config you are good to go: | ||
```js | ||
// nuxt.config.js | ||
export default { | ||
@@ -67,3 +87,3 @@ buildModules: [ | ||
() => ({ | ||
/* module options */ | ||
/* module config */ | ||
}), | ||
@@ -74,3 +94,3 @@ ], | ||
envalid: () => ({ | ||
/* module options */ | ||
/* module config */ | ||
}), | ||
@@ -80,22 +100,22 @@ }; | ||
### Hierarchy | ||
:warning: Defining module options inline will overwrite module options defined at top level. | ||
Defining module options inline will overwrite module options defined at top level. | ||
## Configuration | ||
## Options | ||
### Overview | ||
| Param | Description | Required | Default | | ||
| ------------------ | ----------------------------------------------------------------------------- | -------- | ------- | | ||
| `specs` | An object that specifies the format of required vars. | No | | | ||
| `options` | An (optional) object, which supports the following key: | No | | | ||
| `options.reporter` | Pass in a function to override the default error handling and console output. | No | | | ||
### `specs` | ||
- Type: `{ [key: string]: ValidatorSpec }` | ||
- Default: `{}` | ||
For further information take a look at the [official documentation of envalid](https://github.com/af/envalid#validator-types). | ||
```js | ||
// nuxt.config.js | ||
import { bool, str } from 'nuxt-envalid'; | ||
export default { | ||
env: { | ||
TITLE: 'title', | ||
IS_PUBLIC: true, | ||
}, | ||
buildModules: ['nuxt-envalid'], | ||
@@ -114,14 +134,9 @@ envalid: { | ||
- Type: `CleanOptions` | ||
- Default: `{}` | ||
For further information take a look at the [official documentation of envalid](https://github.com/af/envalid#error-reporting). | ||
```js | ||
// nuxt.config.js | ||
export default { | ||
buildModules: ['nuxt-envalid'], | ||
envalid: { | ||
specs: { | ||
TITLE: str(), | ||
}, | ||
options: { | ||
@@ -138,23 +153,67 @@ reporter: ({ errors, env }) => { | ||
After creating your .env file in the project root, simply run your usual `yarn dev` or `npm run dev`. | ||
The variable inside the .env file will be added to the context (`context.env`) and process (`process.env`). | ||
### Usage with `env` property in Nuxt config | ||
## Using together with [@nuxtjs/dotenv](https://github.com/nuxt-community/dotenv-module) | ||
```js | ||
// nuxt.config.js | ||
import { bool, host } from 'nuxt-envalid'; | ||
export default { | ||
env: { | ||
BACKEND_HOST: 'backend.example.com', | ||
}, | ||
buildModules: ['nuxt-envalid'], | ||
envalid: { | ||
specs: { | ||
BACKEND_HOST: host(), | ||
BACKEND_SECURE: bool({ default: true }), | ||
}, | ||
}, | ||
}; | ||
``` | ||
This module will validate the result of `@nuxtjs/dotenv` as well and then overwrite the values of the variables defined in the `specs`. Be sure to include this module **AFTER** `@nuxtjs/dotenv`. | ||
```vue | ||
<!-- pages/index.vue --> | ||
<template> | ||
<div> | ||
<h1>{ { post.title } }</h1> | ||
<p>{ { post.description } }</p> | ||
</div> | ||
</template> | ||
<script> | ||
export default { | ||
async asyncData({ env }) { | ||
const response = await fetch( | ||
`${env.BACKEND_SECURE ? 'https' : 'http'}://${env.BACKEND_HOST}/post/1` | ||
); | ||
const post = await response.json(); | ||
return { post }; | ||
}, | ||
}; | ||
</script> | ||
``` | ||
### Using together with [@nuxtjs/dotenv](https://github.com/nuxt-community/dotenv-module) | ||
This module will validate the result of `@nuxtjs/dotenv`. | ||
:warning: Be sure to include this module **AFTER** `@nuxtjs/dotenv`. | ||
```sh | ||
# .env file | ||
TITLE='title' | ||
IS_PUBLIC=true | ||
# .env | ||
CTF_CDA_ACCESS_TOKEN="super-secret-access-token" | ||
``` | ||
```js | ||
// nuxt.config.js | ||
import { str } from 'nuxt-envalid'; | ||
export default { | ||
env: { | ||
CTF_SPACE_ID: 'my-space-id', | ||
}, | ||
buildModules: ['@nuxtjs/dotenv', 'nuxt-envalid'], | ||
envalid: { | ||
specs: { | ||
TITLE: str(), | ||
SUBTITLE: str({ default: 'subtitle' }), | ||
IS_PUBLIC: bool({ default: false }), | ||
CTF_SPACE_ID: str(), | ||
CTF_CDA_ACCESS_TOKEN: str(), | ||
CTF_ENVIRONMENT: str({ default: 'production' }), | ||
}, | ||
@@ -165,4 +224,23 @@ }, | ||
```js | ||
// plugins/contentful.js | ||
import { createClient } from 'contentful'; | ||
export default createClient({ | ||
space: process.env.CTF_SPACE_ID, | ||
accessToken: process.env.CTF_CDA_ACCESS_TOKEN, | ||
environment: process.env.CTF_ENVIRONMENT, | ||
}); | ||
``` | ||
### Accessing the data | ||
Since this module is only there to validate the presence of environment variables and to load them sanitized into the already existing `process.env` and `context.env`, the general access of the data doesn't change. Take a look on the official documentation to get a deeper insight [here](https://nuxtjs.org/docs/configuration-glossary/configuration-env/). | ||
### Missing variables | ||
Validation takes places during build time. So if any variable out of the specified configuration is missing in the `env` property of the Nuxt config or in the `.env` file, if `@nuxtjs/dotenv` is used, the build will fail. | ||
## License | ||
[MIT License](./LICENSE) |
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
12744
1
236