import {z} from 'zod' export type GetApiApiAuditLogsArgs = {pageSize: number, page: number, textSearch?: string | undefined, sortProperty?: ('createdTime' | 'entityType' | 'entityName' | 'userName' | 'actionType' | 'actionStatus') | undefined, sortOrder?: ('ASC' | 'DESC') | undefined, startTime?: number | undefined, endTime?: number | undefined, actionTypes?: string | undefined}; export type GetApiApiAuditLogsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const auditLogId = z.object({id: z.string()}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const customerId = z.object({id: z.string(), entityType: z.literal("CUSTOMER")}); export const entityId = z.object({id: z.string(), entityType: z.enum(["TENANT", "CUSTOMER", "USER", "DASHBOARD", "ASSET", "DEVICE", "ALARM", "RULE_CHAIN", "RULE_NODE", "ENTITY_VIEW", "WIDGETS_BUNDLE", "WIDGET_TYPE", "TENANT_PROFILE", "DEVICE_PROFILE", "ASSET_PROFILE", "API_USAGE_STATE", "TB_RESOURCE", "OTA_PACKAGE", "EDGE", "RPC", "QUEUE", "NOTIFICATION_TARGET", "NOTIFICATION_TEMPLATE", "NOTIFICATION_REQUEST", "NOTIFICATION", "NOTIFICATION_RULE", "QUEUE_STATS", "OAUTH2_CLIENT", "DOMAIN", "MOBILE_APP", "MOBILE_APP_BUNDLE", "CALCULATED_FIELD", "JOB", "ADMIN_SETTINGS", "AI_MODEL", "API_KEY"])}); export const userId = z.object({id: z.string(), entityType: z.literal("USER")}); export const jsonNode = z.unknown(); export const auditLog = z.object({id: auditLogId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), customerId: customerId.optional(), entityId: entityId.optional(), entityName: z.string().optional(), userId: userId.optional(), userName: z.string().optional(), actionType: z.enum(["ADDED", "DELETED", "UPDATED", "ATTRIBUTES_UPDATED", "ATTRIBUTES_DELETED", "TIMESERIES_UPDATED", "TIMESERIES_DELETED", "RPC_CALL", "CREDENTIALS_UPDATED", "ASSIGNED_TO_CUSTOMER", "UNASSIGNED_FROM_CUSTOMER", "ACTIVATED", "SUSPENDED", "CREDENTIALS_READ", "ATTRIBUTES_READ", "RELATION_ADD_OR_UPDATE", "RELATION_DELETED", "RELATIONS_DELETED", "REST_API_RULE_ENGINE_CALL", "ALARM_ACK", "ALARM_CLEAR", "ALARM_DELETE", "ALARM_ASSIGNED", "ALARM_UNASSIGNED", "LOGIN", "LOGOUT", "LOCKOUT", "ASSIGNED_FROM_TENANT", "ASSIGNED_TO_TENANT", "PROVISION_SUCCESS", "PROVISION_FAILURE", "ASSIGNED_TO_EDGE", "UNASSIGNED_FROM_EDGE", "ADDED_COMMENT", "UPDATED_COMMENT", "DELETED_COMMENT", "SMS_SENT"]).optional(), actionData: jsonNode.optional(), actionStatus: z.enum(["SUCCESS", "FAILURE"]).optional(), actionFailureDetails: z.string().optional()}); export const pageDataAuditLog = z.object({data: z.array(auditLog).optional(), totalPages: z.number().int().optional(), totalElements: z.number().int().optional(), hasNext: 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 }; /** * Get all audit logs (getAuditLogs) * * GET /api/audit/logs */ export const getApiApiAuditLogs = async (args: GetApiApiAuditLogsArgs, options?: GetApiApiAuditLogsOptions) => { const {baseUrl = "https://demo.thingsboard.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.textSearch != null) query.append("textSearch", String(args.textSearch)) if (args.sortProperty != null) query.append("sortProperty", String(args.sortProperty)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.actionTypes != null) query.append("actionTypes", String(args.actionTypes)) const url = `${baseUrl.replace(/\/+$/, '')}/api/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(pageDataAuditLog, await res.json()) };