### Installing Ozhi with pnpm Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Provides the command to install the `@valink-solutions-ltd/ozhi` package using pnpm, which is the first step to integrate the audit system into a project. ```bash pnpm add @valink-solutions-ltd/ozhi ``` -------------------------------- ### Setting up Drizzle ORM Schema for Ozhi in TypeScript Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Demonstrates how to integrate Ozhi's database schema with an existing Drizzle ORM setup. It imports Ozhi's schema and merges it with the application's schema to ensure audit logs are stored correctly. ```typescript import { drizzle } from 'drizzle-orm/postgres-js'; import { postgres } from 'drizzle-orm/postgres-js'; import * as schema from "./schema"; import * as ohziSchema from '@valink-solutions-ltd/ozhi/db/schema'; const client = postgres(process.env.DATABASE_URL!); export const db = drizzle(client, {{ ...schema, ...ohziSchema }}); ``` -------------------------------- ### Configuring Ozhi Audit Middleware and Plugins in SvelteKit Hooks Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Shows how to set up Ozhi's audit middleware and register optional plugins (security, notification, Stripe) within `src/hooks.server.ts`. It initializes the auditor and applies the `createAuditMiddleware` to handle requests, enabling comprehensive auditing across the application. ```typescript import { createAuditMiddleware } from '@valink-solutions-ltd/ozhi'; import { auditor } from '@valink-solutions-ltd/ozhi'; import { createNotificationPlugin, createStripePlugin, createSecurityPlugin } from '@valink-solutions-ltd/ozhi/plugins'; import { resend } from 'resend'; import { stripe } from 'stripe'; // Configure plugins (optional) auditor.registerPlugin( createSecurityPlugin({ maxFailedAttempts: 5, timeWindowMs: 300000 // 5 minutes }) ); auditor.registerPlugin( createNotificationPlugin({ resend, notifyEmail: 'security@yourdomain.com', severityThreshold: AuditSeverity.HIGH }) ); auditor.registerPlugin(createStripePlugin({ stripe })); // Initialize auditor await auditor.initialize(); // Apply middleware export const handle = createAuditMiddleware(); ``` -------------------------------- ### Creating Custom AuditPlugin in TypeScript Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Demonstrates how to create a custom audit plugin by implementing the `AuditPlugin` interface. It includes `initialize`, `beforeAudit`, and `afterAudit` methods for lifecycle hooks, allowing modification or reaction to audit events. ```typescript import type { AuditPlugin, AuditEvent } from '@valink-solutions-ltd/ozhi/types'; export function createCustomPlugin(): AuditPlugin { return { name: 'custom-plugin', initialize: async () => { // Setup code }, beforeAudit: async (event: AuditEvent) => { // Modify or cancel events // Return null to cancel, or modified event return event; }, afterAudit: async (event: AuditEvent) => { // React to audit events } }; } ``` -------------------------------- ### Auditing SvelteKit Form Actions with Ozhi Helpers Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Demonstrates using `auditFormAction` to automatically audit SvelteKit form actions. This helper simplifies integrating audit logging into form submissions by wrapping the action logic with predefined audit details, reducing boilerplate. ```typescript import { auditFormAction } from '@valink-solutions-ltd/ozhi/helpers'; import type { Actions } from './$types'; export const actions = { delete: auditFormAction( 'user.delete', AuditCategory.USER_MANAGEMENT )(async ({ params }) => { // Delete user logic }) } satisfies Actions; ``` -------------------------------- ### Auditing SvelteKit Server Endpoints with Ozhi Middleware Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Shows how to wrap a SvelteKit `RequestHandler` with `withAudit` to automatically log events for server endpoints. It specifies the action and category for the audit entry, integrating auditing seamlessly into API routes without manual `audit` calls. ```typescript import { withAudit } from '@valink-solutions-ltd/ozhi/middleware'; import type { RequestHandler } from './$types'; export const POST: RequestHandler = withAudit( 'invoice.complete', AuditCategory.INVOICE, async ({ request }) => { const { invoiceId } = await request.json(); // Your business logic here return json({ success: true }); } ); ``` -------------------------------- ### Configuring Security Plugin for Suspicious Activity Monitoring Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Illustrates the configuration of the built-in security plugin. It allows setting `maxFailedAttempts` and a `timeWindowMs` to monitor and alert on suspicious activities like multiple failed login attempts within a defined period. ```typescript createSecurityPlugin({ maxFailedAttempts: 5, timeWindowMs: 300000 // 5 minutes }); ``` -------------------------------- ### Integrating Ozhi Audit with Better-Auth Hooks in TypeScript Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Explains how to integrate Ozhi with Better-Auth to automatically log authentication events. It uses Better-Auth's `after` hooks for `signIn` and `signOut` to trigger audit entries, capturing user and session details for security monitoring. ```typescript import { betterAuth } from 'better-auth'; import { audit } from '@valink-solutions-ltd/ozhi'; import { AuditCategory, AuditResult } from '@valink-solutions-ltd/ozhi/types'; export const auth = betterAuth({ // ... your config hooks: { after: { signIn: async ({ user, session }) => { await audit({ action: 'auth.signin', category: AuditCategory.AUTH, result: AuditResult.SUCCESS, target: { type: 'user', id: user.id, name: user.email } }); }, signOut: async ({ user }) => { await audit({ action: 'auth.signout', category: AuditCategory.AUTH, result: AuditResult.SUCCESS, target: { type: 'user', id: user.id, name: user.email } }); } } } }); ``` -------------------------------- ### Configuring Stripe Plugin for Payment Event Enrichment Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Details the configuration for the built-in Stripe plugin. This plugin enriches payment-related audit events with additional metadata from the Stripe API, requiring a `stripe` client instance. ```typescript createStripePlugin({ stripe: stripeClient }); ``` -------------------------------- ### Manually Logging Audit Events with Ozhi in TypeScript Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Illustrates how to manually log an audit event using the `audit` function. It demonstrates capturing details like action, category, result, target entity, and changes (before/after states) for a user profile update, providing granular control over audit entries. ```typescript import { audit } from '@valink-solutions-ltd/ozhi'; import { AuditCategory, AuditResult } from '@valink-solutions-ltd/ozhi/types'; // Log a successful action await audit({ action: 'user.profile.updated', category: AuditCategory.DATA_MODIFICATION, result: AuditResult.SUCCESS, target: { type: 'user', id: userId, name: userEmail }, changes: { before: { role: 'user' }, after: { role: 'admin' }, fields: ['role'] } }); ``` -------------------------------- ### Querying Audit Logs with Built-in Builder in TypeScript Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Demonstrates how to use the `queryAuditLogs` function to retrieve audit events. It supports various filtering options such as `userId`, `category`, `severity`, `startDate`, `endDate`, `limit`, and `offset` for precise log retrieval. ```typescript import { queryAuditLogs } from '@valink-solutions-ltd/ozhi/query-builder'; const logs = await queryAuditLogs({ userId: 'user_123', category: AuditCategory.PAYMENT, severity: [AuditSeverity.HIGH, AuditSeverity.CRITICAL], startDate: new Date('2024-01-01'), endDate: new Date('2024-12-31'), limit: 50, offset: 0 }); ``` -------------------------------- ### Configuring Notification Plugin for High-Severity Alerts Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Shows how to configure the built-in notification plugin to send email alerts. It requires a `resend` client and a `notifyEmail`, and can be set to a `severityThreshold` to only alert for events at or above a specified level. ```typescript createNotificationPlugin({ resend: resendClient, notifyEmail: 'admin@yourdomain.com', severityThreshold: AuditSeverity.HIGH // Only alert for high and critical }); ``` -------------------------------- ### Importing Ozhi TypeScript Audit Types Source: https://github.com/valink-solutions/ozhi/blob/master/README.md Shows how to import essential TypeScript types from the `@valink-solutions-ltd/ozhi/types` module. These types provide strong typing for audit events, context, categories, severity, results, targets, changes, and plugins, enhancing code reliability and maintainability. ```typescript import type { AuditEvent, AuditContext, AuditCategory, AuditSeverity, AuditResult, AuditTarget, AuditChanges, AuditPlugin } from '@valink-solutions-ltd/ozhi/types'; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.