@meltstudio/config-loader 1.0.4 → 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 +330 -175
  2. package/dist/index.d.ts +135 -84
  3. package/dist/index.js +605 -283
  4. package/package.json +39 -28
package/dist/index.d.ts CHANGED
@@ -1,130 +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
 
12
93
  type NodeTree = {
13
94
  [key: string]: NodeTree | ConfigNode;
14
95
  };
96
+ type RecursivePartial<T> = {
97
+ [K in keyof T]?: RecursivePartial<T[K]>;
98
+ };
15
99
  type SettingsSources<T> = {
16
100
  env: boolean;
17
101
  args: boolean;
18
102
  files?: string | string[] | false;
19
103
  dir?: string | false;
20
- defaults?: Partial<T>;
104
+ envFile?: string | string[] | false;
105
+ defaults?: RecursivePartial<T>;
106
+ exitOnError?: boolean;
21
107
  };
22
- 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;
23
116
  type Path = Array<string | number>;
24
117
  type ConfigFileStructure<T> = {
25
118
  [key: string]: string | T | number | boolean | Array<T> | string[];
26
119
  };
27
120
  interface ConfigFileData extends ConfigFileStructure<ConfigFileData> {
28
121
  }
29
- type ArrayValue = Array<any>;
122
+ type ArrayValue = Array<string | number | boolean | ConfigFileData>;
30
123
  declare class InvalidValue {
31
124
  }
32
125
 
33
- declare class ArrayValueContainer {
34
- readonly val: ArrayValue;
35
- readonly item: Node | OptionTypes;
36
- constructor(item: Node | OptionTypes, val: ArrayValue);
37
- }
38
-
39
- type Value = boolean | string | number | object | InvalidValue;
40
- type DefaultValue = Value | (() => string) | (() => number) | (() => boolean);
41
- type RecursiveNode<T> = {
42
- [key: string]: OptionBase | T;
43
- };
44
- interface Node extends RecursiveNode<Node> {
45
- }
46
- interface OptionClassParams {
47
- kind: OptionKind;
126
+ interface ArrayOptionClassParams<T extends OptionTypes> {
48
127
  required: boolean;
49
- env: string | null;
50
- cli: boolean;
51
- help: string;
52
- defaultValue?: DefaultValue;
128
+ defaultValue?: SchemaValue<T>[] | (() => SchemaValue<T>[]);
129
+ item: T;
53
130
  }
54
- declare class OptionBase {
55
- readonly params: OptionClassParams;
56
- constructor(params: OptionClassParams);
57
- getValue<T>(sourceFile: string | string[], env: {
58
- [key: string]: string | undefined;
59
- }, args: {
60
- [key: string]: string | boolean;
61
- }, path: Path, defaultValues?: Partial<T>, objectFromArray?: {
62
- value: ConfigFileData;
63
- file: string;
64
- }): ConfigNode | null;
65
- protected checkNumberType(val: Value, pathStr: string, sourceOfVal: string): Value;
66
- checkType(val: Value, path: Path, sourceOfVal: string): Value;
67
- protected findInObject(obj: ConfigFileData, path: Path): Value | ArrayValue;
68
- 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;
69
136
  }
70
137
 
71
- interface ArrayOptionClassParams {
138
+ interface ObjectOptionClassParams<T extends Node> {
72
139
  required: boolean;
73
- defaultValue?: DefaultValue;
74
- item: Node | OptionTypes;
75
- }
76
- declare class ArrayOption extends OptionBase {
77
- item: Node | OptionTypes;
78
- constructor(params: ArrayOptionClassParams);
79
- buildArrayOption(val: string[] | ConfigFileData[]): ArrayValueContainer | InvalidValue;
80
- checkType(val: Value, path: Path, sourceOfVal: string): Value;
140
+ item: T;
81
141
  }
82
-
83
- declare class PrimitiveOption extends OptionBase {
142
+ declare class ObjectOption<T extends Node = Node> extends OptionBase<"object"> {
143
+ item: T;
144
+ constructor(params: ObjectOptionClassParams<T>);
84
145
  }
85
146
 
86
- type OptionTypes = PrimitiveOption | ArrayOption;
147
+ type OptionTypes = PrimitiveOption | ArrayOption<OptionTypes> | ObjectOption<Node>;
87
148
 
88
- declare class Settings<T> {
149
+ declare class SettingsBuilder<T extends Node> {
89
150
  private readonly schema;
90
- private readonly sources;
91
- private sourceFile;
92
- private argsData;
93
- private envData;
94
- private optionsTree;
95
- private defaultData;
96
- private program;
97
- constructor(schema: Node, sources: SettingsSources<T>);
98
- private validateFiles;
99
- private load;
100
- private traverseOptions;
101
- private buildOption;
102
- private getValidatedArray;
103
- private processArrayWithSchema;
104
- private setOption;
105
- private addArg;
106
- private getValuesFromTree;
107
- get(): T;
108
- getExtended(): NodeTree;
151
+ constructor(schema: T);
152
+ load(sources: SettingsSources<SchemaValue<T>>): SchemaValue<T>;
153
+ loadExtended(sources: SettingsSources<SchemaValue<T>>): NodeTree;
109
154
  }
110
155
 
111
- interface OptionPropsArgs {
156
+ interface OptionPropsArgs<T> {
112
157
  required?: boolean;
113
158
  env?: string | null;
114
159
  cli?: boolean;
115
- defaultValue?: DefaultValue;
160
+ defaultValue?: T | (() => T);
116
161
  help?: string;
117
162
  }
118
- 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> {
119
169
  required?: boolean;
120
- item: Node | OptionTypes;
121
- defaultValue?: DefaultValue;
170
+ item: T;
122
171
  }
123
172
  declare const option: {
124
- string: (opts?: OptionPropsArgs) => PrimitiveOption;
125
- number: (opts?: OptionPropsArgs) => PrimitiveOption;
126
- bool: (opts?: OptionPropsArgs) => PrimitiveOption;
127
- array: (opts: ArrayOptionPropsArgs) => ArrayOption;
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>;
128
179
  };
129
180
 
130
- export { Settings as default, option };
181
+ export { type ConfigErrorEntry, ConfigFileError, ConfigLoadError, option as default };