Skip to content

Commit 98bb1df

Browse files
[refactor] introduce frontend type augmentation pattern (#4192)
1 parent 75077fe commit 98bb1df

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/composables/widgets/useImageUploadWidget.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { ResultItem } from '@/schemas/apiSchema'
99
import type { InputSpec } from '@/schemas/nodeDefSchema'
1010
import type { ComfyWidgetConstructor } from '@/scripts/widgets'
1111
import { useNodeOutputStore } from '@/stores/imagePreviewStore'
12+
import { isImageUploadInput } from '@/types/nodeDefAugmentation'
1213
import { createAnnotatedPath } from '@/utils/formatUtil'
1314
import { addToComboValues } from '@/utils/litegraphUtil'
1415

@@ -33,7 +34,13 @@ export const useImageUploadWidget = () => {
3334
inputName: string,
3435
inputData: InputSpec
3536
) => {
36-
const inputOptions = inputData[1] ?? {}
37+
if (!isImageUploadInput(inputData)) {
38+
throw new Error(
39+
'Image upload widget requires imageInputName augmentation'
40+
)
41+
}
42+
43+
const inputOptions = inputData[1]
3744
const { imageInputName, allow_batch, image_folder = 'input' } = inputOptions
3845
const nodeOutputStore = useNodeOutputStore()
3946

@@ -43,11 +50,9 @@ export const useImageUploadWidget = () => {
4350
const { showPreview } = isVideo ? useNodeVideo(node) : useNodeImage(node)
4451

4552
const fileFilter = isVideo ? isVideoFile : isImageFile
46-
// @ts-expect-error InputSpec is not typed correctly
4753
const fileComboWidget = findFileComboWidget(node, imageInputName)
4854
const initialFile = `${fileComboWidget.value}`
4955
const formatPath = (value: InternalFile) =>
50-
// @ts-expect-error InputSpec is not typed correctly
5156
createAnnotatedPath(value, { rootFolder: image_folder })
5257

5358
const transform = (internalValue: InternalValue): ExposedValue => {
@@ -67,7 +72,6 @@ export const useImageUploadWidget = () => {
6772

6873
// Setup file upload handling
6974
const { openFileSelection } = useNodeImageUpload(node, {
70-
// @ts-expect-error InputSpec is not typed correctly
7175
allow_batch,
7276
fileFilter,
7377
accept,

src/types/nodeDefAugmentation.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Frontend augmentations for node definitions.
3+
*
4+
* This module defines type extensions that augment the backend node definition
5+
* types with frontend-specific properties. These augmentations are applied at
6+
* runtime and are not part of the backend API contract.
7+
*/
8+
import type { ComboInputOptions, InputSpec } from '@/schemas/nodeDefSchema'
9+
10+
/**
11+
* Frontend augmentation for image upload combo inputs.
12+
* This extends ComboInputOptions with properties injected by the uploadImage extension.
13+
*/
14+
export interface ImageUploadComboOptions extends ComboInputOptions {
15+
/**
16+
* Reference to the associated filename combo widget.
17+
* Injected by uploadImage.ts to link upload buttons with their combo widgets.
18+
*
19+
* @remarks This property exists only in the frontend runtime.
20+
*/
21+
imageInputName: string
22+
}
23+
24+
/**
25+
* Type guard to check if an InputSpec has image upload augmentations.
26+
* Narrows from base InputSpec to augmented type.
27+
*/
28+
export function isImageUploadInput(
29+
inputData: InputSpec
30+
): inputData is [string, ImageUploadComboOptions] {
31+
const options = inputData[1]
32+
return (
33+
options !== undefined &&
34+
typeof options === 'object' &&
35+
'imageInputName' in options &&
36+
typeof options.imageInputName === 'string'
37+
)
38+
}

0 commit comments

Comments
 (0)