Line data Source code
1 : import { zAssetInfo, zHubProfile } from '@comfyorg/ingest-types/zod'
2 : import { z } from 'zod'
3 :
4 2355 : const zComfyHubProfile = zHubProfile
5 : .pick({ username: true, description: true })
6 : .extend({
7 : name: zHubProfile.shape.display_name,
8 : coverImageUrl: z.string().nullish(),
9 : profilePictureUrl: zHubProfile.shape.avatar_url.nullish()
10 : })
11 :
12 : export type ComfyHubProfile = z.infer<typeof zComfyHubProfile>
13 :
14 2355 : export const zPublishRecordResponse = z.object({
15 : workflow_id: z.string(),
16 : share_id: z.string().nullable(),
17 : listed: z.boolean(),
18 : publish_time: z.string().nullable(),
19 : assets: z.array(zAssetInfo).optional()
20 : })
21 :
22 : /**
23 : * Strips path separators and control characters from a workflow name to prevent
24 : * path traversal when the name is later used as part of a file path.
25 : */
26 2 : function sanitizeWorkflowName(name: string): string {
27 2 : return name
28 : .replaceAll(/[/\\:]/g, '_')
29 : .slice(0, 200)
30 : .trim()
31 : }
32 :
33 2355 : export const zSharedWorkflowResponse = z.object({
34 : share_id: z.string(),
35 : workflow_id: z.string(),
36 : name: z.string().transform(sanitizeWorkflowName),
37 : listed: z.boolean(),
38 : publish_time: z.string().nullable(),
39 : workflow_json: z.record(z.string(), z.unknown()),
40 : assets: z.array(zAssetInfo)
41 : })
42 :
43 2355 : export const zHubProfileResponse = z.preprocess((data) => {
44 27 : if (!data || typeof data !== 'object') return data
45 27 : const d = data as Record<string, unknown>
46 27 : return {
47 : username: d.username,
48 : name: d.name ?? d.display_name,
49 : description: d.description,
50 : coverImageUrl: d.coverImageUrl ?? d.cover_image_url,
51 : profilePictureUrl:
52 : d.profilePictureUrl ?? d.profile_picture_url ?? d.avatar_url
53 : }
54 : }, zComfyHubProfile)
55 :
56 2355 : export const zHubAssetUploadUrlResponse = z
57 : .object({
58 : upload_url: z.string(),
59 : public_url: z.string(),
60 : token: z.string()
61 : })
62 : .transform((response) => ({
63 : uploadUrl: response.upload_url,
64 : publicUrl: response.public_url,
65 : token: response.token
66 : }))
67 :
68 2355 : export const zHubWorkflowPublishResponse = z.object({
69 : share_id: z.string(),
70 : workflow_id: z.string(),
71 : thumbnail_type: z.enum(['image', 'video', 'image_comparison']).optional()
72 : })
73 :
74 2355 : const zHubLabelInfo = z.object({
75 : name: z.string(),
76 : display_name: z.string(),
77 : type: z.enum(['tag', 'model', 'custom_node'])
78 : })
79 :
80 2355 : export const zHubLabelListResponse = z.object({
81 : labels: z.array(zHubLabelInfo)
82 : })
|