@@ -148,39 +148,54 @@ const supportedFields: { [key in InputsTypes]: Schema } = {
148
148
export type StudioFieldData =
149
149
PartialSchema &
150
150
{
151
- type : keyof typeof supportedFields
151
+ type ? : keyof typeof supportedFields
152
152
icon ?: string
153
+ fields ?: { [ key : string ] : InputValue }
153
154
}
154
155
155
156
/**
156
- * Declares a Nuxt Studio compatible configuration field .
157
+ * Helper to build aNuxt Studio compatible configuration schema .
157
158
* Supports all type of fields provided by Nuxt Studio and all fields supported from Untyped Schema interface.
158
159
*/
159
- export function field (
160
- type : keyof typeof supportedFields | StudioFieldData ,
161
- defaultValue ?: any
162
- ) : InputValue {
163
- // Custom `type` field should get overwritten by native Schema ones at this stage
164
- const result = defu (
165
- supportedFields [ typeof type === 'string' ? type : type . type ] ,
166
- type
167
- ) as StudioFieldData
160
+ export function field ( schema : StudioFieldData ) : InputValue {
161
+ if ( ! schema . type ) {
162
+ throw new Error ( `Missing type in schema ${ JSON . stringify ( schema ) } ` )
163
+ }
168
164
169
- // Init tags
170
- if ( ! result . tags ) { result . tags = [ ] }
165
+ // copy of supportedFields
166
+ const base = JSON . parse ( JSON . stringify ( supportedFields [ schema . type ] ) )
167
+ const result = defu ( base , schema )
171
168
172
- // Cast `icon` into its tag
169
+ if ( ! result . tags ) {
170
+ result . tags = [ ]
171
+ }
173
172
if ( result . icon ) {
174
173
result . tags . push ( `@studioIcon ${ result . icon } ` )
175
174
delete result . icon
176
175
}
176
+ return {
177
+ $schema : result
178
+ }
179
+ }
180
+
181
+ export function group ( schema : StudioFieldData ) : InputValue {
182
+ const result = { ...schema }
183
+
184
+ if ( result . icon ) {
185
+ result . tags = [ `@studioIcon ${ result . icon } ` ]
186
+ delete result . icon
187
+ }
177
188
178
- // Cast default value passed as 2nd parameter
179
- if ( defaultValue ) {
180
- result . default = defaultValue
189
+ const fields : Record < string , InputValue > = { }
190
+ if ( result . fields ) {
191
+ for ( const key of Object . keys ( result . fields ) ) {
192
+ fields [ key ] = result . fields [ key ]
193
+ }
194
+ delete result . fields
181
195
}
182
196
183
197
return {
184
- $schema : result
198
+ $schema : result ,
199
+ ...fields
185
200
}
186
201
}
0 commit comments