import {z} from 'zod' export type RuleCondition = {field: string, op: string, value: unknown}; export type RuleConditionGroup = {op?: string | undefined, conditions: Array}; export type RuleAction = {op: string, value: unknown}; export type RuleUpdate = {name?: (string | null) | undefined, conditions_op?: (string | null) | undefined, conditions?: (Array | null) | undefined, actions?: (Array | null) | undefined, priority?: (number | null) | undefined, is_active?: (boolean | null) | undefined, apply_to_existing?: (boolean | null) | undefined, overwrite_existing_categories?: boolean | undefined}; export type PatchApiApiRulesRuleIdArgs = {rule_id: string, 'X-Workspace-Id'?: (string | null) | undefined, body: RuleUpdate}; export type PatchApiApiRulesRuleIdOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ruleMutationResponse = z.object({id: z.string(), user_id: z.string(), name: z.string(), conditions_op: z.string(), conditions: z.array(z.record(z.string(), z.unknown())), actions: z.array(z.record(z.string(), z.unknown())), priority: z.number().int(), is_active: z.boolean(), applied_count: 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 }; /** * Update Rule * * PATCH /api/rules/{rule_id} */ export const patchApiApiRulesRuleId = async (args: PatchApiApiRulesRuleIdArgs, options: PatchApiApiRulesRuleIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Workspace-Id'] != null) headers.set("X-Workspace-Id", String(args['X-Workspace-Id'])) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/rules/${encodeURIComponent(String(args.rule_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(ruleMutationResponse, await res.json()) };