main.d.ts 4.8 KB

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