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 TaskQueryRequest = {start?: number | undefined, size?: number | undefined, sort?: string | undefined, order?: string | undefined, taskId?: string | undefined, name?: string | undefined, nameLike?: string | undefined, nameLikeIgnoreCase?: string | undefined, description?: string | undefined, descriptionLike?: string | undefined, priority?: number | undefined, minimumPriority?: number | undefined, maximumPriority?: number | undefined, assignee?: string | undefined, assigneeLike?: string | undefined, owner?: string | undefined, ownerLike?: string | undefined, unassigned?: boolean | undefined, delegationState?: string | undefined, candidateUser?: string | undefined, candidateGroup?: string | undefined, candidateGroupIn?: Array | undefined, ignoreAssignee?: boolean | undefined, involvedUser?: string | undefined, caseInstanceId?: string | undefined, caseInstanceIdWithChildren?: string | undefined, planItemInstanceId?: string | undefined, propagatedStageInstanceId?: string | undefined, scopeId?: string | undefined, scopeIds?: Array | undefined, withoutScopeId?: boolean | undefined, subScopeId?: string | undefined, scopeType?: string | undefined, caseDefinitionId?: string | undefined, caseDefinitionKey?: string | undefined, caseDefinitionKeyLike?: string | undefined, caseDefinitionKeyLikeIgnoreCase?: string | undefined, caseDefinitionKeys?: Array | undefined, createdOn?: string | undefined, createdBefore?: string | undefined, createdAfter?: string | undefined, excludeSubTasks?: boolean | undefined, taskDefinitionKey?: string | undefined, taskDefinitionKeyLike?: string | undefined, dueDate?: string | undefined, dueBefore?: string | undefined, dueAfter?: string | undefined, withoutDueDate?: boolean | undefined, active?: boolean | undefined, includeTaskLocalVariables?: boolean | undefined, includeProcessVariables?: boolean | undefined, tenantId?: string | undefined, tenantIdLike?: string | undefined, withoutTenantId?: boolean | undefined, withoutProcessInstanceId?: boolean | undefined, candidateOrAssigned?: string | undefined, category?: string | undefined, rootScopeId?: string | undefined, parentScopeId?: string | undefined, taskVariables?: Array | undefined, categoryIn?: Array | undefined, categoryNotIn?: Array | undefined, withoutCategory?: boolean | undefined}; export type CreateApiCmmnQueryTasksArgs = {body: TaskQueryRequest}; export type CreateApiCmmnQueryTasksOptions = {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 taskResponse = z.object({id: z.string().optional(), url: z.string().optional(), owner: z.string().optional(), assignee: z.string().optional(), delegationState: z.string().optional(), name: z.string().optional(), description: z.string().optional(), createTime: z.string().optional(), dueDate: z.string().optional(), priority: z.number().int().optional(), suspended: z.boolean().optional(), claimTime: z.string().optional(), taskDefinitionKey: z.string().optional(), scopeDefinitionId: z.string().optional(), scopeId: z.string().optional(), subScopeId: z.string().optional(), scopeType: z.string().optional(), propagatedStageInstanceId: z.string().optional(), tenantId: z.string().optional(), category: z.string().optional(), formKey: z.string().optional(), caseInstanceId: z.string().optional(), caseInstanceUrl: z.string().optional(), caseDefinitionId: z.string().optional(), caseDefinitionUrl: z.string().optional(), parentTaskId: z.string().optional(), parentTaskUrl: z.string().optional(), executionId: z.string().optional(), processInstanceId: z.string().optional(), processDefinitionId: z.string().optional(), variables: z.array(restVariable).optional()}); export const dataResponseTaskResponse = z.object({data: z.array(taskResponse).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 tasks * * POST /cmmn-query/tasks */ export const createApiCmmnQueryTasks = async (args: CreateApiCmmnQueryTasksArgs, options: CreateApiCmmnQueryTasksOptions) => { 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/tasks${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(dataResponseTaskResponse, await res.json()) };