import {z} from 'zod' export type GetApiSimulationsSimIdentifierRunsRunIdentifierArgs = {sim_identifier: number, run_identifier: number, expand?: boolean | undefined}; export type GetApiSimulationsSimIdentifierRunsRunIdentifierOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const tag = z.object({tag: z.string(), color: z.string()}); export const bagSummary = z.object({detail_type: z.string(), name: z.string(), store_data: z.object({}), discovered: z.string(), extraction_failure: z.boolean().optional(), in_trash: z.boolean().optional(), is_extracted: z.boolean(), meta_available: z.boolean(), size: z.number().int().optional(), start_time: z.string().optional(), end_time: z.string().optional(), duration: z.number().optional(), messages: z.number().int().optional(), tags: z.array(tag).optional()}); export const simulationRunDetailed = z.object({detail_type: z.string(), identifier: z.number().int(), success: z.boolean(), description: z.string(), duration: z.number(), bag_name: z.string().optional(), bag_store_name: z.string().optional(), results: z.object({}), bag: bagSummary.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 }; /** * Get simulation run * * GET /simulations/{sim_identifier}/runs/{run_identifier} */ export const getApiSimulationsSimIdentifierRunsRunIdentifier = async (args: GetApiSimulationsSimIdentifierRunsRunIdentifierArgs, options: GetApiSimulationsSimIdentifierRunsRunIdentifierOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.expand != null) query.append("expand", String(args.expand)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/simulations/${encodeURIComponent(String(args.sim_identifier))}/runs/${encodeURIComponent(String(args.run_identifier))}${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(simulationRunDetailed, await res.json()) };