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 HistoricCaseInstanceQueryRequest = {start?: number | undefined, size?: number | undefined, sort?: string | undefined, order?: string | undefined, caseInstanceId?: string | undefined, caseInstanceIds?: Array | undefined, caseDefinitionId?: string | undefined, caseDefinitionKey?: string | undefined, caseDefinitionKeyLike?: string | undefined, caseDefinitionKeyLikeIgnoreCase?: string | undefined, caseDefinitionKeys?: Array | undefined, excludeCaseDefinitionKeys?: Array | undefined, caseDefinitionCategory?: string | undefined, caseDefinitionCategoryLike?: string | undefined, caseDefinitionCategoryLikeIgnoreCase?: string | undefined, caseDefinitionName?: string | undefined, caseDefinitionNameLike?: string | undefined, caseDefinitionNameLikeIgnoreCase?: string | undefined, caseBusinessKey?: string | undefined, caseInstanceName?: string | undefined, caseInstanceNameLike?: string | undefined, caseInstanceNameLikeIgnoreCase?: string | undefined, caseInstanceRootScopeId?: string | undefined, caseInstanceRootScopeIds?: Array | undefined, caseInstanceParentScopeId?: string | undefined, caseInstanceParentScopeIds?: Array | undefined, caseInstanceBusinessKey?: string | undefined, caseInstanceBusinessKeyLike?: string | undefined, caseInstanceBusinessKeyLikeIgnoreCase?: string | undefined, caseInstanceBusinessStatus?: string | undefined, caseInstanceBusinessStatusLike?: string | undefined, caseInstanceBusinessStatusLikeIgnoreCase?: string | undefined, caseInstanceParentId?: string | undefined, caseInstanceState?: string | undefined, caseInstanceCallbackId?: string | undefined, caseInstanceCallbackIds?: Array | undefined, caseInstanceCallbackType?: string | undefined, parentCaseInstanceId?: string | undefined, parentProcessInstanceId?: string | undefined, caseInstanceReferenceId?: string | undefined, caseInstanceReferenceType?: string | undefined, finished?: boolean | undefined, involvedUser?: string | undefined, finishedAfter?: string | undefined, finishedBefore?: string | undefined, startedAfter?: string | undefined, startedBefore?: string | undefined, startedBy?: string | undefined, finishedBy?: string | undefined, lastReactivatedBy?: string | undefined, lastReactivatedBefore?: string | undefined, lastReactivatedAfter?: string | undefined, activePlanItemDefinitionId?: string | undefined, activePlanItemDefinitionIds?: Array | undefined, includeCaseVariables?: boolean | undefined, includeCaseVariablesNames?: Array | undefined, variables?: Array | undefined, tenantId?: string | undefined, tenantIdLike?: string | undefined, tenantIdLikeIgnoreCase?: string | undefined, withoutTenantId?: boolean | undefined, withoutCaseInstanceParentId?: boolean | undefined, withoutCaseInstanceCallbackId?: boolean | undefined}; export type CreateApiCmmnQueryHistoricCaseInstancesArgs = {body: HistoricCaseInstanceQueryRequest}; export type CreateApiCmmnQueryHistoricCaseInstancesOptions = {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 historicCaseInstanceResponse = z.object({id: z.string().optional(), url: z.string().optional(), name: z.string().optional(), businessKey: z.string().optional(), businessStatus: z.string().optional(), dueDate: z.string().optional(), claimTime: z.string().optional(), claimedBy: z.string().optional(), caseDefinitionId: z.string().optional(), caseDefinitionUrl: z.string().optional(), caseDefinitionName: z.string().optional(), caseDefinitionDescription: z.string().optional(), startTime: z.string().optional(), endTime: z.string().optional(), startUserId: z.string().optional(), endUserId: z.string().optional(), superProcessInstanceId: z.string().optional(), variables: z.array(restVariable).optional(), tenantId: z.string().optional(), state: z.string().optional(), callbackId: z.string().optional(), callbackType: z.string().optional(), referenceId: z.string().optional(), referenceType: z.string().optional()}); export const dataResponseHistoricCaseInstanceResponse = z.object({data: z.array(historicCaseInstanceResponse).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 case instances * * POST /cmmn-query/historic-case-instances */ export const createApiCmmnQueryHistoricCaseInstances = async (args: CreateApiCmmnQueryHistoricCaseInstancesArgs, options: CreateApiCmmnQueryHistoricCaseInstancesOptions) => { 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-case-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(dataResponseHistoricCaseInstanceResponse, await res.json()) };