import {z} from 'zod' export type GetApiEnsemblesEmNameWorkflowsArgs = {query?: string | undefined, order?: string | undefined, 'start-index'?: number | undefined, 'max-results'?: number | undefined}; export type GetApiEnsemblesEmNameWorkflowsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const workflow = z.object({id: z.number().int(), name: z.string(), created: z.string(), updated: z.string(), state: z.enum(["READY", "PLANNING", "PLAN_FAILED", "QUEUED", "RUN_FAILED", "RUNNING", "FAILED", "SUCCESSFUL", "ABORTED"]), priority: z.number().int(), wf_uuid: z.string()}); export const getApiEnsemblesEmNameWorkflowsResponse = z.object({records: z.array(workflow), total: z.number().int(), filtered: z.number().int()}); 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 workflows belonging to the given ensemble * * GET /ensembles/{em-name}/workflows */ export const getApiEnsemblesEmNameWorkflows = async (args: GetApiEnsemblesEmNameWorkflowsArgs, options?: GetApiEnsemblesEmNameWorkflowsOptions) => { const {baseUrl = "http://localhost:5000/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.order != null) query.append("order", String(args.order)) if (args['start-index'] != null) query.append("start-index", String(args['start-index'])) if (args['max-results'] != null) query.append("max-results", String(args['max-results'])) const url = `${baseUrl.replace(/\/+$/, '')}/ensembles/${encodeURIComponent(String(args['em-name']))}/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(getApiEnsemblesEmNameWorkflowsResponse, await res.json()) };