Skip to content

Commit

Permalink
Merge pull request #150 from nuxtlabs/feat/schema-generation
Browse files Browse the repository at this point in the history
  • Loading branch information
larbish authored Nov 9, 2023
2 parents 6bdc9ce + 4b74d49 commit aed4ae8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
12 changes: 9 additions & 3 deletions playground/nuxt.schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { field } from '../src/theme'
import { field, group } from '../src/theme'

export default defineNuxtSchema({
appConfig: {
someConfig: field({ type: 'string', default: 'schema default' }),
configFromNuxtSchema: field('boolean', true)
parent: group({
title: 'Parent',
description: 'Parent description',
fields: {
someConfig: field({ type: 'string', default: 'schema default' }),
configFromNuxtSchema: field({ type: 'boolean' })
}
})
}
})
6 changes: 6 additions & 0 deletions src/runtime/composables/useStudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export const useStudio = () => {

const syncPreviewAppConfig = (appConfig?: any) => {
const _appConfig = callWithNuxt(nuxtApp, useAppConfig)

// Set dynamic icons for preview if user is using @nuxt/ui
if (_appConfig?.ui) {
_appConfig.ui.icons = { ..._appConfig.ui.icons, dynamic: true }
}

// Using `defu` to merge with initial config
// This is important to revert to default values for missing properties
deepAssign(_appConfig, defu(appConfig, initialAppConfig))
Expand Down
51 changes: 33 additions & 18 deletions src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,39 +148,54 @@ const supportedFields: { [key in InputsTypes]: Schema } = {
export type StudioFieldData =
PartialSchema &
{
type: keyof typeof supportedFields
type?: keyof typeof supportedFields
icon?: string
fields?: { [key: string]: InputValue }
}

/**
* Declares a Nuxt Studio compatible configuration field.
* Helper to build aNuxt Studio compatible configuration schema.
* Supports all type of fields provided by Nuxt Studio and all fields supported from Untyped Schema interface.
*/
export function field (
type: keyof typeof supportedFields | StudioFieldData,
defaultValue?: any
): InputValue {
// Custom `type` field should get overwritten by native Schema ones at this stage
const result = defu(
supportedFields[typeof type === 'string' ? type : type.type],
type
) as StudioFieldData
export function field (schema: StudioFieldData): InputValue {
if (!schema.type) {
throw new Error(`Missing type in schema ${JSON.stringify(schema)}`)
}

// Init tags
if (!result.tags) { result.tags = [] }
// copy of supportedFields
const base = JSON.parse(JSON.stringify(supportedFields[schema.type]))
const result = defu(base, schema)

// Cast `icon` into its tag
if (!result.tags) {
result.tags = []
}
if (result.icon) {
result.tags.push(`@studioIcon ${result.icon}`)
delete result.icon
}
return {
$schema: result
}
}

export function group (schema: StudioFieldData): InputValue {
const result = { ...schema }

if (result.icon) {
result.tags = [`@studioIcon ${result.icon}`]
delete result.icon
}

// Cast default value passed as 2nd parameter
if (defaultValue) {
result.default = defaultValue
const fields: Record<string, InputValue> = {}
if (result.fields) {
for (const key of Object.keys(result.fields)) {
fields[key] = result.fields[key]
}
delete result.fields
}

return {
$schema: result
$schema: result,
...fields
}
}

0 comments on commit aed4ae8

Please sign in to comment.