import {z} from 'zod' export type UpdateApiApiRulesIdArgs = {id: number, body: {code?: string | undefined, code_ast?: Record | undefined, deactivated_datetime?: (string | null) | undefined, description?: (string | null) | undefined, event_types?: ('ticket-created' | 'ticket-updated' | 'ticket-message-created' | 'ticket-assigned' | 'ticket-self-unsnoozed' | 'satisfaction-survey-responded') | undefined, name?: string | undefined, priority?: number | undefined}}; export type UpdateApiApiRulesIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const rule = z.object({id: z.number().int().optional(), code: z.string().optional(), code_ast: z.object({}).optional(), created_datetime: z.string().optional(), deactivated_datetime: z.string().nullable().optional(), description: z.string().nullable().optional(), event_types: z.enum(["ticket-created", "ticket-updated", "ticket-message-created", "ticket-assigned", "ticket-self-unsnoozed", "satisfaction-survey-responded"]).optional(), name: z.string().optional(), priority: z.number().int().optional(), updated_datetime: z.string().optional(), uri: z.string().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 a rule * * PUT /api/rules/{id} */ export const updateApiApiRulesId = async (args: UpdateApiApiRulesIdArgs, options?: UpdateApiApiRulesIdOptions) => { const {baseUrl = "https://your-domain.gorgias.com", 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(/\/+$/, '')}/api/rules/${encodeURIComponent(String(args.id))}${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(rule, await res.json()) };