ogi-addon 1.1.0 → 1.1.6

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.
@@ -1,271 +1,271 @@
1
- import z, { ZodError } from "zod"
2
-
3
- export interface ConfigurationFile {
4
- [key: string]: ConfigurationOption
5
- }
6
-
7
- const configValidation = z.object({
8
- name: z.string().min(1),
9
- displayName: z.string().min(1),
10
- description: z.string().min(1),
11
- })
12
-
13
- export function isStringOption(option: ConfigurationOption): option is StringOption {
14
- return option.type === 'string';
15
- }
16
-
17
- export function isNumberOption(option: ConfigurationOption): option is NumberOption {
18
- return option.type === 'number';
19
- }
20
-
21
- export function isBooleanOption(option: ConfigurationOption): option is BooleanOption {
22
- return option.type === 'boolean';
23
- }
24
-
25
- export class ConfigurationBuilder {
26
- private options: ConfigurationOption[] = [];
27
-
28
- /**
29
- * Add a number option to the configuration builder and return the builder for chaining. You must provide a name, display name, and description for the option.
30
- * @param option { (option: NumberOption) => NumberOption }
31
- * @returns
32
- */
33
- public addNumberOption(option: (option: NumberOption) => NumberOption): ConfigurationBuilder {
34
- let newOption = new NumberOption();
35
- newOption = option(newOption);
36
- this.options.push(newOption);
37
- return this;
38
- }
39
-
40
- /**
41
- * Add a string option to the configuration builder and return the builder for chaining. You must provide a name, display name, and description for the option.
42
- * @param option { (option: StringOption) => StringOption }
43
- */
44
- public addStringOption(option: (option: StringOption) => StringOption) {
45
- let newOption = new StringOption();
46
- newOption = option(newOption);
47
- this.options.push(newOption);
48
- return this;
49
- }
50
-
51
- /**
52
- * Add a boolean option to the configuration builder and return the builder for chaining. You must provide a name, display name, and description for the option.
53
- * @param option { (option: BooleanOption) => BooleanOption }
54
- */
55
- public addBooleanOption(option: (option: BooleanOption) => BooleanOption) {
56
- let newOption = new BooleanOption();
57
- newOption = option(newOption);
58
- this.options.push(newOption);
59
- return this;
60
- }
61
-
62
- public build(includeFunctions: boolean): ConfigurationFile {
63
- let config: ConfigurationFile = {};
64
- this.options.forEach(option => {
65
- // remove all functions from the option object
66
- if (!includeFunctions) {
67
- option = JSON.parse(JSON.stringify(option));
68
- const optionData = configValidation.safeParse(option)
69
- if (!optionData.success) {
70
- throw new ZodError(optionData.error.errors)
71
- }
72
-
73
- config[option.name] = option;
74
- }
75
- else {
76
- config[option.name] = option;
77
- }
78
- });
79
- return config;
80
- }
81
- }
82
-
83
- export type ConfigurationOptionType = 'string' | 'number' | 'boolean' | 'unset'
84
- export class ConfigurationOption {
85
- public name: string = '';
86
- public defaultValue: unknown = '';
87
- public displayName: string = '';
88
- public description: string = '';
89
- public type: ConfigurationOptionType = 'unset'
90
-
91
- /**
92
- * Set the name of the option. **REQUIRED**
93
- * @param name {string} The name of the option. This is used to reference the option in the configuration file.
94
- */
95
- setName(name: string) {
96
- this.name = name;
97
- return this;
98
- }
99
-
100
- /**
101
- * Set the display name of the option. This is used to show the user a human readable version of what the option is. **REQUIRED**
102
- * @param displayName {string} The display name of the option.
103
- * @returns
104
- */
105
- setDisplayName(displayName: string) {
106
- this.displayName = displayName;
107
- return this;
108
- }
109
-
110
- /**
111
- * Set the description of the option. This is to show the user a brief description of what this option does. **REQUIRED**
112
- * @param description {string} The description of the option.
113
- * @returns
114
- */
115
- setDescription(description: string) {
116
- this.description = description;
117
- return this;
118
- }
119
-
120
- /**
121
- * Validation code for the option. This is called when the user provides input to the option. If the validation fails, the user will be prompted to provide input again.
122
- * @param input {unknown} The input to validate
123
- */
124
- validate(input: unknown): [ boolean, string ] {
125
- throw new Error('Validation code not implemented. Value: ' + input)
126
- };
127
- }
128
-
129
- export class StringOption extends ConfigurationOption {
130
- public allowedValues: string[] = [];
131
- public minTextLength: number = 0;
132
- public maxTextLength: number = Number.MAX_SAFE_INTEGER;
133
- public defaultValue: string = '';
134
- public inputType: 'text' | 'file' | 'password' | 'folder' = 'text';
135
- public type: ConfigurationOptionType = 'string'
136
-
137
- /**
138
- * Set the allowed values for the string. If the array is empty, any value is allowed. When provided, the client will act like this option is a dropdown.
139
- * @param allowedValues {string[]} An array of allowed values for the string. If the array is empty, any value is allowed.
140
- */
141
- setAllowedValues(allowedValues: string[]): this {
142
- this.allowedValues = allowedValues;
143
- return this;
144
- }
145
-
146
- /**
147
- * Set the default value for the string. This value will be used if the user does not provide a value. **HIGHLY RECOMMENDED**
148
- * @param defaultValue {string} The default value for the string.
149
- */
150
- setDefaultValue(defaultValue: string): this {
151
- this.defaultValue = defaultValue;
152
- return this;
153
- }
154
-
155
- /**
156
- * Set the minimum text length for the string. If the user provides a string that is less than this value, the validation will fail.
157
- * @param minTextLength {number} The minimum text length for the string.
158
- */
159
- setMinTextLength(minTextLength: number): this {
160
- this.minTextLength = minTextLength;
161
- return this;
162
- }
163
-
164
- /**
165
- * Set the maximum text length for the string. If the user provides a string that is greater than this value, the validation will fail.
166
- * @param maxTextLength {number} The maximum text length for the string.
167
- */
168
- setMaxTextLength(maxTextLength: number): this {
169
- this.maxTextLength = maxTextLength;
170
- return this;
171
- }
172
-
173
- /**
174
- * Set the input type for the string. This will change how the client renders the input.
175
- * @param inputType {'text' | 'file' | 'password' | 'folder'} The input type for the string.
176
- */
177
- setInputType(inputType: 'text' | 'file' | 'password' | 'folder'): this {
178
- this.inputType = inputType;
179
- return this;
180
- }
181
-
182
- override validate(input: unknown): [ boolean, string ] {
183
- if (typeof input !== 'string') {
184
- return [ false, 'Input is not a string' ];
185
- }
186
- if (this.allowedValues.length === 0 && input.length !== 0)
187
- return [ true, '' ];
188
- if (input.length < this.minTextLength || input.length > this.maxTextLength) {
189
- return [ false, 'Input is not within the text length ' + this.minTextLength + ' and ' + this.maxTextLength + ' characters (currently ' + input.length + ' characters)' ];
190
- }
191
-
192
- return [ this.allowedValues.includes(input), 'Input is not an allowed value' ];
193
- }
194
- }
195
-
196
- export class NumberOption extends ConfigurationOption {
197
- public min: number = 0;
198
- public max: number = Number.MAX_SAFE_INTEGER;
199
- public defaultValue: number = 0;
200
- public type: ConfigurationOptionType = 'number'
201
- public inputType: 'range' | 'number' = 'number';
202
-
203
- /**
204
- * Set the minimum value for the number. If the user provides a number that is less than this value, the validation will fail.
205
- * @param min {number} The minimum value for the number.
206
- */
207
- setMin(min: number): this {
208
- this.min = min;
209
- return this;
210
- }
211
-
212
- /**
213
- * Set the input type for the number. This will change how the client renders the input.
214
- * @param type {'range' | 'number'} The input type for the number.
215
- */
216
- setInputType(type: 'range' | 'number'): this {
217
- this.inputType = type;
218
- return this;
219
- }
220
-
221
- /**
222
- * Set the maximum value for the number. If the user provides a number that is greater than this value, the validation will fail.
223
- * @param max {number} The maximum value for the number.
224
- */
225
- setMax(max: number): this {
226
- this.max = max;
227
- return this
228
- }
229
-
230
- /**
231
- * Set the default value for the number. This value will be used if the user does not provide a value. **HIGHLY RECOMMENDED**
232
- * @param defaultValue {number} The default value for the number.
233
- */
234
- setDefaultValue(defaultValue: number): this {
235
- this.defaultValue = defaultValue;
236
- return this;
237
- }
238
-
239
- override validate(input: unknown): [ boolean, string ] {
240
- if (isNaN(Number(input))) {
241
- return [ false, 'Input is not a number' ];
242
- }
243
- if (Number(input) < this.min || Number(input) > this.max) {
244
- return [ false, 'Input is not within the range of ' + this.min + ' and ' + this.max ];
245
- }
246
- return [ true, '' ];
247
- }
248
-
249
- }
250
-
251
- export class BooleanOption extends ConfigurationOption {
252
- public type: ConfigurationOptionType = 'boolean'
253
- public defaultValue: boolean = false;
254
-
255
- /**
256
- * Set the default value for the boolean. This value will be used if the user does not provide a value. **HIGHLY RECOMMENDED**
257
- * @param defaultValue {boolean} The default value for the boolean.
258
- */
259
- setDefaultValue(defaultValue: boolean): this {
260
- this.defaultValue = defaultValue;
261
- return this;
262
- }
263
-
264
- override validate(input: unknown): [ boolean, string ] {
265
- if (typeof input !== 'boolean') {
266
- return [ false, 'Input is not a boolean' ];
267
- }
268
- return [ true, '' ];
269
- }
270
-
1
+ import z, { ZodError } from "zod"
2
+
3
+ export interface ConfigurationFile {
4
+ [key: string]: ConfigurationOption
5
+ }
6
+
7
+ const configValidation = z.object({
8
+ name: z.string().min(1),
9
+ displayName: z.string().min(1),
10
+ description: z.string().min(1),
11
+ })
12
+
13
+ export function isStringOption(option: ConfigurationOption): option is StringOption {
14
+ return option.type === 'string';
15
+ }
16
+
17
+ export function isNumberOption(option: ConfigurationOption): option is NumberOption {
18
+ return option.type === 'number';
19
+ }
20
+
21
+ export function isBooleanOption(option: ConfigurationOption): option is BooleanOption {
22
+ return option.type === 'boolean';
23
+ }
24
+
25
+ export class ConfigurationBuilder {
26
+ private options: ConfigurationOption[] = [];
27
+
28
+ /**
29
+ * Add a number option to the configuration builder and return the builder for chaining. You must provide a name, display name, and description for the option.
30
+ * @param option { (option: NumberOption) => NumberOption }
31
+ * @returns
32
+ */
33
+ public addNumberOption(option: (option: NumberOption) => NumberOption): ConfigurationBuilder {
34
+ let newOption = new NumberOption();
35
+ newOption = option(newOption);
36
+ this.options.push(newOption);
37
+ return this;
38
+ }
39
+
40
+ /**
41
+ * Add a string option to the configuration builder and return the builder for chaining. You must provide a name, display name, and description for the option.
42
+ * @param option { (option: StringOption) => StringOption }
43
+ */
44
+ public addStringOption(option: (option: StringOption) => StringOption) {
45
+ let newOption = new StringOption();
46
+ newOption = option(newOption);
47
+ this.options.push(newOption);
48
+ return this;
49
+ }
50
+
51
+ /**
52
+ * Add a boolean option to the configuration builder and return the builder for chaining. You must provide a name, display name, and description for the option.
53
+ * @param option { (option: BooleanOption) => BooleanOption }
54
+ */
55
+ public addBooleanOption(option: (option: BooleanOption) => BooleanOption) {
56
+ let newOption = new BooleanOption();
57
+ newOption = option(newOption);
58
+ this.options.push(newOption);
59
+ return this;
60
+ }
61
+
62
+ public build(includeFunctions: boolean): ConfigurationFile {
63
+ let config: ConfigurationFile = {};
64
+ this.options.forEach(option => {
65
+ // remove all functions from the option object
66
+ if (!includeFunctions) {
67
+ option = JSON.parse(JSON.stringify(option));
68
+ const optionData = configValidation.safeParse(option)
69
+ if (!optionData.success) {
70
+ throw new ZodError(optionData.error.errors)
71
+ }
72
+
73
+ config[option.name] = option;
74
+ }
75
+ else {
76
+ config[option.name] = option;
77
+ }
78
+ });
79
+ return config;
80
+ }
81
+ }
82
+
83
+ export type ConfigurationOptionType = 'string' | 'number' | 'boolean' | 'unset'
84
+ export class ConfigurationOption {
85
+ public name: string = '';
86
+ public defaultValue: unknown = '';
87
+ public displayName: string = '';
88
+ public description: string = '';
89
+ public type: ConfigurationOptionType = 'unset'
90
+
91
+ /**
92
+ * Set the name of the option. **REQUIRED**
93
+ * @param name {string} The name of the option. This is used to reference the option in the configuration file.
94
+ */
95
+ setName(name: string) {
96
+ this.name = name;
97
+ return this;
98
+ }
99
+
100
+ /**
101
+ * Set the display name of the option. This is used to show the user a human readable version of what the option is. **REQUIRED**
102
+ * @param displayName {string} The display name of the option.
103
+ * @returns
104
+ */
105
+ setDisplayName(displayName: string) {
106
+ this.displayName = displayName;
107
+ return this;
108
+ }
109
+
110
+ /**
111
+ * Set the description of the option. This is to show the user a brief description of what this option does. **REQUIRED**
112
+ * @param description {string} The description of the option.
113
+ * @returns
114
+ */
115
+ setDescription(description: string) {
116
+ this.description = description;
117
+ return this;
118
+ }
119
+
120
+ /**
121
+ * Validation code for the option. This is called when the user provides input to the option. If the validation fails, the user will be prompted to provide input again.
122
+ * @param input {unknown} The input to validate
123
+ */
124
+ validate(input: unknown): [ boolean, string ] {
125
+ throw new Error('Validation code not implemented. Value: ' + input)
126
+ };
127
+ }
128
+
129
+ export class StringOption extends ConfigurationOption {
130
+ public allowedValues: string[] = [];
131
+ public minTextLength: number = 0;
132
+ public maxTextLength: number = Number.MAX_SAFE_INTEGER;
133
+ public defaultValue: string = '';
134
+ public inputType: 'text' | 'file' | 'password' | 'folder' = 'text';
135
+ public type: ConfigurationOptionType = 'string'
136
+
137
+ /**
138
+ * Set the allowed values for the string. If the array is empty, any value is allowed. When provided, the client will act like this option is a dropdown.
139
+ * @param allowedValues {string[]} An array of allowed values for the string. If the array is empty, any value is allowed.
140
+ */
141
+ setAllowedValues(allowedValues: string[]): this {
142
+ this.allowedValues = allowedValues;
143
+ return this;
144
+ }
145
+
146
+ /**
147
+ * Set the default value for the string. This value will be used if the user does not provide a value. **HIGHLY RECOMMENDED**
148
+ * @param defaultValue {string} The default value for the string.
149
+ */
150
+ setDefaultValue(defaultValue: string): this {
151
+ this.defaultValue = defaultValue;
152
+ return this;
153
+ }
154
+
155
+ /**
156
+ * Set the minimum text length for the string. If the user provides a string that is less than this value, the validation will fail.
157
+ * @param minTextLength {number} The minimum text length for the string.
158
+ */
159
+ setMinTextLength(minTextLength: number): this {
160
+ this.minTextLength = minTextLength;
161
+ return this;
162
+ }
163
+
164
+ /**
165
+ * Set the maximum text length for the string. If the user provides a string that is greater than this value, the validation will fail.
166
+ * @param maxTextLength {number} The maximum text length for the string.
167
+ */
168
+ setMaxTextLength(maxTextLength: number): this {
169
+ this.maxTextLength = maxTextLength;
170
+ return this;
171
+ }
172
+
173
+ /**
174
+ * Set the input type for the string. This will change how the client renders the input.
175
+ * @param inputType {'text' | 'file' | 'password' | 'folder'} The input type for the string.
176
+ */
177
+ setInputType(inputType: 'text' | 'file' | 'password' | 'folder'): this {
178
+ this.inputType = inputType;
179
+ return this;
180
+ }
181
+
182
+ override validate(input: unknown): [ boolean, string ] {
183
+ if (typeof input !== 'string') {
184
+ return [ false, 'Input is not a string' ];
185
+ }
186
+ if (this.allowedValues.length === 0 && input.length !== 0)
187
+ return [ true, '' ];
188
+ if (input.length < this.minTextLength || input.length > this.maxTextLength) {
189
+ return [ false, 'Input is not within the text length ' + this.minTextLength + ' and ' + this.maxTextLength + ' characters (currently ' + input.length + ' characters)' ];
190
+ }
191
+
192
+ return [ this.allowedValues.includes(input), 'Input is not an allowed value' ];
193
+ }
194
+ }
195
+
196
+ export class NumberOption extends ConfigurationOption {
197
+ public min: number = 0;
198
+ public max: number = Number.MAX_SAFE_INTEGER;
199
+ public defaultValue: number = 0;
200
+ public type: ConfigurationOptionType = 'number'
201
+ public inputType: 'range' | 'number' = 'number';
202
+
203
+ /**
204
+ * Set the minimum value for the number. If the user provides a number that is less than this value, the validation will fail.
205
+ * @param min {number} The minimum value for the number.
206
+ */
207
+ setMin(min: number): this {
208
+ this.min = min;
209
+ return this;
210
+ }
211
+
212
+ /**
213
+ * Set the input type for the number. This will change how the client renders the input.
214
+ * @param type {'range' | 'number'} The input type for the number.
215
+ */
216
+ setInputType(type: 'range' | 'number'): this {
217
+ this.inputType = type;
218
+ return this;
219
+ }
220
+
221
+ /**
222
+ * Set the maximum value for the number. If the user provides a number that is greater than this value, the validation will fail.
223
+ * @param max {number} The maximum value for the number.
224
+ */
225
+ setMax(max: number): this {
226
+ this.max = max;
227
+ return this
228
+ }
229
+
230
+ /**
231
+ * Set the default value for the number. This value will be used if the user does not provide a value. **HIGHLY RECOMMENDED**
232
+ * @param defaultValue {number} The default value for the number.
233
+ */
234
+ setDefaultValue(defaultValue: number): this {
235
+ this.defaultValue = defaultValue;
236
+ return this;
237
+ }
238
+
239
+ override validate(input: unknown): [ boolean, string ] {
240
+ if (isNaN(Number(input))) {
241
+ return [ false, 'Input is not a number' ];
242
+ }
243
+ if (Number(input) < this.min || Number(input) > this.max) {
244
+ return [ false, 'Input is not within the range of ' + this.min + ' and ' + this.max ];
245
+ }
246
+ return [ true, '' ];
247
+ }
248
+
249
+ }
250
+
251
+ export class BooleanOption extends ConfigurationOption {
252
+ public type: ConfigurationOptionType = 'boolean'
253
+ public defaultValue: boolean = false;
254
+
255
+ /**
256
+ * Set the default value for the boolean. This value will be used if the user does not provide a value. **HIGHLY RECOMMENDED**
257
+ * @param defaultValue {boolean} The default value for the boolean.
258
+ */
259
+ setDefaultValue(defaultValue: boolean): this {
260
+ this.defaultValue = defaultValue;
261
+ return this;
262
+ }
263
+
264
+ override validate(input: unknown): [ boolean, string ] {
265
+ if (typeof input !== 'boolean') {
266
+ return [ false, 'Input is not a boolean' ];
267
+ }
268
+ return [ true, '' ];
269
+ }
270
+
271
271
  }