UNPKG

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