mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-22 23:39:45 +00:00
refactor: improve type safety patterns from @ts-expect-error cleanup
Amp-Thread-ID: https://ampcode.com/threads/T-019bb3bd-f607-735a-b1a8-fce5fe4f0125 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
58
browser_tests/fixtures/utils/subgraphUtils.ts
Normal file
58
browser_tests/fixtures/utils/subgraphUtils.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Represents a subgraph's graph object with inputs and outputs slots.
|
||||
*/
|
||||
export interface SubgraphGraph {
|
||||
inputs: SubgraphSlot[]
|
||||
outputs: SubgraphSlot[]
|
||||
}
|
||||
|
||||
export interface SubgraphSlot {
|
||||
label?: string
|
||||
name?: string
|
||||
displayName?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard to check if a graph object is a subgraph.
|
||||
*/
|
||||
export function isSubgraph(graph: unknown): graph is SubgraphGraph {
|
||||
return (
|
||||
graph !== null &&
|
||||
typeof graph === 'object' &&
|
||||
'inputs' in graph &&
|
||||
'outputs' in graph &&
|
||||
Array.isArray((graph as SubgraphGraph).inputs) &&
|
||||
Array.isArray((graph as SubgraphGraph).outputs)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Assertion function that throws if the graph is not a subgraph.
|
||||
*/
|
||||
export function assertSubgraph(
|
||||
graph: unknown,
|
||||
message = 'Not in subgraph'
|
||||
): asserts graph is SubgraphGraph {
|
||||
if (!isSubgraph(graph)) {
|
||||
throw new Error(message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inline assertion for use inside page.evaluate() browser context.
|
||||
* Returns a string that can be used with Function constructor or eval.
|
||||
*/
|
||||
export const SUBGRAPH_ASSERT_INLINE = `
|
||||
const assertSubgraph = (graph) => {
|
||||
if (
|
||||
graph === null ||
|
||||
typeof graph !== 'object' ||
|
||||
!('inputs' in graph) ||
|
||||
!('outputs' in graph) ||
|
||||
!Array.isArray(graph.inputs) ||
|
||||
!Array.isArray(graph.outputs)
|
||||
) {
|
||||
throw new Error('Not in subgraph');
|
||||
}
|
||||
};
|
||||
`
|
||||
Reference in New Issue
Block a user