import {z} from 'zod' export type GetApiApiOrgIdWorkflowsArgs = {org_id: string, folder?: string | undefined, all_folders?: boolean | undefined, search_substring?: string | undefined}; export type GetApiApiOrgIdWorkflowsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowAssociation = z.object({created_at: z.number().int(), entity_id: z.string(), entity_type: z.string(), id: z.number().int(), org_id: z.string(), trigger_type: z.string(), workflow_id: z.string()}); export const edge = z.object({id: z.string(), source: z.string(), source_handle: z.string().nullable().optional(), target: z.string()}); export const position = z.object({x: z.number(), y: z.number()}); export const nodeStyle = z.object({backgroundColor: z.string().nullable().optional()}); export const node = z.object({data: z.object({}), id: z.string(), io_type: z.string(), is_disabled: z.boolean().optional(), meta: z.record(z.string(), z.string()).nullable().optional(), position: position, style: nodeStyle.nullable().optional()}); export const workflowListItem = z.object({associations: z.array(workflowAssociation), created_at: z.number().int(), created_by: z.string(), description: z.string(), edges: z.array(edge), enabled: z.boolean(), folder_id: z.string().nullable().optional(), folder_name: z.string().nullable().optional(), id: z.string(), is_draft: z.boolean(), name: z.string(), nodes: z.array(node), org_id: z.string(), updated_at: z.number().int()}); export const getApiApiOrgIdWorkflowsResponse = z.array(workflowListItem); 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 organization workflows * * GET /api/{org_id}/workflows */ export const getApiApiOrgIdWorkflows = async (args: GetApiApiOrgIdWorkflowsArgs, options: GetApiApiOrgIdWorkflowsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.folder != null) query.append("folder", String(args.folder)) if (args.all_folders != null) query.append("all_folders", String(args.all_folders)) if (args.search_substring != null) query.append("search_substring", String(args.search_substring)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/workflows${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(getApiApiOrgIdWorkflowsResponse, await res.json()) };