All files / web/src/utils schemaGenerator.ts

4.47% Statements 3/67
100% Branches 0/0
0% Functions 0/3
4.47% Lines 3/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                1x                                                                                                                                                       1x                       1x      
/**
 * Schema Generator Utility
 *
 * Generates VariableField structures from example output values.
 * Uses backend-generated types (via ts-rs) as the source of truth.
 */
 
import type { VariableField } from '@/composables/variables/useAvailableVariables'
import { NODE_OUTPUT_EXAMPLES } from '@/constants/node/output-examples'
 
/**
 * Parse a JSON value into VariableField structure
 * Recursively processes objects and arrays to build field hierarchy
 */
function parseValueToFields(value: any, basePath: string): VariableField[] {
  if (value === null || value === undefined) {
    return []
  }
 
  if (typeof value !== 'object') {
    return [{
      name: '',
      type: typeof value,
      path: basePath,
      value
    }]
  }
 
  if (Array.isArray(value)) {
    const itemFields: VariableField[] = []
 
    // For arrays, show first item as example
    if (value.length > 0) {
      const firstItem = value[0]
      const children = parseValueToFields(firstItem, `${basePath}[0]`)
 
      itemFields.push({
        name: '[0]',
        type: 'array-item',
        path: `${basePath}[0]`,
        value: firstItem,
        children
      })
    }
 
    return [{
      name: '',
      type: 'array',
      path: basePath,
      value,
      children: itemFields
    }]
  }
 
  const fields: VariableField[] = []
  for (const [key, val] of Object.entries(value)) {
    const fieldPath = basePath ? `${basePath}.${key}` : key
 
    if (typeof val === 'object' && val !== null) {
      const children = parseValueToFields(val, fieldPath)
      fields.push({
        name: key,
        type: Array.isArray(val) ? 'array' : 'object',
        path: fieldPath,
        value: val,
        children
      })
    } else {
      fields.push({
        name: key,
        type: typeof val,
        path: fieldPath,
        value: val
      })
    }
  }
 
  return fields
}
 
/**
 * Get output schema for a node type based on example data
 * Returns VariableField array with structure and example values
 */
export function getNodeOutputSchema(nodeType: string): VariableField[] {
  const example = NODE_OUTPUT_EXAMPLES[nodeType]
  if (!example) {
    return []
  }
 
  return parseValueToFields(example, '')
}
 
/**
 * Check if a node type has an output schema defined
 */
export function hasNodeOutputSchema(nodeType: string): boolean {
  return nodeType in NODE_OUTPUT_EXAMPLES
}