1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | declare namespace convict {
|
15 |
|
16 | type Overwrite<T, U> = { [P in Exclude<keyof T, keyof U>]: T[P] } & U;
|
17 |
|
18 | type ValidationMethod = 'strict' | 'warn';
|
19 |
|
20 | interface ValidateOptions {
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | allowed?: ValidationMethod | undefined;
|
28 |
|
29 |
|
30 | strict?: boolean | undefined;
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 | output?: ((message: string) => void) | undefined;
|
37 | }
|
38 |
|
39 | interface Format {
|
40 | name?: string | undefined;
|
41 | validate?(val: any, schema: SchemaObj): void;
|
42 | coerce?(val: any): any;
|
43 | }
|
44 |
|
45 | interface Parser {
|
46 | extension: string | string[];
|
47 | parse: (content: string) => any;
|
48 | }
|
49 |
|
50 | type PredefinedFormat =
|
51 | | '*'
|
52 | | 'int'
|
53 | | 'port'
|
54 | | 'windows_named_pipe'
|
55 | | 'port_or_windows_named_pipe'
|
56 | | 'url'
|
57 | | 'email'
|
58 | | 'ipaddress'
|
59 | | 'duration'
|
60 | | 'timestamp'
|
61 | | 'nat'
|
62 | | String
|
63 | | Object
|
64 | | Number
|
65 | | RegExp
|
66 | | Boolean;
|
67 |
|
68 | interface SchemaObj<T = any> {
|
69 | |
70 |
|
71 |
|
72 |
|
73 | default: T | null;
|
74 | doc?: string | undefined;
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | format?: PredefinedFormat | any[] | ((val: any) => asserts val is T) | ((val: any) => void) | undefined;
|
88 | env?: string | undefined;
|
89 | arg?: string | undefined;
|
90 | sensitive?: boolean | undefined;
|
91 | nullable?: boolean | undefined;
|
92 | [key: string]: any;
|
93 | }
|
94 |
|
95 | type Schema<T> = {
|
96 | [P in keyof T]: Schema<T[P]> | SchemaObj<T[P]>;
|
97 | };
|
98 |
|
99 | interface InternalSchema<T> {
|
100 | _cvtProperties: {
|
101 | [K in keyof T]: T[K] extends object ? InternalSchema<T[K]> : { default: T[K] };
|
102 | };
|
103 | }
|
104 |
|
105 | interface Options {
|
106 | env?: NodeJS.ProcessEnv | undefined;
|
107 | args?: string[] | undefined;
|
108 | }
|
109 |
|
110 | // Taken from https://twitter.com/diegohaz/status/1309489079378219009
|
111 | type PathImpl<T, K extends keyof T> = K extends string
|
112 | ? T[K] extends Record<string, any>
|
113 | ? T[K] extends ArrayLike<any>
|
114 | ? K | `${K}.${PathImpl<T[K], Exclude<keyof T[K], keyof any[]>>}`
|
115 | : K | `${K}.${PathImpl<T[K], keyof T[K]>}`
|
116 | : K
|
117 | : never;
|
118 |
|
119 | type Path<T> = PathImpl<T, keyof T> | keyof T;
|
120 |
|
121 | type PathValue<T, P extends Path<T>> = P extends `${infer K}.${infer Rest}`
|
122 | ? K extends keyof T
|
123 | ? Rest extends Path<T[K]>
|
124 | ? PathValue<T[K], Rest>
|
125 | : never
|
126 | : never
|
127 | : P extends keyof T
|
128 | ? T[P]
|
129 | : never;
|
130 |
|
131 | interface Config<T> {
|
132 | /**
|
133 | * @returns the current value of the name property. name can use dot
|
134 | * notation to reference nested values
|
135 | */
|
136 | get<K extends Path<T> | null | undefined = undefined>(
|
137 | name?: K,
|
138 | ): K extends null | undefined ? T : K extends Path<T> ? PathValue<T, K> : never;
|
139 |
|
140 | /**
|
141 | * @returns the default value of the name property. name can use dot
|
142 | * notation to reference nested values
|
143 | */
|
144 | default<K extends Path<T> | null | undefined = undefined>(
|
145 | name?: K,
|
146 | ): K extends null | undefined ? T : K extends Path<T> ? PathValue<T, K> : never;
|
147 |
|
148 | /**
|
149 | * @returns true if the property name is defined, or false otherwise
|
150 | */
|
151 | has<K extends Path<T>>(name: K): boolean;
|
152 |
|
153 | /**
|
154 | * Sets the value of name to value. name can use dot notation to reference
|
155 | * nested values, e.g. "database.port". If objects in the chain don't yet
|
156 | * exist, they will be initialized to empty objects
|
157 | */
|
158 | set<K extends Path<T> | string>(name: K, value: K extends Path<T> ? PathValue<T, K> : any): Config<T>;
|
159 |
|
160 | /**
|
161 | * Loads and merges a JavaScript object into config
|
162 | */
|
163 | load<U>(conf: U): Config<Overwrite<T, U>>;
|
164 |
|
165 | /**
|
166 | * Loads and merges JSON configuration file(s) into config
|
167 | */
|
168 |
|
169 | loadFile<U>(files: string | string[]): Config<Overwrite<T, U>>;
|
170 | /**
|
171 | * Validates config against the schema used to initialize it
|
172 | */
|
173 | validate(options?: ValidateOptions): Config<T>;
|
174 |
|
175 | /**
|
176 | * Exports all the properties (that is the keys and their current values) as a {JSON} {Object}
|
177 | * @returns A {JSON} compliant {Object}
|
178 | */
|
179 | getProperties(): T;
|
180 |
|
181 | /**
|
182 | * Exports the schema as a {JSON} {Object}
|
183 | * @returns A {JSON} compliant {Object}
|
184 | */
|
185 | getSchema(): InternalSchema<T>;
|
186 |
|
187 | /**
|
188 | * Exports all the properties (that is the keys and their current values) as a JSON string.
|
189 | * @returns A string representing this object
|
190 | */
|
191 | toString(): string;
|
192 |
|
193 | /**
|
194 | * Exports the schema as a JSON string.
|
195 | * @returns A string representing the schema of this {Config}
|
196 | */
|
197 | getSchemaString(): string;
|
198 |
|
199 | /**
|
200 | * Gets the environment variable map, using the override passed to the
|
201 | * convict function or process.env if no override was passed.
|
202 | */
|
203 | getEnv(): string[];
|
204 |
|
205 | /**
|
206 | * Gets the array of process arguments, using the override passed to the
|
207 | * convict function or process.argv if no override was passed.
|
208 | */
|
209 | getArgs(): string[];
|
210 | }
|
211 | }
|
212 |
|
213 | interface convict {
|
214 | addFormat(format: convict.Format): void;
|
215 | addFormats(formats: { [name: string]: convict.Format }): void;
|
216 | addParser(parsers: convict.Parser | convict.Parser[]): void;
|
217 | <T>(config: convict.Schema<T> | string, opts?: convict.Options): convict.Config<T>;
|
218 | }
|
219 | declare var convict: convict;
|
220 | export = convict;
|
221 |
|
\ | No newline at end of file |