import {z} from 'zod' export type GetApiApiOrgIdWorkflowsIdHistoryArgs = {org_id: string, id: string, start_time?: number | undefined, end_time?: number | undefined}; export type GetApiApiOrgIdWorkflowsIdHistoryOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowTriggerType = z.enum(["AlertFired", "AlertResolved", "IncidentEvent", "Webhook", "Manual", "Test", "Retry", "Unknown"]); export const httpRequestFacts = z.object({body_bytes: z.number().int(), caller_ip: z.string(), content_type: z.string().nullable().optional(), credential_prefix: z.string(), error: z.string().nullable().optional(), idempotency_key: z.string().nullable().optional(), status: z.string()}); export const workflowHistoryRow = z.object({_timestamp: z.number().int(), end_time: z.number().int(), error: z.string().nullable().optional(), evaluation_took_in_secs: z.number(), event_type: workflowTriggerType.optional(), http_request: httpRequestFacts.nullable().optional(), key: z.string(), org: z.string(), run_id: z.string().optional(), source_id: z.string().optional(), source_node: z.string(), start_time: z.number().int(), status: z.string().nullable().optional()}); export const getApiApiOrgIdWorkflowsIdHistoryResponse = z.array(workflowHistoryRow); 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 history of workflow executions * * GET /api/{org_id}/workflows/{id}/history */ export const getApiApiOrgIdWorkflowsIdHistory = async (args: GetApiApiOrgIdWorkflowsIdHistoryArgs, options: GetApiApiOrgIdWorkflowsIdHistoryOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/workflows/${encodeURIComponent(String(args.id))}/history${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(getApiApiOrgIdWorkflowsIdHistoryResponse, await res.json()) };