From 7d4050de05f9ce01523b6b4848d46f3c3ef88d3e Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Sun, 4 May 2025 01:08:28 +1000 Subject: [PATCH] [Refactor] Generic TS type dedupe --- src/schemas/comfyWorkflowSchema.ts | 70 ++++++++++++------------------ 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/src/schemas/comfyWorkflowSchema.ts b/src/schemas/comfyWorkflowSchema.ts index 62c75d4f8..4f14e4cf3 100644 --- a/src/schemas/comfyWorkflowSchema.ts +++ b/src/schemas/comfyWorkflowSchema.ts @@ -315,7 +315,7 @@ interface ComfyWorkflow1BaseType { state: z.infer } -/** Required for recursive definition of subgraphs. */ +/** Required for recursive definition of subgraphs w/ZodEffects. */ interface ComfyWorkflow1BaseInput extends ComfyWorkflow1BaseType { groups?: z.input[] nodes: z.input[] @@ -323,11 +323,11 @@ interface ComfyWorkflow1BaseInput extends ComfyWorkflow1BaseType { floatingLinks?: z.input[] reroutes?: z.input[] definitions?: { - subgraphs: SubgraphDefinitionBaseInput[] + subgraphs: SubgraphDefinitionBase[] } } -/** Required for recursive definition of subgraphs. */ +/** Required for recursive definition of subgraphs w/ZodEffects. */ interface ComfyWorkflow1BaseOutput extends ComfyWorkflow1BaseType { groups?: z.output[] nodes: z.output[] @@ -335,19 +335,10 @@ interface ComfyWorkflow1BaseOutput extends ComfyWorkflow1BaseType { floatingLinks?: z.output[] reroutes?: z.output[] definitions?: { - subgraphs: SubgraphDefinitionBaseOutput[] + subgraphs: SubgraphDefinitionBase[] } } -/** Required for recursive definition of subgraphs. */ -type SubgraphDefinitionInput = z.input & - ComfyWorkflow1BaseInput & - SubgraphDefinitionBaseInput -/** Required for recursive definition of subgraphs. */ -type SubgraphDefinitionOutput = z.output & - ComfyWorkflow1BaseOutput & - SubgraphDefinitionBaseOutput - /** Schema version 1 */ export const zComfyWorkflow1 = zBaseExportableGraph .extend({ @@ -367,9 +358,9 @@ export const zComfyWorkflow1 = zBaseExportableGraph subgraphs: z.lazy( (): z.ZodArray< z.ZodType< - SubgraphDefinitionOutput, + SubgraphDefinitionBase, z.ZodTypeDef, - SubgraphDefinitionInput + SubgraphDefinitionBase >, 'many' > => z.array(zSubgraphDefinition) @@ -389,41 +380,34 @@ export const zExposedWidget = z.object({ name: z.string() }) -interface SubgraphDefinitionBaseInput extends ComfyWorkflow1BaseInput { +interface SubgraphDefinitionBase< + T extends ComfyWorkflow1BaseInput | ComfyWorkflow1BaseOutput +> { /** Unique graph ID. Automatically generated if not provided. */ id: string revision: number name: string - inputNode: z.input - outputNode: z.input + inputNode: T extends ComfyWorkflow1BaseInput + ? z.input + : z.output + outputNode: T extends ComfyWorkflow1BaseInput + ? z.input + : z.output /** Ordered list of inputs to the subgraph itself. Similar to a reroute, with the input side in the graph, and the output side in the subgraph. */ - inputs?: z.input[] + inputs?: T extends ComfyWorkflow1BaseInput + ? z.input[] + : z.output[] /** Ordered list of outputs from the subgraph itself. Similar to a reroute, with the input side in the subgraph, and the output side in the graph. */ - outputs?: z.input[] + outputs?: T extends ComfyWorkflow1BaseInput + ? z.input[] + : z.output[] /** A list of node widgets displayed in the parent graph, on the subgraph object. */ - widgets?: z.input[] + widgets?: T extends ComfyWorkflow1BaseInput + ? z.input[] + : z.output[] definitions?: { - subgraphs: SubgraphDefinitionBaseInput[] - } -} - -interface SubgraphDefinitionBaseOutput extends ComfyWorkflow1BaseOutput { - /** Unique graph ID. Automatically generated if not provided. */ - id: string - revision: number - name: string - - inputNode: z.output - outputNode: z.output - /** Ordered list of inputs to the subgraph itself. Similar to a reroute, with the input side in the graph, and the output side in the subgraph. */ - inputs?: z.output[] - /** Ordered list of outputs from the subgraph itself. Similar to a reroute, with the input side in the subgraph, and the output side in the graph. */ - outputs?: z.output[] - /** A list of node widgets displayed in the parent graph, on the subgraph object. */ - widgets?: z.output[] - definitions?: { - subgraphs: SubgraphDefinitionBaseOutput[] + subgraphs: SubgraphDefinitionBase[] } } @@ -447,9 +431,9 @@ export const zSubgraphDefinition = zComfyWorkflow1 subgraphs: z.lazy( (): z.ZodArray< z.ZodType< - SubgraphDefinitionOutput, + SubgraphDefinitionBase, z.ZodTypeDef, - SubgraphDefinitionInput + SubgraphDefinitionBase >, 'many' > => zSubgraphDefinition.array()