@meltstudio/config-loader 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +306 -169
  2. package/dist/index.d.ts +138 -68
  3. package/dist/index.js +570 -259
  4. package/package.json +39 -28
package/dist/index.d.ts CHANGED
@@ -1,111 +1,181 @@
1
- type SourceTypes = "file" | "env" | "args" | "default";
1
+ declare class ArrayValueContainer {
2
+ readonly val: ArrayValue;
3
+ readonly item: OptionTypes;
4
+ constructor(item: OptionTypes, val: ArrayValue);
5
+ }
6
+
7
+ interface EnvFileEntry {
8
+ value: string;
9
+ line: number;
10
+ column: number;
11
+ }
12
+ interface EnvFileResult {
13
+ entries: Map<string, EnvFileEntry>;
14
+ filePath: string;
15
+ }
16
+
17
+ interface ConfigErrorEntry {
18
+ message: string;
19
+ path?: string;
20
+ source?: string;
21
+ kind?: "required" | "type_conversion" | "invalid_path" | "invalid_state" | "file_validation";
22
+ line?: number;
23
+ column?: number;
24
+ }
25
+ declare class ConfigLoadError extends Error {
26
+ readonly errors: ConfigErrorEntry[];
27
+ readonly warnings: string[];
28
+ constructor(errors: ConfigErrorEntry[], warnings: string[]);
29
+ }
30
+ declare class ConfigFileError extends ConfigLoadError {
31
+ constructor(message: string);
32
+ }
33
+
34
+ declare class OptionErrors {
35
+ errors: ConfigErrorEntry[];
36
+ warnings: string[];
37
+ clearAll(): void;
38
+ }
39
+
40
+ type Value = boolean | string | number | object | InvalidValue;
41
+ type DefaultValue = Value | (() => string) | (() => number) | (() => boolean);
42
+ type TypedDefaultValue<T extends OptionKind> = T extends PrimitiveKind ? TypeOfPrimitiveKind<T> | (() => TypeOfPrimitiveKind<T>) : DefaultValue;
43
+ interface Node {
44
+ [key: string]: OptionBase;
45
+ }
46
+ interface OptionClassParams<T extends OptionKind> {
47
+ kind: T;
48
+ required: boolean;
49
+ env: string | null;
50
+ cli: boolean;
51
+ help: string;
52
+ defaultValue?: TypedDefaultValue<T>;
53
+ }
54
+ declare class OptionBase<T extends OptionKind = OptionKind> {
55
+ readonly params: OptionClassParams<T>;
56
+ constructor(params: OptionClassParams<T>);
57
+ getValue<U>(sourceFile: string | string[], env: {
58
+ [key: string]: string | undefined;
59
+ }, args: {
60
+ [key: string]: string | boolean;
61
+ }, path: Path, defaultValues?: Partial<U>, objectFromArray?: {
62
+ value: ConfigFileData;
63
+ file: string;
64
+ sourceMap?: {
65
+ lookup(path: string | string[]): {
66
+ line: number;
67
+ column: number;
68
+ } | undefined;
69
+ } | null;
70
+ }, envFileResults?: EnvFileResult[], errors?: OptionErrors): ConfigNode | null;
71
+ private resolveFromFileData;
72
+ checkType(val: Value, path: Path, sourceOfVal: string, errors?: OptionErrors): Value;
73
+ protected findInObject(obj: ConfigFileData, path: Path, errors?: OptionErrors): Value | ArrayValue;
74
+ buildArrayOption(_val: string[] | ConfigFileData[], _errors?: OptionErrors): ArrayValueContainer | InvalidValue;
75
+ }
76
+
77
+ type SourceTypes = "file" | "env" | "envFile" | "args" | "default";
2
78
  declare class ConfigNode {
3
79
  value: Value | ArrayValueContainer;
4
80
  path: string;
5
- source_type: SourceTypes;
81
+ sourceType: SourceTypes;
6
82
  file: string | null;
7
- variable_name: string | null;
8
- arg_name: string | null;
9
- constructor(value: Value | ArrayValue, path: string, source_type: SourceTypes, file: string | null, variable_name: string | null, arg_name: string | null);
83
+ variableName: string | null;
84
+ argName: string | null;
85
+ line: number | null;
86
+ column: number | null;
87
+ constructor(value: Value | ArrayValue, path: string, sourceType: SourceTypes, file: string | null, variableName: string | null, argName: string | null, line?: number | null, column?: number | null);
88
+ }
89
+
90
+ declare class PrimitiveOption<T extends PrimitiveKind = PrimitiveKind> extends OptionBase<T> {
10
91
  }
