import {z} from 'zod' export type AuditLogType = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; export type ViewType = 0 | 1; export type GetApiV1AuditlogsArgs = {Id?: number | undefined, UserId?: number | undefined, UserName?: string | undefined, Nickname?: string | undefined, Type?: AuditLogType | undefined, FromDate?: string | undefined, ToDate?: string | undefined, Event?: string | undefined, ActorType?: string | undefined, TargetType?: string | undefined, RecordId?: string | undefined, RecordName?: string | undefined, Result?: string | undefined, RetentionClass?: string | undefined, Source?: string | undefined, CorrelationId?: string | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined, isExport?: boolean | undefined}; export type GetApiV1AuditlogsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const auditLogType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8)]); export const auditLogResponseModel = z.object({id: z.number().int().optional(), accountId: z.number().int().optional(), departmentId: z.number().int().optional(), userId: z.number().int().optional(), userName: z.string().nullable().optional(), actorType: z.string().nullable().optional(), type: auditLogType.optional(), event: z.string().nullable().optional(), targetType: z.string().nullable().optional(), result: z.string().nullable().optional(), eventPrevData: z.string().nullable().optional(), eventNewData: z.string().nullable().optional(), recordId: z.string().nullable().optional(), recordName: z.string().nullable().optional(), ipAddress: z.string().nullable().optional(), sessionId: z.string().nullable().optional(), retentionClass: z.string().nullable().optional(), source: z.string().nullable().optional(), apiKeyId: z.number().int().nullable().optional(), correlationId: z.string().nullable().optional(), hash: z.string().nullable().optional(), prevHash: z.string().nullable().optional(), date: z.string().optional()}); export const auditLogResponseModelQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(auditLogResponseModel).nullable().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 all audit logs * * GET /v1/auditlogs * * Also accepts auth: API Key */ export const getApiV1Auditlogs = async (args: GetApiV1AuditlogsArgs, options: GetApiV1AuditlogsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.UserId != null) query.append("UserId", String(args.UserId)) if (args.UserName != null) query.append("UserName", String(args.UserName)) if (args.Nickname != null) query.append("Nickname", String(args.Nickname)) if (args.Type != null) query.append("Type", String(args.Type)) if (args.FromDate != null) query.append("FromDate", String(args.FromDate)) if (args.ToDate != null) query.append("ToDate", String(args.ToDate)) if (args.Event != null) query.append("Event", String(args.Event)) if (args.ActorType != null) query.append("ActorType", String(args.ActorType)) if (args.TargetType != null) query.append("TargetType", String(args.TargetType)) if (args.RecordId != null) query.append("RecordId", String(args.RecordId)) if (args.RecordName != null) query.append("RecordName", String(args.RecordName)) if (args.Result != null) query.append("Result", String(args.Result)) if (args.RetentionClass != null) query.append("RetentionClass", String(args.RetentionClass)) if (args.Source != null) query.append("Source", String(args.Source)) if (args.CorrelationId != null) query.append("CorrelationId", String(args.CorrelationId)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (args.isExport != null) query.append("isExport", String(args.isExport)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/auditlogs${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(auditLogResponseModelQueryResult, await res.json()) };