市场夺宝奇兵
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.3 KiB

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