11
92
 
93
+ type NodeTree = {
94
+ [key: string]: NodeTree | ConfigNode;
95
+ };
96
+ type RecursivePartial<T> = {
97
+ [K in keyof T]?: RecursivePartial<T[K]>;
98
+ };
12
99
  type SettingsSources<T> = {
13
100
  env: boolean;
14
101
  args: boolean;
15
102
  files?: string | string[] | false;
16
103
  dir?: string | false;
17
- defaults?: Partial<T>;
104
+ envFile?: string | string[] | false;
105
+ defaults?: RecursivePartial<T>;
106
+ exitOnError?: boolean;
18
107
  };
19
- type OptionKind = "boolean" | "string" | "number" | "any" | "array" | "object";
108
+ type OptionKind = "boolean" | "string" | "number" | "array" | "object";
109
+ type PrimitiveKind = Extract<OptionKind, "boolean" | "string" | "number">;
110
+ type TypeOfPrimitiveKind<T extends PrimitiveKind> = T extends "boolean" ? boolean : T extends "string" ? string : T extends "number" ? number : never;
111
+ type SchemaValue<T extends OptionBase | Node> = T extends OptionBase ? T extends ArrayOption<OptionTypes> ? SchemaValue<T["item"]>[] : T extends ObjectOption<infer R> ? {
112
+ [K in keyof R]: SchemaValue<R[K]>;
113
+ } : T extends PrimitiveOption<infer R> ? TypeOfPrimitiveKind<R> : never : T extends Node ? {
114
+ [K in keyof T]: SchemaValue<T[K]>;
115
+ } : never;
20
116
  type Path = Array<string | number>;
21
117
  type ConfigFileStructure<T> = {
22
118
  [key: string]: string | T | number | boolean | Array<T> | string[];
23
119
  };
24
120
  interface ConfigFileData extends ConfigFileStructure<ConfigFileData> {
25
121
  }
26
- type ArrayValue = Array<any>;
122
+ type ArrayValue = Array<string | number | boolean | ConfigFileData>;
27
123
  declare class InvalidValue {
28
124
  }
29
125
 
