import {z} from 'zod' export type RestVariable = {name?: string | undefined, type?: string | undefined, value?: Record | undefined, valueUrl?: string | undefined, scope?: string | undefined}; export type CaseInstanceCreateRequest = {caseDefinitionId?: string | undefined, caseDefinitionKey?: string | undefined, name?: string | undefined, businessKey?: string | undefined, variables?: Array | undefined, transientVariables?: Array | undefined, startFormVariables?: Array | undefined, outcome?: string | undefined, tenantId?: string | undefined, overrideDefinitionTenantId?: string | undefined, returnVariables?: boolean | undefined}; export type CreateApiCmmnRuntimeCaseInstancesArgs = {body: CaseInstanceCreateRequest}; export type CreateApiCmmnRuntimeCaseInstancesOptions = {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 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 }; /** * Start a case instance * * POST /cmmn-runtime/case-instances */ export const createApiCmmnRuntimeCaseInstances = async (args: CreateApiCmmnRuntimeCaseInstancesArgs, options: CreateApiCmmnRuntimeCaseInstancesOptions) => { 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-runtime/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(caseInstanceResponse, await res.json()) };