-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from StatelessStudio/v1.1.0
V1.1.0
- Loading branch information
Showing
32 changed files
with
3,206 additions
and
1,940 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.