[dotenv-flow] update typings to v3 (#36737)

This commit is contained in:
Dan Kerimdzhanov 2019-07-11 01:40:08 +06:00 committed by Armando Aguirre
parent bd4617c11e
commit 9b5c4c8179
6 changed files with 224 additions and 52 deletions

View File

@ -1,18 +1,21 @@
import dotenv = require("dotenv-flow");
import dotenv = require('dotenv-flow');
const filenames: string[] = dotenv.listDotenvFiles('/path/to/project');
dotenv.listDotenvFiles('/path/to/project', { node_env: 'development' });
const parsed: { [name: string]: string } = dotenv.parse('/path/to/project/.env');
dotenv.parse(['/path/to/project/.env'], { encoding: 'utf8' });
dotenv.unload('/path/to/project/.env');
dotenv.unload(['/path/to/project/.env'], { encoding: 'utf8' });
const env = dotenv.config();
const dbUrl: string | null = env.error || !env.parsed ? null : env.parsed["BASIC"];
const dbUrl: string | null = env.error || !env.parsed ? null : env.parsed['BASIC'];
dotenv.load({
cwd: "someDir",
encoding: "utf8",
purge_dotenv: true
dotenv.config({
node_env: 'production',
default_node_env: 'development',
path: '/path/to/project',
encoding: 'utf8',
purge_dotenv: true,
});
const parsed = dotenv.parse("NODE_ENV=production\nDB_HOST=a.b.c");
const dbHost: string = parsed["DB_HOST"];
const parsedFromBuffer = dotenv.parse(new Buffer("JUSTICE=league\n"), {
debug: true
});
const justice: string = parsedFromBuffer["JUSTICE"];

View File

@ -1,62 +1,106 @@
// Type definitions for dotenv-flow 0.2
// Type definitions for dotenv-flow 3.0
// Project: https://github.com/kerimdzhanov/dotenv-flow
// Definitions by: Vincent Langlet <https://github.com/vincentlanglet>
// Dan Kerimdzhanov <https://github.com/kerimdzhanov>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <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 DotenvListFilesOptions {
/**
* Node environment (development/test/production/etc,.).
*/
node_env?: string;
}
/**
* Returns a list of `.env*` filenames ordered by the env files priority from lowest to highest.
*
* @param dirname - path to `.env*` files' directory
* @param options - `.env*` files listing options
* @return a list of filenames for a given environment
*/
export function listDotenvFiles(dirname: string, options?: DotenvListFilesOptions): string[];
export interface DotenvReadFileOptions {
/**
* Encoding for reading the `.env*` files.
*/
encoding?: string;
}
export interface DotenvParseOutput {
[name: string]: string;
[name: string]: string;
}
/**
* Parses a string or buffer in the .env file format into an object.
* Parse a given file(s) to use the result programmatically.
* When several filenames are given, the parsed variables are merged using the "overwrite" strategy.
*
* @param src - contents to be parsed
* @param options - additional options
* @returns an object with keys and values based on `src`
* @param filenames - filename or a list of filenames to parse
* @param options - `fs.readFileSync` options
* @return the resulting map of `{ env_var: value }` as an object
*/
export function parse(
src: string | Buffer,
options?: DotenvParseOptions
): DotenvParseOutput;
export function parse(filenames: string | string[], options?: DotenvReadFileOptions): DotenvParseOutput;
export interface DotenvLoadOutput {
error?: Error;
parsed?: DotenvParseOutput;
}
/**
* Load variables defined in a given file(s) into `process.env`.
*
* @param filenames - filename or a list of filenames to load
* @param options - `fs.readFileSync` options
* @return an object with a `parsed` key containing the loaded content or an `error` key with an error that is occurred
*/
export function load(filenames: string | string[], options?: DotenvReadFileOptions): DotenvLoadOutput;
/**
* Unload variables defined in a given file(s) from `process.env`.
*
* @param filenames - filename or a list of filenames to unload
* @param options - `fs.readFileSync` options
*/
export function unload(filenames: string | string[], options?: DotenvReadFileOptions): void;
export interface DotenvConfigOptions {
/**
* You may specify a custom path if your file containing environment variables is located elsewhere.
*/
cwd?: string;
/**
* Node environment (development/test/production/etc,.).
*/
node_env?: string;
/**
* You may specify the encoding of your file containing environment variables.
*/
encoding?: string;
/**
* Default node environment to use if `process.env.NODE_ENV` is not present.
*/
default_node_env?: string;
/**
* In some cases the original "dotenv" library can be used by one of the dependent npm modules. Use to fix this.
*/
purge_dotenv?: boolean;
}
/**
* Path to `.env*` files directory.
*/
path?: string;
export interface DotenvConfigOutput {
error?: Error;
parsed?: DotenvParseOutput;
/**
* Encoding for reading the `.env*` files.
*/
encoding?: string;
/**
* In some cases the original "dotenv" library can be used by one of the dependent npm modules.
* It causes calling the original `dotenv.config()` that loads the `.env` file from your project before you can call `dotenv-flow.config()`.
*
* Such cases breaks `.env*` files priority because the previously loaded environment variables are treated as shell-defined thus having the higher priority.
*
* Setting the `purge_dotenv` option to `true` can gracefully fix this issue.
*/
purge_dotenv?: boolean;
}
/**
* 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
* Main entry point into the "dotenv-flow". Allows configuration before loading `.env*` files.
*
* @param options - configuration options
* @return an object with a `parsed` key containing the loaded content or an `error` key with an error that is occurred
*/
export function config(options?: DotenvConfigOptions): DotenvConfigOutput;
export const load: typeof config;
export function config(options?: DotenvConfigOptions): DotenvLoadOutput;

View File

@ -0,0 +1,20 @@
import dotenv = require("dotenv-flow");
const env = dotenv.config();
const dbUrl: string | null = env.error || !env.parsed ? null : env.parsed["BASIC"];
dotenv.config({
node_env: "production",
default_node_env: "development",
path: "/path/to/project",
encoding: "utf8",
purge_dotenv: true
});
const parsed = dotenv.parse("NODE_ENV=production\nDB_HOST=a.b.c");
const dbHost: string = parsed["DB_HOST"];
const parsedFromBuffer = dotenv.parse(new Buffer("JUSTICE=league\n"), {
debug: true
});
const justice: string = parsedFromBuffer["JUSTICE"];

76
types/dotenv-flow/v2/index.d.ts vendored Normal file
View File

@ -0,0 +1,76 @@
// Type definitions for dotenv-flow 2.0
// Project: https://github.com/kerimdzhanov/dotenv-flow
// Definitions by: Vincent Langlet <https://github.com/vincentlanglet>
// Dan Kerimdzhanov <https://github.com/kerimdzhanov>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <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 {
/**
* Node environment (development/test/production/etc,.).
*/
node_env?: string;
/**
* Default node environment to use if `process.env.NODE_ENV` is not present.
*/
default_node_env?: string;
/**
* Path to `.env*` files directory.
*/
path?: string;
/**
* Encoding for reading the `.env*` files.
*/
encoding?: string;
/**
* In some cases the original "dotenv" library can be used by one of the dependent npm modules.
* It causes calling the original `dotenv.config()` that loads the `.env` file from your project before you can call `dotenv-flow.config()`.
*
* Such cases breaks `.env*` files priority because the previously loaded environment variables are treated as shell-defined thus having the higher priority.
*
* Setting the `purge_dotenv` option to `true` can gracefully fix this issue.
*/
purge_dotenv?: 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;

View File

@ -0,0 +1,28 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../../",
"typeRoots": [
"../../"
],
"paths": {
"dotenv-flow": [
"dotenv-flow/v2"
]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"dotenv-flow-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }