import {z} from 'zod' export type RestConditionGroup = {operator: string, priority?: number | undefined, conditions?: {[key: string]: boolean} | undefined}; export type RestRule = {name?: string | undefined, fireOn?: string | undefined, shortCircuit?: boolean | undefined, priority?: number | undefined, enabled?: boolean | undefined, conditionGroups?: {[key: string]: RestConditionGroup} | undefined, ruleActions?: {[key: string]: boolean} | undefined}; export type UpdateApiApiV1SitesSiteIdRuleengineRulesRuleIdArgs = {siteId: string, ruleId: string, body: RestRule}; export type UpdateApiApiV1SitesSiteIdRuleengineRulesRuleIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const restConditionGroup = z.object({operator: z.string(), priority: z.number().int().optional(), conditions: z.record(z.string(), z.boolean()).optional()}); export const restRule = z.object({name: z.string().optional(), fireOn: z.string().optional(), shortCircuit: z.boolean().optional(), priority: z.number().int().optional(), enabled: z.boolean().optional(), conditionGroups: z.record(z.string(), restConditionGroup).optional(), ruleActions: z.record(z.string(), 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 }; /** * PUT /api/v1/sites/{siteId}/ruleengine/rules/{ruleId} */ export const updateApiApiV1SitesSiteIdRuleengineRulesRuleId = async (args: UpdateApiApiV1SitesSiteIdRuleengineRulesRuleIdArgs, options: UpdateApiApiV1SitesSiteIdRuleengineRulesRuleIdOptions) => { 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/sites/${encodeURIComponent(String(args.siteId))}/ruleengine/rules/${encodeURIComponent(String(args.ruleId))}${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(restRule, await res.json()) };