import {z} from 'zod' export type GetApiV2WorkflowRunsIdArgs = {id: string}; export type GetApiV2WorkflowRunsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const svflowCommonModelsWorkflowRunStatus = z.enum(["WORKFLOW_RUN_STATUS_UNSPECIFIED", "WORKFLOW_RUN_STATUS_PENDING", "WORKFLOW_RUN_STATUS_RUNNING", "WORKFLOW_RUN_STATUS_COMPLETED", "WORKFLOW_RUN_STATUS_FAILED", "WORKFLOW_RUN_STATUS_DENIED", "WORKFLOW_RUN_STATUS_CANCELED"]); export const svflowPubapimodelsWorkflowRun = z.object({id: z.string().optional(), workflowId: z.string().optional(), teamId: z.string().optional(), status: svflowCommonModelsWorkflowRunStatus.optional(), createdAt: z.string().optional(), completedAt: z.string().nullable().optional(), initiatedByUserId: z.string().optional(), targetUserId: z.string().nullable().optional(), inputs: z.string().nullable().optional(), output: z.string().nullable().optional(), linkedTicketId: z.string().nullable().optional(), approvalRequestId: z.string().nullable().optional(), statusUpdatedAt: z.string().optional(), parentWorkflowRunId: z.string().nullable().optional(), parentWorkflowRunStepNumber: z.union([z.number().int(), z.string()]).nullable().optional()}); export const svflowPublicapiGetWorkflowRunResponse = z.object({data: svflowPubapimodelsWorkflowRun.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 Workflow Run * * GET /v2/workflow-runs/{id} */ export const getApiV2WorkflowRunsId = async (args: GetApiV2WorkflowRunsIdArgs, options?: GetApiV2WorkflowRunsIdOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/workflow-runs/${encodeURIComponent(String(args.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(svflowPublicapiGetWorkflowRunResponse, await res.json()) };