import {z} from 'zod' export type WorkflowSystemActionForm = {actionId: string, schemeId?: string | undefined, contentTypeVariable?: string | undefined, systemAction: 'NEW' | 'EDIT' | 'PUBLISH' | 'UNPUBLISH' | 'ARCHIVE' | 'UNARCHIVE' | 'DELETE' | 'DESTROY' | 'LOCK' | 'UNLOCK'}; export type UpdateApiApiV1WorkflowSystemActionsArgs = {body: WorkflowSystemActionForm}; export type UpdateApiApiV1WorkflowSystemActionsOptions = {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 actionFail = z.object({identifier: z.string().optional(), inode: z.string().optional(), errorMessage: z.string().optional()}); export const bulkActionsResultView = z.object({successCount: z.number().int().optional(), skippedCount: z.number().int().optional(), fails: z.array(actionFail).optional(), skipReason: z.string().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 responseEntityBulkActionsResultView = z.object({errors: z.array(errorEntity).optional(), entity: bulkActionsResultView.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 }; /** * Save a default system action mapping * * PUT /api/v1/workflow/system/actions */ export const updateApiApiV1WorkflowSystemActions = async (args: UpdateApiApiV1WorkflowSystemActionsArgs, options: UpdateApiApiV1WorkflowSystemActionsOptions) => { 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/system/actions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(responseEntityBulkActionsResultView, await res.json()) };