Skip to content

Commit

Permalink
add TypeScript types
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Beatty committed Oct 25, 2018
1 parent 78546c1 commit 77c1b2a
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ untyped-import
untyped-type-import

[version]
^0.82.0
^0.84.0
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.nyc_output/
flow-typed/
tests/
.editorconfig
.flowconfig
.npmignore
.travis.yml
appveyor.yml
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ You can additionally, pass options to `config`.

Default: `path.resolve(process.cwd(), '.env')`

You may specify a custom path if your file containing environment variables is
named or located differently.
You may specify a custom path if your file containing environment variables is located elsewhere.

```js
require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })
Expand All @@ -102,8 +101,7 @@ require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })

Default: `utf8`

You may specify the encoding of your file containing environment variables
using this option.
You may specify the encoding of your file containing environment variables.

```js
require('dotenv').config({ encoding: 'base64' })
Expand Down Expand Up @@ -163,6 +161,7 @@ The parsing engine currently supports the following rules:
{MULTILINE: 'new
line'}
```

- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
- whitespace is removed from both ends of the value (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO=" some value "` becomes `{FOO: 'some value'}`)

Expand Down
155 changes: 151 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
"description": "Loads environment variables from .env file",
"main": "lib/main.js",
"scripts": {
"dtslint": "dtslint types",
"flow": "flow",
"pretest": "npm run lint",
"test": "tap tests/*.js --100",
"lint": "standard",
"postlint": "npm run lint-md",
"lint-md": "standard-markdown"
"lint-md": "standard-markdown",
"pretest": "npm run lint",
"test": "tap tests/*.js --100",
"posttest": "npm run dtslint && npm run flow"
},
"repository": {
"type": "git",
Expand All @@ -27,7 +29,9 @@
"readmeFilename": "README.md",
"license": "BSD-2-Clause",
"devDependencies": {
"flow-bin": "^0.82.0",
"@types/node": "^10.12.0",
"dtslint": "^0.3.0",
"flow-bin": "^0.84.0",
"sinon": "^6.3.5",
"standard": "^12.0.1",
"standard-markdown": "^5.0.1",
Expand All @@ -41,5 +45,6 @@
"ignore": [
"flow-typed/"
]
}
},
"types": "types"
}
57 changes: 57 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// <reference types="node" />

export interface DotenvParseOptions {
/**
* You may turn on logging to help debug why certain keys or values are not being set as you expect.
*/
debug?: boolean;
}

export interface DotenvParseOutput {
[name: string]: string;
}

/**
* Parses a string or buffer in the .env file format into an object.
*
* @param src - contents to be parsed
* @param options - additional options
* @returns an object with keys and values based on `src`
*/
export function parse(
src: string | Buffer,
options?: DotenvParseOptions
): DotenvParseOutput;

export interface DotenvConfigOptions {
/**
* You may specify a custom path if your file containing environment variables is located elsewhere.
*/
path?: string;

/**
* You may specify the encoding of your file containing environment variables.
*/
encoding?: string;

/**
* You may turn on logging to help debug why certain keys or values are not being set as you expect.
*/
debug?: boolean;
}

export interface DotenvConfigOutput {
error?: Error;
parsed?: DotenvParseOutput;
}

/**
* Loads `.env` file contents into {@link https://nodejs.org/api/process.html#process_process_env | `process.env`}.
* Example: 'KEY=value' becomes { parsed: { KEY: 'value' } }
*
* @param options - controls behavior
* @returns an object with a `parsed` key if successful or `error` key if an error occurred
*
*/
export function config(options?: DotenvConfigOptions): DotenvConfigOutput;
export const load: typeof config;
Loading

0 comments on commit 77c1b2a

Please sign in to comment.