Skip to content

Commit

Permalink
Merge pull request #38 from paperhive/feature/fefe-3
Browse files Browse the repository at this point in the history
Upgrade to fefe@3
  • Loading branch information
andrenarchy authored Apr 6, 2021
2 parents 7a9a4d1 + dbe2a71 commit 5ee3c2e
Show file tree
Hide file tree
Showing 15 changed files with 7,610 additions and 2,164 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable */
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:prettier/recommended'
],
};
34 changes: 34 additions & 0 deletions .github/workflows/npm-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Publish releases to npm

# based on https://dev.to/michi/publish-to-npm-automatically-with-github-actions-5805

on:
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest
name: 'Build, test and publish'
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.release.target_commitish }}
token: ${{ secrets.GH_RELEASE_TOKEN }}
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: 14
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run lint
- run: npm test
- run: git config --global user.name "GitHub CD bot"
- run: git config --global user.email "github-cd-bot@ems.press"
- run: npm version ${{ github.event.release.tag_name }}
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTOMATION_TOKEN }}
- run: git push
env:
github-token: ${{ secrets.GITHUB_TOKEN }}
22 changes: 22 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test

on: push

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['14', '15']
name: 'Build and test on node ${{ matrix.node }}'
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm run lint
- run: npm run test-cover
- uses: codecov/codecov-action@v1
- run: npm run build
8 changes: 5 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
coverage
/.*
/coverage
/src
node_modules
npm-debug.log
.DS_Store
.nyc_output
.*.swp
tsconfig*.json
tsconfig.json
*.tgz
*.test.*
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

42 changes: 16 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

Disclaimer: the author of this package opposes Trump and other racists and misogynists.

`envfefe` makes extensive use of the [fefe](https://github.com/paperhive/fefe) module that provides type-safe and purely functional validation, sanitization and transformation.

## Usage

Imagine you use the following environment variables, e.g., in a Docker `.env` file:
Expand All @@ -28,19 +30,19 @@ that you can use in your application:

```typescript
import { parseEnv } from 'envfefe'
import { parseBoolean, parseDate, parseJson, parseNumber, string } from 'fefe'
import { parseBoolean, parseDate, parseJson, parseNumber, pipe, string, success } from 'fefe'

const config = parseEnv({
elasticHost: string(),
elasticPort: parseNumber(),
enableCache: parseBoolean(),
launchDate: parseDate(),
gcloudCredentials: parseJson(),
whitelist: value => value.split(','),
elasticPort: pipe(string()).pipe(parseNumber()),
enableCache: pipe(string()).pipe(parseBoolean()),
launchDate: pipe(string()).pipe(parseDate()),
gcloudCredentials: pipe(string()).pipe(parseJson()),
whitelist: pipe(string()).pipe(value => success(value.split(','))),
})
```

The resulting `config` object will then be:
If validation passes (check via `isSuccess(config)`) then `config.right` will equal:
```typescript
{
elasticHost: 'elasticsearch',
Expand All @@ -53,54 +55,42 @@ The resulting `config` object will then be:
```

This module comes with full TypeScript support so if you are using
TypeScript then `config` will even have the correct types
automatically:
TypeScript then `config.right` will have the correct types automatically:
```typescript
{
elasticHost: string
elasticPort: number
enableCache: boolean
launchDate: Date
gcloudCredentials: any
gcloudCredentials: unknown
whitelist: string[]
}
```

Note:
* `camelCase` keys are automatically translated to
`SNAKE_CASE` environment variable names. You can override
this behavior, see below.
`SNAKE_CASE` environment variable names.
* By default all variables are mandatory. See below for
optional variables and default values.
* Values in the resulting object have proper types. If it can't be
sanitized because of a wrong type (like providing `foo` for a number)
will throw an error.
then `isSuccess(config)` will be `false` and `config.left` contains
the `FefeError` (see [FefeError docs](https://github.com/paperhive/fefe#fefeerror)).

## Advanced usage

### Specify environment variable names manually

If you don't use `SNAKE_CASE` for your environment variables
or unrelated names (why would you?) then you can set the name manually:

```javascript
const config = parse({
elasticHost: {name: 'MYELASTICHOST', sanitize: string()},
});
```

### Optional variables

```javascript
const config = parse({
elasticHost: {sanitize: string(), optional: true},
elasticHost: optional(string())
});
```

### Default values

```javascript
const config = parse({
elasticHost: {sanitize: string(), default: 'localhost'},
elasticHost: defaultTo(string())
});
```
2 changes: 0 additions & 2 deletions mocha.opts

This file was deleted.

Loading

0 comments on commit 5ee3c2e

Please sign in to comment.