import {z} from 'zod' export type WorkflowSearcherForm = {keywords?: string | undefined, assignedTo?: string | undefined, daysOld?: number | undefined, schemeId?: string | undefined, stepId?: string | undefined, open?: boolean | undefined, closed?: boolean | undefined, createdBy?: string | undefined, show4all?: boolean | undefined, orderBy?: string | undefined, count?: number | undefined, page?: number | undefined}; export type CreateApiApiV1WorkflowTasksArgs = {body: WorkflowSearcherForm}; export type CreateApiApiV1WorkflowTasksOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const errorEntity = z.object({errorCode: z.string().optional(), message: z.string().optional(), fieldName: z.string().optional()}); export const workflowTaskView = z.object({id: z.string().optional(), assignedTo: z.string().optional(), assignedUserName: z.string().optional(), createdBy: z.string().optional(), description: z.string().optional(), belongsTo: z.string().optional(), creationDate: z.string().optional(), dueDate: z.string().optional(), languageId: z.number().int().optional(), modDate: z.string().optional(), title: z.string().optional(), status: z.string().optional()}); export const workflowSearchResultsView = z.object({totalCount: z.number().int().optional(), workflowTasks: z.array(workflowTaskView).optional()}); export const messageEntity = z.object({message: z.string().optional()}); export const pagination = z.object({currentPage: z.number().int().optional(), perPage: z.number().int().optional(), totalEntries: z.number().int().optional()}); export const responseEntityWorkflowTasksView = z.object({errors: z.array(errorEntity).optional(), entity: workflowSearchResultsView.optional(), messages: z.array(messageEntity).optional(), i18nMessagesMap: z.record(z.string(), z.string()).optional(), permissions: z.array(z.string()).optional(), pagination: pagination.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 }; /** * Find workflow tasks based on filter parameters * * POST /api/v1/workflow/tasks */ export const createApiApiV1WorkflowTasks = async (args: CreateApiApiV1WorkflowTasksArgs, options: CreateApiApiV1WorkflowTasksOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/workflow/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(responseEntityWorkflowTasksView, await res.json()) };