LCOV - code coverage report
Current view: top level - src/platform/workflow/sharing/services - comfyHubService.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 59.6 % 52 31
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { ComfyHubProfile } from '@/platform/workflow/sharing/schemas/shareSchemas'
       2              : import {
       3              :   zHubAssetUploadUrlResponse,
       4              :   zHubLabelListResponse,
       5              :   zHubProfileResponse,
       6              :   zHubWorkflowPublishResponse
       7              : } from '@/platform/workflow/sharing/schemas/shareSchemas'
       8              : import { api } from '@/scripts/api'
       9              : 
      10              : type HubThumbnailType = 'image' | 'video' | 'image_comparison'
      11              : 
      12              : type ThumbnailTypeInput = HubThumbnailType | 'imageComparison'
      13              : 
      14              : interface CreateProfileInput {
      15              :   workspaceId: string
      16              :   username: string
      17              :   displayName?: string
      18              :   description?: string
      19              :   avatarToken?: string
      20              : }
      21              : 
      22              : interface PublishWorkflowInput {
      23              :   username: string
      24              :   name: string
      25              :   workflowFilename: string
      26              :   assetIds: string[]
      27              :   description?: string
      28              :   tags?: string[]
      29              :   models?: string[]
      30              :   customNodes?: string[]
      31              :   thumbnailType?: ThumbnailTypeInput
      32              :   thumbnailTokenOrUrl?: string
      33              :   thumbnailComparisonTokenOrUrl?: string
      34              :   sampleImageTokensOrUrls?: string[]
      35              :   tutorialUrl?: string
      36              :   metadata?: Record<string, unknown>
      37              : }
      38              : 
      39            3 : function normalizeThumbnailType(type: ThumbnailTypeInput): HubThumbnailType {
      40            3 :   if (type === 'imageComparison') {
      41            0 :     return 'image_comparison'
      42              :   }
      43              : 
      44            3 :   return type
      45              : }
      46              : 
      47            1 : async function parseErrorMessage(
      48              :   response: Response,
      49              :   fallbackMessage: string
      50              : ): Promise<string> {
      51            1 :   const body = await response.json().catch(() => null)
      52            1 :   if (!body || typeof body !== 'object') {
      53            0 :     return fallbackMessage
      54              :   }
      55              : 
      56            1 :   if ('message' in body && typeof body.message === 'string') {
      57            1 :     return body.message
      58              :   }
      59              : 
      60            0 :   return fallbackMessage
      61              : }
      62              : 
      63           62 : async function parseRequiredJson<T>(
      64              :   response: Response,
      65              :   parser: {
      66              :     safeParse: (
      67              :       value: unknown
      68              :     ) => { success: true; data: T } | { success: false }
      69              :   },
      70              :   fallbackMessage: string
      71              : ): Promise<T> {
      72           62 :   const payload = await response.json().catch(() => null)
      73           60 :   const parsed = parser.safeParse(payload)
      74           60 :   if (!parsed.success) {
      75            0 :     throw new Error(fallbackMessage)
      76              :   }
      77              : 
      78           60 :   return parsed.data
      79              : }
      80              : 
      81          153 : export function useComfyHubService() {
      82            0 :   async function requestAssetUploadUrl(input: {
      83              :     filename: string
      84              :     contentType: string
      85              :   }) {
      86            0 :     const response = await api.fetchApi('/hub/assets/upload-url', {
      87              :       method: 'POST',
      88              :       headers: { 'Content-Type': 'application/json' },
      89              :       body: JSON.stringify({
      90              :         filename: input.filename,
      91              :         content_type: input.contentType
      92              :       })
      93              :     })
      94              : 
      95            0 :     if (!response.ok) {
      96            0 :       throw new Error(
      97              :         await parseErrorMessage(response, 'Failed to request upload URL')
      98              :       )
      99              :     }
     100              : 
     101            0 :     return parseRequiredJson(
     102              :       response,
     103              :       zHubAssetUploadUrlResponse,
     104              :       'Invalid upload URL response from server'
     105              :     )
     106              :   }
     107              : 
     108            0 :   async function uploadFileToPresignedUrl(input: {
     109              :     uploadUrl: string
     110              :     file: File
     111              :     contentType: string
     112              :   }): Promise<void> {
     113            0 :     const response = await fetch(input.uploadUrl, {
     114              :       method: 'PUT',
     115              :       headers: {
     116              :         'Content-Type': input.contentType
     117              :       },
     118              :       body: input.file
     119              :     })
     120              : 
     121            0 :     if (!response.ok) {
     122            0 :       const message = await parseErrorMessage(
     123              :         response,
     124              :         'Failed to upload file to presigned URL'
     125              :       )
     126            0 :       throw new Error(message)
     127              :     }
     128              :   }
     129              : 
     130           30 :   async function getMyProfile(): Promise<ComfyHubProfile | null> {
     131           30 :     const response = await api.fetchApi('/hub/profiles/me')
     132              : 
     133           30 :     if (!response.ok) {
     134            2 :       if (response.status === 404) {
     135            2 :         return null
     136              :       }
     137              : 
     138            0 :       throw new Error(
     139              :         await parseErrorMessage(response, 'Failed to load ComfyHub profile')
     140              :       )
     141              :     }
     142              : 
     143           28 :     return parseRequiredJson(
     144              :       response,
     145              :       zHubProfileResponse,
     146              :       'Invalid profile response from server'
     147              :     )
     148              :   }
     149              : 
     150            0 :   async function createProfile(
     151              :     input: CreateProfileInput
     152              :   ): Promise<ComfyHubProfile> {
     153            0 :     const response = await api.fetchApi('/hub/profiles', {
     154              :       method: 'POST',
     155              :       headers: { 'Content-Type': 'application/json' },
     156              :       body: JSON.stringify({
     157              :         workspace_id: input.workspaceId,
     158              :         username: input.username,
     159              :         display_name: input.displayName,
     160              :         description: input.description,
     161              :         avatar_token: input.avatarToken
     162              :       })
     163              :     })
     164              : 
     165            0 :     if (!response.ok) {
     166            0 :       throw new Error(
     167              :         await parseErrorMessage(response, 'Failed to create ComfyHub profile')
     168              :       )
     169              :     }
     170              : 
     171            0 :     return parseRequiredJson(
     172              :       response,
     173              :       zHubProfileResponse,
     174              :       'Invalid profile response from server'
     175              :     )
     176              :   }
     177              : 
     178            3 :   async function publishWorkflow(input: PublishWorkflowInput) {
     179            3 :     const body = {
     180              :       username: input.username,
     181              :       name: input.name,
     182              :       workflow_filename: input.workflowFilename,
     183              :       asset_ids: input.assetIds,
     184              :       description: input.description,
     185              :       tags: input.tags,
     186              :       models: input.models,
     187              :       custom_nodes: input.customNodes,
     188              :       thumbnail_type: input.thumbnailType
     189              :         ? normalizeThumbnailType(input.thumbnailType)
     190              :         : undefined,
     191              :       thumbnail_token_or_url: input.thumbnailTokenOrUrl,
     192              :       thumbnail_comparison_token_or_url: input.thumbnailComparisonTokenOrUrl,
     193              :       sample_image_tokens_or_urls: input.sampleImageTokensOrUrls,
     194              :       tutorial_url: input.tutorialUrl,
     195              :       metadata: input.metadata
     196              :     }
     197              : 
     198            3 :     const response = await api.fetchApi('/hub/workflows', {
     199              :       method: 'POST',
     200              :       headers: { 'Content-Type': 'application/json' },
     201              :       body: JSON.stringify(body)
     202              :     })
     203              : 
     204            3 :     if (!response.ok) {
     205            1 :       throw new Error(
     206              :         await parseErrorMessage(response, 'Failed to publish workflow')
     207              :       )
     208              :     }
     209              : 
     210            2 :     return parseRequiredJson(
     211              :       response,
     212              :       zHubWorkflowPublishResponse,
     213              :       'Invalid publish response from server'
     214              :     )
     215              :   }
     216              : 
     217           32 :   async function fetchTagLabels(): Promise<string[]> {
     218           32 :     const response = await api.fetchApi('/hub/labels?type=tag')
     219              : 
     220           32 :     if (!response.ok) {
     221            0 :       throw new Error(
     222              :         await parseErrorMessage(response, 'Failed to fetch hub labels')
     223              :       )
     224              :     }
     225              : 
     226              :     const data = await parseRequiredJson(
     227              :       response,
     228              :       zHubLabelListResponse,
     229              :       'Invalid label list response from server'
     230              :     )
     231              : 
     232           32 :     return data.labels.map((label) => label.display_name)
     233              :   }
     234              : 
     235          153 :   return {
     236              :     requestAssetUploadUrl,
     237              :     uploadFileToPresignedUrl,
     238              :     getMyProfile,
     239              :     createProfile,
     240              :     publishWorkflow,
     241              :     fetchTagLabels
     242              :   }
     243              : }
        

Generated by: LCOV version 2.0-1