@@ -148,39 +148,54 @@ const supportedFields: { [key in InputsTypes]: Schema } = {
148148export type StudioFieldData =
149149 PartialSchema &
150150 {
151- type : keyof typeof supportedFields
151+ type ? : keyof typeof supportedFields
152152 icon ?: string
153+ fields ?: { [ key : string ] : InputValue }
153154 }
154155
155156/**
156- * Declares a Nuxt Studio compatible configuration field .
157+ * Helper to build aNuxt Studio compatible configuration schema .
157158 * Supports all type of fields provided by Nuxt Studio and all fields supported from Untyped Schema interface.
158159 */
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+ }
168164
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 )
171168
172- // Cast `icon` into its tag
169+ if ( ! result . tags ) {
170+ result . tags = [ ]
171+ }
173172 if ( result . icon ) {
174173 result . tags . push ( `@studioIcon ${ result . icon } ` )
175174 delete result . icon
176175 }
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+ }
177188
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
181195 }
182196
183197 return {
184- $schema : result
198+ $schema : result ,
199+ ...fields
185200 }
186201}
0 commit comments