import {z} from 'zod' export type QueryVariable = {name?: string | undefined, operation?: ('equals' | 'notEquals' | 'equalsIgnoreCase' | 'notEqualsIgnoreCase' | 'like' | 'likeIgnoreCase' | 'greaterThan' | 'greaterThanOrEquals' | 'lessThan' | 'lessThanOrEquals' | 'exists' | 'notExists') | undefined, value?: Record | undefined, type?: string | undefined}; export type HistoricTaskInstanceQueryRequest = {start?: number | undefined, size?: number | undefined, sort?: string | undefined, order?: string | undefined, taskId?: string | undefined, caseInstanceId?: string | undefined, caseInstanceIdWithChildren?: string | undefined, caseDefinitionId?: string | undefined, caseDefinitionKey?: string | undefined, caseDefinitionKeyLike?: string | undefined, caseDefinitionKeyLikeIgnoreCase?: string | undefined, caseDefinitionKeys?: Array | undefined, planItemInstanceId?: string | undefined, propagatedStageInstanceId?: string | undefined, scopeId?: string | undefined, scopeIds?: Array | undefined, withoutScopeId?: boolean | undefined, taskName?: string | undefined, taskNameLike?: string | undefined, taskNameLikeIgnoreCase?: string | undefined, taskDescription?: string | undefined, taskDescriptionLike?: string | undefined, taskDefinitionKey?: string | undefined, taskDefinitionKeyLike?: string | undefined, taskCategory?: string | undefined, taskCategoryIn?: Array | undefined, taskCategoryNotIn?: Array | undefined, taskWithoutCategory?: boolean | undefined, taskDeleteReason?: string | undefined, taskDeleteReasonLike?: string | undefined, taskAssignee?: string | undefined, taskAssigneeLike?: string | undefined, taskOwner?: string | undefined, taskOwnerLike?: string | undefined, taskInvolvedUser?: string | undefined, taskPriority?: number | undefined, taskMinPriority?: number | undefined, taskMaxPriority?: number | undefined, finished?: boolean | undefined, processFinished?: boolean | undefined, parentTaskId?: string | undefined, dueDate?: string | undefined, dueDateAfter?: string | undefined, dueDateBefore?: string | undefined, withoutDueDate?: boolean | undefined, taskCreatedOn?: string | undefined, taskCreatedBefore?: string | undefined, taskCreatedAfter?: string | undefined, taskCompletedOn?: string | undefined, taskCompletedBefore?: string | undefined, taskCompletedAfter?: string | undefined, includeTaskLocalVariables?: boolean | undefined, includeProcessVariables?: boolean | undefined, taskVariables?: Array | undefined, tenantId?: string | undefined, tenantIdLike?: string | undefined, withoutTenantId?: boolean | undefined, withoutProcessInstanceId?: boolean | undefined, taskCandidateGroup?: string | undefined, ignoreTaskAssignee?: boolean | undefined, rootScopeId?: string | undefined, parentScopeId?: string | undefined}; export type CreateApiCmmnQueryHistoricTaskInstancesArgs = {body: HistoricTaskInstanceQueryRequest}; export type CreateApiCmmnQueryHistoricTaskInstancesOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const restVariable = z.object({name: z.string().optional(), type: z.string().optional(), value: z.object({}).optional(), valueUrl: z.string().optional(), scope: z.string().optional()}); export const historicTaskInstanceResponse = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), deleteReason: z.string().optional(), owner: z.string().optional(), assignee: z.string().optional(), startTime: z.string().optional(), endTime: z.string().optional(), durationInMillis: z.number().int().optional(), workTimeInMillis: z.number().int().optional(), claimTime: z.string().optional(), taskDefinitionKey: z.string().optional(), formKey: z.string().optional(), priority: z.number().int().optional(), dueDate: z.string().optional(), parentTaskId: z.string().optional(), url: z.string().optional(), variables: z.array(restVariable).optional(), tenantId: z.string().optional(), category: z.string().optional(), caseInstanceId: z.string().optional(), caseInstanceUrl: z.string().optional(), caseDefinitionId: z.string().optional(), caseDefinitionUrl: z.string().optional(), scopeDefinitionId: z.string().optional(), scopeId: z.string().optional(), subScopeId: z.string().optional(), scopeType: z.string().optional(), propagatedStageInstanceId: z.string().optional(), executionId: z.string().optional(), processInstanceId: z.string().optional(), processDefinitionId: z.string().optional()}); export const dataResponseHistoricTaskInstanceResponse = z.object({data: z.array(historicTaskInstanceResponse).optional(), total: z.number().int().optional(), start: z.number().int().optional(), sort: z.string().optional(), order: z.string().optional(), size: z.number().int().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 }; /** * Query for historic task instances * * POST /cmmn-query/historic-task-instances */ export const createApiCmmnQueryHistoricTaskInstances = async (args: CreateApiCmmnQueryHistoricTaskInstancesArgs, options: CreateApiCmmnQueryHistoricTaskInstancesOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/cmmn-query/historic-task-instances${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(dataResponseHistoricTaskInstanceResponse, await res.json()) };