diff --git a/types/dotenv/dotenv-tests.ts b/types/dotenv/dotenv-tests.ts index 613da3b056..2776e6d202 100644 --- a/types/dotenv/dotenv-tests.ts +++ b/types/dotenv/dotenv-tests.ts @@ -1,20 +1,18 @@ -import dotenv = require('dotenv'); +import dotenv = require("dotenv"); const env = dotenv.config(); -const dbUrl: string | null = env.error || !env.parsed ? null : env.parsed['DATABASE_URL']; +const dbUrl: string | null = env.error || !env.parsed ? null : env.parsed["BASIC"]; -dotenv.config({ - path: '.env' +dotenv.load({ + path: ".env-example", + encoding: "utf8", + debug: true }); -dotenv.config({ - encoding: 'utf8' +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 }); - -dotenv.load(); - -const parsed = dotenv.parse("ENVIRONMENT=production\nDEBUG=no\n"); -const debug: string = parsed['DEBUG']; - -const parsedFromBuffer = dotenv.parse(new Buffer("JUSTICE=league\n")); -const justice: string = parsedFromBuffer['JUSTICE']; +const justice: string = parsedFromBuffer["JUSTICE"]; diff --git a/types/dotenv/index.d.ts b/types/dotenv/index.d.ts index 4dd67b15c0..f8675ec22e 100644 --- a/types/dotenv/index.d.ts +++ b/types/dotenv/index.d.ts @@ -1,46 +1,65 @@ -// Type definitions for dotenv 4.0 +// Type definitions for dotenv 6.1 // Project: https://github.com/motdotla/dotenv -// Definitions by: Jussi Kinnula , Borek Bernard , Eric Naeseth +// Definitions by: Jussi Kinnula +// Borek Bernard +// Eric Naeseth +// Max Beatty // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +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): {[name: string]: string}; +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` into `process.env`. - * - * @return Object Object with either - * - "parsed" containing the parsed keys and values or - * - "error" containing an Error object - * + * 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?: DotenvOptions): DotenvResult; +export function config(options?: DotenvConfigOptions): DotenvConfigOutput; export const load: typeof config; - -export interface DotenvOptions { - /** - * You can specify a custom path if your file containing environment variables is named or located differently. - * - * @default '.env' - */ - path?: string; - - /** - * You may specify the encoding of your file containing environment variables using this option. - * - * @default 'utf8' - */ - encoding?: string; -} - -export interface DotenvResult { - error?: Error; - parsed?: { - [name: string]: string; - }; -}