Skip to content

Commit

Permalink
Merge pull request #6 from StatelessStudio/v1.1.0
Browse files Browse the repository at this point in the history
V1.1.0
  • Loading branch information
DrewImm authored Feb 11, 2022
2 parents 37338f0 + 0ccfe06 commit 2b4896f
Show file tree
Hide file tree
Showing 32 changed files with 3,206 additions and 1,940 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
}
],
"rules": {
"no-console": 1,
"no-debugger": 1,
"curly": 2,
"indent": [2, "tab"],
"max-len": [1, 80],
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# ts-appconfig

## [1.1.0]

### Additions
- [Issue #5] Add readme section for options
- [Issue #2] Add option to disable overwriting process.env
- Typescript Template v2

### Fixes
- [Issue #4] TypeError: Cannot read properties of undefined (reading 'includes') when variable is not defined
- [Issue #3] refval.includes is not a function when referencing non-string variable

## [1.0.0] Initial Release
63 changes: 53 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,78 @@
# ts-appconfig

ts-appconfig is a zero-dependency node module to get strongly-typed and IDE-friendly environment variables.

Environment variables are pulled from 3 places, in this order. This means that each step will be overwritten by the following step, if that step also contains the variable:

1. Defaults from environment schema (your extended AppConfig class)
2. .env file
3. process.env

## Install

`npm i ts-appconfig`

## Setup

Create a file where you will declare your environment variable schema:

`src/environment.ts`
```typescript
import { AppConfig, configure } from '../src';

export class Environment extends AppConfig {
readonly APP_TITLE: string = '';
}

export const env: Environment = configure(Environment);
```

Create a `.env` file (and perhaps a `.env.example` file, too). Make sure you add `.env` to your `.gitignore` file, if it's not there already!

`.env`
```
APP_TITLE="Cool App"
```

Now import your `env` from the schema file you created and you're ready to use the environment variables!

`src/index.ts`
```typescript
import { env } from './environment.ts';

console.log(env.APP_TITLE);
```

`src/environment.ts`
Output:
```
Cool App
```

## Options

Pass `ConfigurationOptions` as a second argument to `configure` to customize how variables are loaded and parsed. All options are optional, you only have to specify the options you would like to change.

```typescript
import { AppConfig, configure } from '../src';
...
export const env: Environment = configure(Environment, {
// Give the .env file your own name or file path
// Default: .env
relativePath: 'my-own-filename.env',

export class Environment extends AppConfig {
readonly APP_TITLE: string = '';
}
// Allow variables that are not in schema but are defined in .env
// Default: false
allowUndeclared: false,

export const env: Environment = configure(Environment);
```
// Set process.env variables from the .env file
// Default: true
overwriteProcessEnv: true,

Output:
```
Cool App
// Allow duplicate entries in the .env file for the same variable
// Default: false
allowDuplicates: false,

// Skip unknown/unparsable lines in the .env file, or throw an exception
// Default: false (throws exception)
skipUnknownLines: false,
});
```
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"dist"
],
"watch": [ "src/", "*.*", ".env" ],
"exec": "npm start",
"exec": "node_modules/.bin/ts-node src",
"ext": "*"
}
Loading

0 comments on commit 2b4896f

Please sign in to comment.