UNPKG

7.67 kBTypeScriptView Raw
1// Type definitions for convict 6.1
2// Project: https://github.com/mozilla/node-convict
3// Definitions by: Wim Looman <https://github.com/Nemo157>
4// Vesa Poikajärvi <https://github.com/vesse>
5// Eli Young <https://github.com/elyscape>
6// Suntharesan Mohan <https://github.com/msuntharesan>
7// Igor Strebezhev <https://github.com/xamgore>
8// Peter Somogyvari <https://github.com/petermetz>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10// TypeScript Version: 4.1
11
12/// <reference types="node" />
13
14declare namespace convict {
15 // Taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
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 * If set to warn, any properties specified in config files that are not declared in
23 * the schema will print a warning. This is the default behavior. If set to strict,
24 * any properties specified in config files that are not declared in the schema will
25 * throw errors. This is to ensure that the schema and the config files are in sync.
26 */
27 allowed?: ValidationMethod | undefined;
28
29 /** @deprecated use allowed instead */
30 strict?: boolean | undefined;
31
32 /**
33 * If specified, possible warnings will be passed to this function instead of being
34 * outputted to console.log, which would be the default behaviour.
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 * You can define a configuration property as "required" without providing a default value.
71 * Set its default to null and if your format doesn't accept null it will throw an error.
72 */
73 default: T | null;
74 doc?: string | undefined;
75 /**
76 * From the implementation:
77 *
78 * format can be a:
79 * - predefined type, as seen below
80 * - an array of enumerated values, e.g. ["production", "development", "testing"]
81 * - built-in JavaScript type, i.e. Object, Array, String, Number, Boolean
82 * - function that performs validation and throws an Error on failure
83 *
84 * If omitted, format will be set to the value of Object.prototype.toString.call
85 * for the default value
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 properties: {
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
213interface 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}
219declare var convict: convict;
220export = convict;
221
\No newline at end of file