import {z} from 'zod' export type GetApiCmmnRuntimeCaseInstancesArgs = {id?: string | undefined, ids?: string | undefined, caseDefinitionKey?: string | undefined, caseDefinitionKeyLike?: string | undefined, caseDefinitionKeyLikeIgnoreCase?: string | undefined, caseDefinitionId?: string | undefined, caseDefinitionCategory?: string | undefined, caseDefinitionCategoryLike?: string | undefined, caseDefinitionCategoryLikeIgnoreCase?: string | undefined, caseDefinitionName?: string | undefined, caseDefinitionNameLike?: string | undefined, caseDefinitionNameLikeIgnoreCase?: string | undefined, name?: string | undefined, nameLike?: string | undefined, nameLikeIgnoreCase?: string | undefined, rootScopeId?: string | undefined, rootScopeIds?: string | undefined, parentScopeId?: string | undefined, parentScopeIds?: string | undefined, businessKey?: string | undefined, businessKeyLike?: string | undefined, businessKeyLikeIgnoreCase?: string | undefined, businessStatus?: string | undefined, businessStatusLike?: string | undefined, businessStatusLikeIgnoreCase?: string | undefined, caseInstanceParentId?: string | undefined, startedBy?: string | undefined, startedBefore?: string | undefined, startedAfter?: string | undefined, state?: string | undefined, callbackId?: string | undefined, callbackIds?: string | undefined, callbackType?: string | undefined, parentCaseInstanceId?: string | undefined, parentProcessInstanceId?: string | undefined, referenceId?: string | undefined, referenceType?: string | undefined, lastReactivatedBy?: string | undefined, lastReactivatedBefore?: string | undefined, lastReactivatedAfter?: string | undefined, includeCaseVariables?: boolean | undefined, includeCaseVariablesName?: string | undefined, activePlanItemDefinitionId?: string | undefined, tenantId?: string | undefined, tenantIdLike?: string | undefined, tenantIdLikeIgnoreCase?: string | undefined, withoutTenantId?: boolean | undefined, sort?: ('id' | 'caseDefinitionId' | 'tenantId' | 'caseDefinitionKey') | undefined}; export type GetApiCmmnRuntimeCaseInstancesOptions = {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 caseInstanceResponse = z.object({id: z.string().optional(), name: z.string().optional(), url: z.string().optional(), businessKey: z.string().optional(), businessStatus: z.string().optional(), dueDate: z.string().optional(), claimTime: z.string().optional(), claimedBy: z.string().optional(), startTime: z.string().optional(), startUserId: z.string().optional(), state: z.string().optional(), ended: z.boolean().optional(), caseDefinitionId: z.string().optional(), caseDefinitionUrl: z.string().optional(), caseDefinitionName: z.string().optional(), caseDefinitionDescription: z.string().optional(), parentId: z.string().optional(), callbackId: z.string().optional(), callbackType: z.string().optional(), referenceId: z.string().optional(), referenceType: z.string().optional(), variables: z.array(restVariable).optional(), tenantId: z.string().optional(), completed: z.boolean().optional()}); export const dataResponseCaseInstanceResponse = z.object({data: z.array(caseInstanceResponse).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 }; /** * List case instances * * GET /cmmn-runtime/case-instances */ export const getApiCmmnRuntimeCaseInstances = async (args: GetApiCmmnRuntimeCaseInstancesArgs, options: GetApiCmmnRuntimeCaseInstancesOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.caseDefinitionKey != null) query.append("caseDefinitionKey", String(args.caseDefinitionKey)) if (args.caseDefinitionKeyLike != null) query.append("caseDefinitionKeyLike", String(args.caseDefinitionKeyLike)) if (args.caseDefinitionKeyLikeIgnoreCase != null) query.append("caseDefinitionKeyLikeIgnoreCase", String(args.caseDefinitionKeyLikeIgnoreCase)) if (args.caseDefinitionId != null) query.append("caseDefinitionId", String(args.caseDefinitionId)) if (args.caseDefinitionCategory != null) query.append("caseDefinitionCategory", String(args.caseDefinitionCategory)) if (args.caseDefinitionCategoryLike != null) query.append("caseDefinitionCategoryLike", String(args.caseDefinitionCategoryLike)) if (args.caseDefinitionCategoryLikeIgnoreCase != null) query.append("caseDefinitionCategoryLikeIgnoreCase", String(args.caseDefinitionCategoryLikeIgnoreCase)) if (args.caseDefinitionName != null) query.append("caseDefinitionName", String(args.caseDefinitionName)) if (args.caseDefinitionNameLike != null) query.append("caseDefinitionNameLike", String(args.caseDefinitionNameLike)) if (args.caseDefinitionNameLikeIgnoreCase != null) query.append("caseDefinitionNameLikeIgnoreCase", String(args.caseDefinitionNameLikeIgnoreCase)) if (args.name != null) query.append("name", String(args.name)) if (args.nameLike != null) query.append("nameLike", String(args.nameLike)) if (args.nameLikeIgnoreCase != null) query.append("nameLikeIgnoreCase", String(args.nameLikeIgnoreCase)) if (args.rootScopeId != null) query.append("rootScopeId", String(args.rootScopeId)) if (args.rootScopeIds != null) query.append("rootScopeIds", String(args.rootScopeIds)) if (args.parentScopeId != null) query.append("parentScopeId", String(args.parentScopeId)) if (args.parentScopeIds != null) query.append("parentScopeIds", String(args.parentScopeIds)) if (args.businessKey != null) query.append("businessKey", String(args.businessKey)) if (args.businessKeyLike != null) query.append("businessKeyLike", String(args.businessKeyLike)) if (args.businessKeyLikeIgnoreCase != null) query.append("businessKeyLikeIgnoreCase", String(args.businessKeyLikeIgnoreCase)) if (args.businessStatus != null) query.append("businessStatus", String(args.businessStatus)) if (args.businessStatusLike != null) query.append("businessStatusLike", String(args.businessStatusLike)) if (args.businessStatusLikeIgnoreCase != null) query.append("businessStatusLikeIgnoreCase", String(args.businessStatusLikeIgnoreCase)) if (args.caseInstanceParentId != null) query.append("caseInstanceParentId", String(args.caseInstanceParentId)) if (args.startedBy != null) query.append("startedBy", String(args.startedBy)) if (args.startedBefore != null) query.append("startedBefore", String(args.startedBefore)) if (args.startedAfter != null) query.append("startedAfter", String(args.startedAfter)) if (args.state != null) query.append("state", String(args.state)) if (args.callbackId != null) query.append("callbackId", String(args.callbackId)) if (args.callbackIds != null) query.append("callbackIds", String(args.callbackIds)) if (args.callbackType != null) query.append("callbackType", String(args.callbackType)) if (args.parentCaseInstanceId != null) query.append("parentCaseInstanceId", String(args.parentCaseInstanceId)) if (args.parentProcessInstanceId != null) query.append("parentProcessInstanceId", String(args.parentProcessInstanceId)) if (args.referenceId != null) query.append("referenceId", String(args.referenceId)) if (args.referenceType != null) query.append("referenceType", String(args.referenceType)) if (args.lastReactivatedBy != null) query.append("lastReactivatedBy", String(args.lastReactivatedBy)) if (args.lastReactivatedBefore != null) query.append("lastReactivatedBefore", String(args.lastReactivatedBefore)) if (args.lastReactivatedAfter != null) query.append("lastReactivatedAfter", String(args.lastReactivatedAfter)) if (args.includeCaseVariables != null) query.append("includeCaseVariables", String(args.includeCaseVariables)) if (args.includeCaseVariablesName != null) query.append("includeCaseVariablesName", String(args.includeCaseVariablesName)) if (args.activePlanItemDefinitionId != null) query.append("activePlanItemDefinitionId", String(args.activePlanItemDefinitionId)) if (args.tenantId != null) query.append("tenantId", String(args.tenantId)) if (args.tenantIdLike != null) query.append("tenantIdLike", String(args.tenantIdLike)) if (args.tenantIdLikeIgnoreCase != null) query.append("tenantIdLikeIgnoreCase", String(args.tenantIdLikeIgnoreCase)) if (args.withoutTenantId != null) query.append("withoutTenantId", String(args.withoutTenantId)) 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-runtime/case-instances${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dataResponseCaseInstanceResponse, await res.json()) };