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 HistoricVariableInstanceQueryRequest = {start?: number | undefined, size?: number | undefined, sort?: string | undefined, order?: string | undefined, excludeTaskVariables?: boolean | undefined, taskId?: string | undefined, planItemInstanceId?: string | undefined, caseInstanceId?: string | undefined, variableName?: string | undefined, variableNameLike?: string | undefined, variables?: Array | undefined, excludeLocalVariables?: boolean | undefined}; export type CreateApiCmmnQueryHistoricVariableInstancesArgs = {sort?: ('caseInstanceId' | 'variableName') | undefined, body: HistoricVariableInstanceQueryRequest}; export type CreateApiCmmnQueryHistoricVariableInstancesOptions = {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 historicVariableInstanceResponse = z.object({id: z.string().optional(), caseInstanceId: z.string().optional(), caseInstanceUrl: z.string().optional(), taskId: z.string().optional(), planItemInstanceId: z.string().optional(), variable: restVariable.optional()}); export const dataResponseHistoricVariableInstanceResponse = z.object({data: z.array(historicVariableInstanceResponse).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 variable instances * * POST /cmmn-query/historic-variable-instances */ export const createApiCmmnQueryHistoricVariableInstances = async (args: CreateApiCmmnQueryHistoricVariableInstancesArgs, options: CreateApiCmmnQueryHistoricVariableInstancesOptions) => { 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 (args.sort != null) query.append("sort", String(args.sort)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/cmmn-query/historic-variable-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(dataResponseHistoricVariableInstanceResponse, await res.json()) };