export const processMarketplaceSchemaResponse = (rawEntry: any, nameKey: string) => { const roles: string[] = rawEntry.roles || []; const roleName = roles.join(", "); const schemas = rawEntry.schemas || []; const processSchema = (schema: any) => { const parameters: any[] = []; const resources: any[] = []; const channels: any[] = []; const configProps = schema.properties?.config?.properties; if (configProps) { Object.entries(configProps).forEach(([key, value]: [string, any]) => { parameters.push({ name: key, type: value.type || "string", required: schema.properties?.config?.required?.includes(key) || false, defaultValue: value.default ?? value.enum?.[0] ?? value.const ?? undefined, }); }); } const volumeTypes = ["Registered", "NonReplicated", "Persisted", "Volatile", "Ephemeral", "Persistent"]; const isVolumeResource = (v: any): boolean => { if (!v.oneOf) return false; return v.oneOf.some((o: any) => { if (o.oneOf) return o.oneOf.some((n: any) => volumeTypes.includes(n?.properties?.$kdsl?.const?.NamedType?.Name)); return volumeTypes.includes(o?.properties?.$kdsl?.const?.NamedType?.Name); }); }; const resourceProps = schema.properties?.resource; if (resourceProps?.properties) { Object.entries(resourceProps.properties).forEach(([key, value]: [string, any]) => { if (isVolumeResource(value)) { resources.push({ name: key, type: "volume", required: resourceProps.required?.includes(key) || false, }); } else { const typeName = value.properties?.["$kdsl"]?.const?.NamedType?.Name; const resourceType = typeName ? typeName.toLowerCase() : Object.keys(value.properties || {})[0]; const inner = value.properties?.[resourceType] || value.properties?.inner; const defaultValue = inner?.default !== undefined ? String(inner.default) : inner?.enum?.[0] ? String(inner.enum[0]) : undefined; resources.push({ name: key, type: resourceType || "string", required: resourceProps.required?.includes(key) || false, defaultValue, }); } }); } const srvProps = schema.properties?.srv?.properties; if (srvProps) { (["client", "server", "duplex"] as const).forEach((channelType) => { if (srvProps[channelType]?.properties) { Object.entries(srvProps[channelType].properties).forEach(([channelName, channelData]: [string, any]) => { const port = channelData.properties?.port?.const || channelData.properties?.port?.enum?.[0]; const protocol = channelData.properties?.protocol?.const || channelData.properties?.protocol?.enum?.[0]; channels.push({ name: channelName, type: channelType, resource: port && protocol ? { type: "port", value: `${protocol}:${port}` } : undefined, }); }); } }); } return { parameters, resources, channels }; }; if (schemas.length > 0 && schemas[0].oneOf) { const processedVariants: any[] = []; for (const schemaRef of schemas[0].oneOf) { const refKey = schemaRef.$ref?.replace("#/$defs/", ""); const actualSchema = schemas[0].$defs?.[refKey]; if (actualSchema) { const result = processSchema(actualSchema); processedVariants.push({ name: nameKey, roleName, roles, variant: refKey, ...result }); } } return { parameters: processedVariants[0]?.parameters || [], resources: processedVariants[0]?.resources || [], channels: processedVariants[0]?.channels || [], name: nameKey, roleName, roles, hasVariants: true, variants: processedVariants, }; } if (schemas[0]) { const result = processSchema(schemas[0]); return { ...result, name: nameKey, roleName, roles, hasVariants: false }; } return { parameters: [], resources: [], channels: [], name: nameKey, roleName, roles, hasVariants: false }; };