forked from af/envalid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envalid.d.ts
148 lines (134 loc) · 4.44 KB
/
envalid.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
interface Spec<T> {
/**
* An Array that lists the admissable parsed values for the env var.
*/
choices?: T[]
/**
* A fallback value, which will be used if the env var wasn't specified. Providing a default effectively makes the env var optional.
*/
default?: T
/**
* A fallback value to use only when NODE_ENV is not 'production'.
* This is handy for env vars that are required for production environments, but optional for development and testing.
*/
devDefault?: T
/**
* A string that describes the env var.
*/
desc?: string
/**
* An example value for the env var.
*/
example?: string
/**
* A url that leads to more detailed documentation about the env var.
*/
docs?: string
}
interface ValidatorSpec<T> extends Spec<T> {
type: string
_parse: (input: string) => T
}
interface CleanEnv {
/** true if NODE_ENV === 'development' */
readonly isDevelopment: boolean
readonly isDev: boolean
/** true if NODE_ENV === 'test' */
readonly isTest: boolean
/** true if NODE_ENV === 'production' */
readonly isProduction: boolean
readonly isProd: boolean
readonly [key: string]: string | object | number | boolean | undefined
}
interface ReporterOptions {
errors: { [key: string]: Error }
env: CleanEnv
}
interface CleanOptions {
/**
* If true, the output of cleanEnv will only contain the env vars that were specified in the validators argument.
* @default false
*/
strict?: boolean
/**
* Pass in a function to override the default error handling and console output.
* See lib/reporter.js for the default implementation.
*/
reporter?: (opts: ReporterOptions) => void
/**
* A function used to transform the cleaned environment object before it is returned from cleanEnv.
*/
transformer?: (env: CleanEnv) => CleanEnv
/**
* Path to the file that is parsed by dotenv to optionally load more env vars at runtime.
* Pass null if you want to skip dotenv processing entirely and only load from process.env.
* @default ".env"
*/
dotEnvPath?: string | null
}
interface StrictCleanOptions extends CleanOptions {
strict: true
}
/**
* Returns a sanitized, immutable environment object. _Only_ the env vars
* specified in the `validators` parameter will be accessible on the returned
* object.
* @param environment An object containing your env vars (eg. process.env).
* @param validators An object that specifies the format of required vars.
* @param options An object that specifies options for cleanEnv.
*/
export function cleanEnv<T>(
environment: NodeJS.ProcessEnv,
validators: { [K in keyof T]: ValidatorSpec<T[K]> },
options: StrictCleanOptions
): Readonly<T> & CleanEnv
/**
* Returns a sanitized, immutable environment object.
* @param environment An object containing your env vars (eg. process.env).
* @param validators An object that specifies the format of required vars.
* @param options An object that specifies options for cleanEnv.
*/
export function cleanEnv<T>(
environment: NodeJS.ProcessEnv,
validators?: { [K in keyof T]: ValidatorSpec<T[K]> },
options?: CleanOptions
): Readonly<T> & CleanEnv
/**
* Create your own validator functions.
*/
export function makeValidator<T>(
parser: (input: string) => any,
type?: string
): (spec?: Spec<T>) => ValidatorSpec<T>
/**
* Parses env var string "0", "1", "true", "false", "t", "f" into Boolean.
*/
export function bool(spec?: Spec<boolean>): ValidatorSpec<boolean>
/**
* Parses an env var (eg. "42", "0.23", "1e5") into a Number.
*/
export function num(spec?: Spec<number>): ValidatorSpec<number>
/**
* Passes string values through, will ensure an value is present unless a default value is given.
*/
export function str(spec?: Spec<string>): ValidatorSpec<string>
/**
* Parses an env var with JSON.parse.
*/
export function json(spec?: Spec<any>): ValidatorSpec<any>
/**
* Ensures an env var is a url with a protocol and hostname
*/
export function url(spec?: Spec<string>): ValidatorSpec<string>
/**
* Ensures an env var is an email address
*/
export function email(spec?: Spec<string>): ValidatorSpec<string>
/**
* Ensures an env var is either a domain name or an ip address (v4 or v6)
*/
export function host(spec?: Spec<string>): ValidatorSpec<string>
/**
* Ensures an env var is a TCP port (1-65535)
*/
export function port(spec?: Spec<number>): ValidatorSpec<number>