30
- declare class ArrayValueContainer {
31
- readonly val: ArrayValue;
32
- readonly item: Node | OptionTypes;
33
- constructor(item: Node | OptionTypes, val: ArrayValue);
34
- }
35
-
36
- type Value = boolean | string | number | object | InvalidValue;
37
- type DefaultValue = Value | (() => string) | (() => number) | (() => boolean);
38
- type RecursiveNode<T> = {
39
- [key: string]: OptionBase | T;
40
- };
41
- interface Node extends RecursiveNode<Node> {
42
- }
43
- interface OptionClassParams {
44
- kind: OptionKind;
126
+ interface ArrayOptionClassParams<T extends OptionTypes> {
45
127
  required: boolean;
46
- env: string | null;
47
- cli: boolean;
48
- help: string;
49
- defaultValue?: DefaultValue;
128
+ defaultValue?: SchemaValue<T>[] | (() => SchemaValue<T>[]);
129
+ item: T;
50
130
  }
51
- declare class OptionBase {
52
- readonly params: OptionClassParams;
53
- constructor(params: OptionClassParams);
54
- getValue<T>(sourceFile: string | string[], env: {
55
- [key: string]: string | undefined;
56
- }, args: {
57
- [key: string]: string | boolean;
58
- }, path: Path, defaultValues?: Partial<T>, objectFromArray?: {
59
- value: ConfigFileData;
60
- file: string;
61
- }): ConfigNode | null;
62
- protected checkNumberType(val: Value, pathStr: string, sourceOfVal: string): Value;
63
- checkType(val: Value, path: Path, sourceOfVal: string): Value;
64
- protected findInObject(obj: ConfigFileData, path: Path): Value | ArrayValue;
65
- buildArrayOption(_val: string[] | ConfigFileData[]): ArrayValueContainer | InvalidValue;
131
+ declare class ArrayOption<T extends OptionTypes> extends OptionBase<"array"> {
132
+ item: T;
133
+ constructor(params: ArrayOptionClassParams<T>);
134
+ buildArrayOption(val: string[] | ConfigFileData[], errors?: OptionErrors): ArrayValueContainer | InvalidValue;
135
+ checkType(val: Value, path: Path, sourceOfVal: string, errors?: OptionErrors): Value;
66
136
  }
67
137
 
68
- interface ArrayOptionClassParams {
138
+ interface ObjectOptionClassParams<T extends Node> {
69
139
  required: boolean;
70
- defaultValue?: DefaultValue;
71
- item: Node | OptionTypes;
72
- }
73
- declare class ArrayOption extends OptionBase {
74
- item: Node | OptionTypes;
75
- constructor(params: ArrayOptionClassParams);
76
- buildArrayOption(val: string[] | ConfigFileData[]): ArrayValueContainer | InvalidValue;
77
- checkType(val: Value, path: Path, sourceOfVal: string): Value;
140
+ item: T;
78
141
  }
79
-
80
- declare class PrimitiveOption extends OptionBase {
142
+ declare class ObjectOption<T extends Node = Node> extends OptionBase<"object"> {
143
+ item: T;
144
+ constructor(params: ObjectOptionClassParams<T>);
81
145
  }
82
146
 
83
- type OptionTypes = PrimitiveOption | ArrayOption;
147
+ type OptionTypes = PrimitiveOption | ArrayOption<OptionTypes> | ObjectOption<Node>;
84
148
 
85
- declare class SettingsBuilder {
149
+ declare class SettingsBuilder<T extends Node> {
86
150
  private readonly schema;
87
- constructor(schema: Node);
88
- load<T>(sources: SettingsSources<T>): T;
151
+ constructor(schema: T);
152
+ load(sources: SettingsSources<SchemaValue<T>>): SchemaValue<T>;
153
+ loadExtended(sources: SettingsSources<SchemaValue<T>>): NodeTree;
89
154
  }
90
155
 
91
- interface OptionPropsArgs {
156
+ interface OptionPropsArgs<T> {
92
157
  required?: boolean;
93
158
  env?: string | null;
94
159
  cli?: boolean;
95
- defaultValue?: DefaultValue;
160
+ defaultValue?: T | (() => T);
96
161
  help?: string;
97
162
  }
98
- interface ArrayOptionPropsArgs {
163
+ interface ArrayOptionPropsArgs<T extends OptionTypes> {
164
+ required?: boolean;
165
+ item: T;
166
+ defaultValue?: SchemaValue<T>[] | (() => SchemaValue<T>[]);
167
+ }
168
+ interface ObjectOptionPropsArgs<T extends Node> {
99
169
  required?: boolean;
100
- item: Node | OptionTypes;
101
- defaultValue?: DefaultValue;
170
+ item: T;
102
171
  }
103
172
  declare const option: {
104
- string: (opts?: OptionPropsArgs) => PrimitiveOption;
105
- number: (opts?: OptionPropsArgs) => PrimitiveOption;
106
- bool: (opts?: OptionPropsArgs) => PrimitiveOption;
107
- array: (opts: ArrayOptionPropsArgs) => ArrayOption;
108
- schema: (theSchema: Node) => SettingsBuilder;
173
+ string: (opts?: OptionPropsArgs<string>) => PrimitiveOption<"string">;
174
+ number: (opts?: OptionPropsArgs<number>) => PrimitiveOption<"number">;
175
+ bool: (opts?: OptionPropsArgs<boolean>) => PrimitiveOption<"boolean">;
176
+ array: <T extends OptionTypes>(opts: ArrayOptionPropsArgs<T>) => ArrayOption<T>;
177
+ object: <T extends Node>(opts: ObjectOptionPropsArgs<T>) => ObjectOption<T>;
178
+ schema: <T extends Node>(theSchema: T) => SettingsBuilder<T>;
109
179
  };
110
180
 
111
- export { option as default };
181
+ export { type ConfigErrorEntry, ConfigFileError, ConfigLoadError, option as default };