import {z} from 'zod' export type NodeVersionStatus = 'NodeVersionStatusActive' | 'NodeVersionStatusDeleted' | 'NodeVersionStatusBanned' | 'NodeVersionStatusPending' | 'NodeVersionStatusFlagged'; export type GetApiVersionsArgs = {nodeId?: string | undefined, statuses?: Array | undefined, include_status_reason?: boolean | undefined, page?: number | undefined, pageSize?: number | undefined, status_reason?: string | undefined}; export type GetApiVersionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nodeVersionStatus = z.enum(["NodeVersionStatusActive", "NodeVersionStatusDeleted", "NodeVersionStatusBanned", "NodeVersionStatusPending", "NodeVersionStatusFlagged"]); export const nodeVersion = z.object({changelog: z.string().optional(), comfy_node_extract_status: z.string().optional(), createdAt: z.string().optional(), dependencies: z.array(z.string()).optional(), deprecated: z.boolean().optional(), downloadUrl: z.string().optional(), id: z.string().optional(), node_id: z.string().optional(), status: nodeVersionStatus.optional(), status_reason: z.string().optional(), supported_accelerators: z.array(z.string()).optional(), supported_comfyui_frontend_version: z.string().optional(), supported_comfyui_version: z.string().optional(), supported_os: z.array(z.string()).optional(), tags: z.array(z.string()).optional(), tags_admin: z.array(z.string()).optional(), version: z.string().optional()}); export const getApiVersionsResponse = z.object({page: z.number().int().optional(), pageSize: z.number().int().optional(), total: z.number().int().optional(), totalPages: z.number().int().optional(), versions: z.array(nodeVersion).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List all node versions given some filters. * * GET /versions */ export const getApiVersions = async (args: GetApiVersionsArgs, options?: GetApiVersionsOptions) => { const {baseUrl = "https://api.comfy.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.nodeId != null) query.append("nodeId", String(args.nodeId)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) if (args.include_status_reason != null) query.append("include_status_reason", String(args.include_status_reason)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.status_reason != null) query.append("status_reason", String(args.status_reason)) const url = `${baseUrl.replace(/\/+$/, '')}/versions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiVersionsResponse, await res.json()) };