import {z} from 'zod' export type GetApiApiV2AuditLogsArgs = {'page[before]'?: string | undefined, 'page[after]'?: string | undefined, 'page[size]'?: number | undefined, 'filter[source_type]'?: string | undefined, 'filter[source_id]'?: number | undefined, 'filter[actor_id]'?: number | undefined, 'filter[ip_address]'?: string | undefined, 'filter[created_at]'?: string | undefined, 'filter[action]'?: string | undefined, sort?: string | undefined}; export type GetApiApiV2AuditLogsOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const auditLogObject = z.object({action: z.string().optional(), action_label: z.string().optional(), actor_id: z.number().int().optional(), actor_name: z.string().optional(), change_description: z.string().optional(), created_at: z.string().optional(), id: z.number().int().optional(), ip_address: z.string().optional(), source_id: z.number().int().optional(), source_label: z.string().optional(), source_type: z.string().optional(), url: z.string().optional()}); export const auditLogsResponse = z.object({audit_logs: z.array(auditLogObject).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 }; /** * List Audit Logs * * GET /api/v2/audit_logs */ export const getApiApiV2AuditLogs = async (args: GetApiApiV2AuditLogsArgs, options?: GetApiApiV2AuditLogsOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['filter[source_type]'] != null) query.append("filter[source_type]", String(args['filter[source_type]'])) if (args['filter[source_id]'] != null) query.append("filter[source_id]", String(args['filter[source_id]'])) if (args['filter[actor_id]'] != null) query.append("filter[actor_id]", String(args['filter[actor_id]'])) if (args['filter[ip_address]'] != null) query.append("filter[ip_address]", String(args['filter[ip_address]'])) if (args['filter[created_at]'] != null) query.append("filter[created_at]", String(args['filter[created_at]'])) if (args['filter[action]'] != null) query.append("filter[action]", String(args['filter[action]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/audit_logs${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(auditLogsResponse, await res.json()) };