Files
windmill/frontend/src/lib/schema.ts
T
Ruben Fiszel a8ef98edff feat: build a React raw app from the script and flow detail pages (#10337)
* feat: build a React raw app from the script and flow detail pages

* fix: keep generated raw-app state and setter names unique

* fix: make the generated app readable on dark and handle labeled enums

* fix: reserve the undefined binding in generated raw apps

* fix: mask password args, keep __proto__ args, and enforce required inputs

* fix: quote non-identifier arg names, preserve JSX entities, support resource args

* fix: JSON-quote generated arg keys and start resource fields empty

* fix: render array enums as multi-selects and let optional enums be omitted

* fix: stop the generated template naming Math/Array and omit untouched optional json

* fix: enforce required on array-enum multiselects
2026-07-27 12:53:43 +02:00

55 lines
1.6 KiB
TypeScript

import type { Schema, SchemaProperty } from './common'
export function schemaToTsType(schema: Schema | SchemaProperty): string {
const schemaProperties = schema.properties
const schemaRequired = schema.required
if (!schema || !schemaProperties) {
return 'any'
}
const propKeys = Object.keys(schemaProperties)
const types = propKeys
.map((key: string) => {
const prop = schemaProperties[key]
const isOptional = !schemaRequired?.includes(key)
// Flow inputs allow names TS cannot use bare, e.g. `user-name`, which
// would emit an unparseable member. A quoted key means the same thing.
const name = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : JSON.stringify(key)
const prefix = `${name}${isOptional ? '?' : ''}`
let type: string = 'any'
if (prop.type === 'string') {
type = 'string'
} else if (prop.type === 'number' || prop.type === 'integer') {
type = 'number'
} else if (prop.type === 'boolean') {
type = 'boolean'
} else if (prop.type === 'array') {
type = prop.items?.type ?? 'any'
if (type === 'integer') {
type = 'number'
}
type = `${type}[]`
} else if (prop.type === 'object' && prop.properties) {
type = schemaToTsType(prop)
}
return `${prefix}: ${type}`
})
.join('; ')
return `{ ${types} }`
}
export function schemaToObject(schema: Schema, args: Record<string, any>): Object {
const object = {}
if (!schema || !schema.properties) {
return object
}
const propKeys = Object.keys(schema.properties)
propKeys.forEach((key: string) => {
object[key] = args[key] ?? null
})
return object
}