import {z} from 'zod' export type SvflowCommonModelsWorkflowRunStatus = '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 type SvflowPublicapiSearchWorkflowRunsRequest = {teamId?: string | undefined, workflowId?: (string | null) | undefined, statuses?: Array | undefined, targetUserId?: (string | null) | undefined, initiatedByUserId?: (string | null) | undefined, createdAfter?: (string | null) | undefined, createdBefore?: (string | null) | undefined, linkedTicketId?: (string | null) | undefined, pageSize?: (number | null) | undefined, pageToken?: (string | null) | undefined, includeOutput?: (boolean | null) | undefined, parentWorkflowRunId?: (string | null) | undefined}; export type CreateApiV2WorkflowRunsSearchArgs = {body: SvflowPublicapiSearchWorkflowRunsRequest}; export type CreateApiV2WorkflowRunsSearchOptions = {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 svflowPublicapiSearchWorkflowRunsResponse = z.object({data: z.array(svflowPubapimodelsWorkflowRun).optional(), nextPageToken: z.string().nullable().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 }; /** * Search Workflow Runs * * POST /v2/workflow-runs/search */ export const createApiV2WorkflowRunsSearch = async (args: CreateApiV2WorkflowRunsSearchArgs, options?: CreateApiV2WorkflowRunsSearchOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/workflow-runs/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(svflowPublicapiSearchWorkflowRunsResponse, await res.json()) };