import {z} from 'zod' export type GetApiRootMWfIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowstate = z.object({wf_id: z.number().int().optional(), state: z.string().optional(), status: z.number().int().optional(), restart_count: z.number().int().optional(), timestamp: z.number().optional()}); export const rootWorkflow = z.object({wf_id: z.number().int().optional(), wf_uuid: z.string().optional(), submit_hostname: z.string().optional(), submit_dir: z.string().optional(), planner_arguments: z.string().optional(), planner_version: z.string().optional(), user: z.string().optional(), grid_dn: z.string().optional(), dax_label: z.string().optional(), dax_version: z.string().optional(), dax_file: z.string().optional(), dag_file_name: z.string().optional(), timestamp: z.number().optional(), workflow_state: workflowstate.optional(), archived: z.boolean().optional()}); export const getApiRootMWfIdResponse = z.array(rootWorkflow); 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 }; /** * Retrieve a root workflow * * GET /root/{m-wf-id} */ export const getApiRootMWfId = async (options?: GetApiRootMWfIdOptions) => { const {baseUrl = "https://localhost:5000/api/v1/user/unknown", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/root/${encodeURIComponent(String(args['m-wf-id']))}${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(getApiRootMWfIdResponse, await res.json()) };