|
| 1 | +import { |
| 2 | + ConfigurableProps, V1Component, |
| 3 | +} from "@pipedream/sdk"; |
| 4 | +import { |
| 5 | + z, ZodRawShape, |
| 6 | +} from "zod"; |
| 7 | +import { |
| 8 | + extractEnumValues, toNonEmptyTuple, |
| 9 | +} from "./lib/helpers"; |
| 10 | + |
| 11 | +export const configurablePropsToZod = ( |
| 12 | + component: V1Component, |
| 13 | + options?: { |
| 14 | + asyncOptionsDescription?: string; |
| 15 | + configureComponentDescription?: string; |
| 16 | + configurableProps?: ConfigurableProps; |
| 17 | + }, |
| 18 | +) => { |
| 19 | + const schema: ZodRawShape = {}; |
| 20 | + |
| 21 | + for (const cp of options?.configurableProps || |
| 22 | + (component.configurable_props as ConfigurableProps)) { |
| 23 | + if (cp.hidden) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + if (cp.type === "app") { |
| 28 | + // XXX handle directly in implementation |
| 29 | + continue; |
| 30 | + } else if (cp.type === "string") { |
| 31 | + if (cp.options && Array.isArray(cp.options) && cp.options.length > 0) { |
| 32 | + const enumValues = toNonEmptyTuple(extractEnumValues(cp.options)); |
| 33 | + if (enumValues) { |
| 34 | + schema[cp.name] = z.enum(enumValues); |
| 35 | + } else { |
| 36 | + schema[cp.name] = z.string(); |
| 37 | + } |
| 38 | + } else { |
| 39 | + schema[cp.name] = z.string(); |
| 40 | + } |
| 41 | + } else if (cp.type === "string[]") { |
| 42 | + if (cp.options && Array.isArray(cp.options) && cp.options.length > 0) { |
| 43 | + const enumValues = toNonEmptyTuple(extractEnumValues(cp.options)); |
| 44 | + if (enumValues) { |
| 45 | + schema[cp.name] = z.array(z.enum(enumValues)); |
| 46 | + } else { |
| 47 | + schema[cp.name] = z.array(z.string()); |
| 48 | + } |
| 49 | + } else { |
| 50 | + schema[cp.name] = z.array(z.string()); |
| 51 | + } |
| 52 | + } else if (cp.type === "$.discord.channel") { |
| 53 | + schema[cp.name] = z.string(); |
| 54 | + } else if (cp.type === "$.discord.channel[]") { |
| 55 | + schema[cp.name] = z.array(z.string()); |
| 56 | + } else if (cp.type === "object") { |
| 57 | + schema[cp.name] = z.object({}).passthrough(); |
| 58 | + } else if (cp.type === "any") { |
| 59 | + schema[cp.name] = z.any(); |
| 60 | + } else if (cp.type === "integer") { |
| 61 | + schema[cp.name] = z.number().int(); |
| 62 | + } else if (cp.type === "integer[]") { |
| 63 | + schema[cp.name] = z.array(z.number().int()); |
| 64 | + } else if (cp.type === "boolean") { |
| 65 | + schema[cp.name] = z.boolean(); |
| 66 | + } else if (cp.type === "boolean[]") { |
| 67 | + schema[cp.name] = z.array(z.boolean()); |
| 68 | + // ignore alerts, as no user input required |
| 69 | + } else { |
| 70 | + console.error("unhandled type. Skipping", cp.name, cp.type); |
| 71 | + } |
| 72 | + |
| 73 | + if (schema[cp.name]) { |
| 74 | + if (cp.optional) { |
| 75 | + schema[cp.name] = schema[cp.name].optional().nullable(); |
| 76 | + } |
| 77 | + |
| 78 | + let description: string = cp.description || ""; |
| 79 | + |
| 80 | + if (cp.hidden) { |
| 81 | + description += |
| 82 | + "\n\nIMPORTANT: This property is hidden. Do not configure it and leave it blank.\n"; |
| 83 | + } |
| 84 | + |
| 85 | + if (cp.remoteOptions) { |
| 86 | + if (options?.asyncOptionsDescription) { |
| 87 | + if (options.configureComponentDescription) { |
| 88 | + description += `\n\n${options.asyncOptionsDescription}`; |
| 89 | + } |
| 90 | + } else { |
| 91 | + if (options?.configureComponentDescription) { |
| 92 | + description += `\n\n${options.configureComponentDescription}`; |
| 93 | + } |
| 94 | + } |
| 95 | + // if (cp.name.includes("id")) { |
| 96 | + // description += `\n\nIMPORTANT: An ID is required for this property. If you don't have the id and only have the name, use the "${CONFIGURE_COMPONENT_TOOL_NAME}" tool to get the values.`; |
| 97 | + // } |
| 98 | + } |
| 99 | + |
| 100 | + if (description.trim()) { |
| 101 | + schema[cp.name] = schema[cp.name].describe(description.trim()); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return schema; |
| 107 | +}; |
0 commit comments