UNPKG

4.73 kBTypeScriptView Raw
1// TypeScript Version: 3.0
2/// <reference types="node" />
3import type { URL } from 'node:url';
4
5export interface DotenvParseOutput {
6 [name: string]: string;
7}
8
9/**
10 * Parses a string or buffer in the .env file format into an object.
11 *
12 * See https://docs.dotenv.org
13 *
14 * @param src - contents to be parsed. example: `'DB_HOST=localhost'`
15 * @returns an object with keys and values based on `src`. example: `{ DB_HOST : 'localhost' }`
16 */
17export function parse<T extends DotenvParseOutput = DotenvParseOutput>(
18 src: string | Buffer
19): T;
20
21export interface DotenvConfigOptions {
22 /**
23 * Default: `path.resolve(process.cwd(), '.env')`
24 *
25 * Specify a custom path if your file containing environment variables is located elsewhere.
26 *
27 * example: `require('dotenv').config({ path: '/custom/path/to/.env' })`
28 */
29 path?: string | URL;
30
31 /**
32 * Default: `utf8`
33 *
34 * Specify the encoding of your file containing environment variables.
35 *
36 * example: `require('dotenv').config({ encoding: 'latin1' })`
37 */
38 encoding?: string;
39
40 /**
41 * Default: `false`
42 *
43 * Turn on logging to help debug why certain keys or values are not being set as you expect.
44 *
45 * example: `require('dotenv').config({ debug: process.env.DEBUG })`
46 */
47 debug?: boolean;
48
49 /**
50 * Default: `false`
51 *
52 * Override any environment variables that have already been set on your machine with values from your .env file.
53 *
54 * example: `require('dotenv').config({ override: true })`
55 */
56 override?: boolean;
57
58 /**
59 * Default: `process.env`
60 *
61 * Specify an object to write your secrets to. Defaults to process.env environment variables.
62 *
63 * example: `const processEnv = {}; require('dotenv').config({ processEnv: processEnv })`
64 */
65 processEnv?: DotenvPopulateInput;
66
67 /**
68 * Default: `undefined`
69 *
70 * Pass the DOTENV_KEY directly to config options. Defaults to looking for process.env.DOTENV_KEY environment variable. Note this only applies to decrypting .env.vault files. If passed as null or undefined, or not passed at all, dotenv falls back to its traditional job of parsing a .env file.
71 *
72 * example: `require('dotenv').config({ DOTENV_KEY: 'dotenv://:key_1234…@dotenv.org/vault/.env.vault?environment=production' })`
73 */
74 DOTENV_KEY?: string;
75}
76
77export interface DotenvConfigOutput {
78 error?: Error;
79 parsed?: DotenvParseOutput;
80}
81
82export interface DotenvPopulateOptions {
83 /**
84 * Default: `false`
85 *
86 * Turn on logging to help debug why certain keys or values are not being set as you expect.
87 *
88 * example: `require('dotenv').config({ debug: process.env.DEBUG })`
89 */
90 debug?: boolean;
91
92 /**
93 * Default: `false`
94 *
95 * Override any environment variables that have already been set on your machine with values from your .env file.
96 *
97 * example: `require('dotenv').config({ override: true })`
98 */
99 override?: boolean;
100}
101
102export interface DotenvPopulateInput {
103 [name: string]: string;
104}
105
106/**
107 * Loads `.env` file contents into process.env by default. If `DOTENV_KEY` is present, it smartly attempts to load encrypted `.env.vault` file contents into process.env.
108 *
109 * See https://docs.dotenv.org
110 *
111 * @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', debug: true, override: false }`
112 * @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } }
113 *
114 */
115export function config(options?: DotenvConfigOptions): DotenvConfigOutput;
116
117/**
118 * Loads `.env` file contents into process.env.
119 *
120 * See https://docs.dotenv.org
121 *
122 * @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', debug: true, override: false }`
123 * @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } }
124 *
125 */
126export function configDotenv(options?: DotenvConfigOptions): DotenvConfigOutput;
127
128/**
129 * Loads `source` json contents into `target` like process.env.
130 *
131 * See https://docs.dotenv.org
132 *
133 * @param processEnv - the target JSON object. in most cases use process.env but you can also pass your own JSON object
134 * @param parsed - the source JSON object
135 * @param options - additional options. example: `{ debug: true, override: false }`
136 * @returns {void}
137 *
138 */
139export function populate(processEnv: DotenvPopulateInput, parsed: DotenvPopulateInput, options?: DotenvConfigOptions): void;
140
141/**
142 * Decrypt ciphertext
143 *
144 * See https://docs.dotenv.org
145 *
146 * @param encrypted - the encrypted ciphertext string
147 * @param keyStr - the decryption key string
148 * @returns {string}
149 *
150 */
151export function decrypt(encrypted: string, keyStr: string): string;