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