import {z} from 'zod' export type GetApiApiRulesArgs = {cursor?: string | undefined, limit?: number | undefined, order_by?: ('created_datetime:asc' | 'created_datetime:desc') | undefined, search?: string | undefined}; export type GetApiApiRulesOptions = {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 getApiApiRulesResponse = z.array(rule); 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 }; /** * List rules * * GET /api/rules */ export const getApiApiRules = async (args: GetApiApiRulesArgs, options?: GetApiApiRulesOptions) => { const {baseUrl = "https://your-domain.gorgias.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.search != null) query.append("search", String(args.search)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/rules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiRulesResponse, await res.json()) };