@usebetterdev/audit-core 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +348 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +141 -9
- package/dist/index.d.ts +141 -9
- package/dist/index.js +334 -25
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -53,6 +53,8 @@ interface AuditQueryResult {
|
|
|
53
53
|
entries: AuditLog[];
|
|
54
54
|
/** If present, more results are available. Pass to `.after()` for the next page. */
|
|
55
55
|
nextCursor?: string;
|
|
56
|
+
/** If present, more results are available in the reverse direction. */
|
|
57
|
+
prevCursor?: string;
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
/** Callback that executes a query spec against the adapter. */
|
|
@@ -170,12 +172,6 @@ interface AuditContext {
|
|
|
170
172
|
/** Arbitrary key-value metadata. Redaction rules do not apply to metadata. */
|
|
171
173
|
metadata?: Record<string, unknown>;
|
|
172
174
|
}
|
|
173
|
-
/**
|
|
174
|
-
* Database adapter provided by ORM packages (e.g. drizzle, prisma).
|
|
175
|
-
* Passed as the `database` field in `BetterAuditConfig`.
|
|
176
|
-
*
|
|
177
|
-
* `writeLog` is required. `queryLogs` is optional — only needed when `query()` is used.
|
|
178
|
-
*/
|
|
179
175
|
/** Aggregated statistics returned by `AuditApi.getStats()`. */
|
|
180
176
|
interface AuditStats {
|
|
181
177
|
totalLogs: number;
|
|
@@ -195,12 +191,19 @@ interface AuditStats {
|
|
|
195
191
|
operationBreakdown: Record<string, number>;
|
|
196
192
|
severityBreakdown: Record<string, number>;
|
|
197
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Database adapter provided by ORM packages (e.g. drizzle, prisma).
|
|
196
|
+
* Passed as the `database` field in `BetterAuditConfig`.
|
|
197
|
+
*
|
|
198
|
+
* `writeLog` is required. `queryLogs` is optional — only needed when `query()` is used.
|
|
199
|
+
*/
|
|
198
200
|
interface AuditDatabaseAdapter {
|
|
199
201
|
writeLog(log: AuditLog): Promise<void>;
|
|
200
202
|
queryLogs?(spec: AuditQuerySpec): Promise<AuditQueryResult>;
|
|
201
203
|
getLogById?(id: string): Promise<AuditLog | null>;
|
|
202
204
|
getStats?(options?: {
|
|
203
205
|
since?: Date;
|
|
206
|
+
until?: Date;
|
|
204
207
|
}): Promise<AuditStats>;
|
|
205
208
|
purgeLogs?(options: {
|
|
206
209
|
before: Date;
|
|
@@ -414,7 +417,7 @@ declare function normalizeInput(operation: AuditOperation, before: Record<string
|
|
|
414
417
|
* ORM adapters translate this into their own migration format.
|
|
415
418
|
* Core never runs SQL — this is a declarative data structure only.
|
|
416
419
|
*/
|
|
417
|
-
type ColumnType = "uuid" | "timestamptz" | "text" | "jsonb" | "boolean";
|
|
420
|
+
type ColumnType = "uuid" | "timestamptz" | "text" | "jsonb" | "boolean" | "integer";
|
|
418
421
|
interface ColumnDefinition {
|
|
419
422
|
type: ColumnType;
|
|
420
423
|
nullable: boolean;
|
|
@@ -452,6 +455,120 @@ declare const AUDIT_LOG_SCHEMA: AuditLogSchema;
|
|
|
452
455
|
*/
|
|
453
456
|
declare function parseDuration(input: string, referenceDate?: Date): Date;
|
|
454
457
|
|
|
458
|
+
/**
|
|
459
|
+
* Encodes a cursor from a timestamp and id.
|
|
460
|
+
* Format: base64(JSON.stringify({ t: iso_string, i: id }))
|
|
461
|
+
*/
|
|
462
|
+
declare function encodeCursor(timestamp: Date, id: string): string;
|
|
463
|
+
/**
|
|
464
|
+
* Decodes an opaque cursor string back to timestamp + id.
|
|
465
|
+
* Throws on invalid input.
|
|
466
|
+
*/
|
|
467
|
+
declare function decodeCursor(cursor: string): {
|
|
468
|
+
timestamp: Date;
|
|
469
|
+
id: string;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Escapes LIKE/ILIKE wildcard characters so they match literally.
|
|
474
|
+
* Backslash is the default escape character in PostgreSQL and SQLite LIKE patterns.
|
|
475
|
+
*/
|
|
476
|
+
declare function escapeLikePattern(input: string): string;
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Resolves a `TimeFilter` to an absolute `Date`.
|
|
480
|
+
* Absolute dates are returned as-is; duration strings are parsed relative to now.
|
|
481
|
+
*/
|
|
482
|
+
declare function resolveTimeFilter(filter: TimeFilter): Date;
|
|
483
|
+
|
|
484
|
+
/** Column names that support simple equality / IN filtering. */
|
|
485
|
+
type AuditFilterField = "tableName" | "recordId" | "actorId" | "severity" | "operation";
|
|
486
|
+
/**
|
|
487
|
+
* Intermediate representation for a single filter condition.
|
|
488
|
+
*
|
|
489
|
+
* Adapters map each variant to their native expression type
|
|
490
|
+
* (Drizzle `SQL`, Prisma `QueryState` fragments, etc.).
|
|
491
|
+
*/
|
|
492
|
+
type FilterCondition = {
|
|
493
|
+
readonly kind: "eq";
|
|
494
|
+
readonly field: AuditFilterField;
|
|
495
|
+
readonly value: string;
|
|
496
|
+
} | {
|
|
497
|
+
readonly kind: "in";
|
|
498
|
+
readonly field: AuditFilterField;
|
|
499
|
+
readonly values: string[];
|
|
500
|
+
} | {
|
|
501
|
+
readonly kind: "timestampGte";
|
|
502
|
+
readonly value: Date;
|
|
503
|
+
} | {
|
|
504
|
+
readonly kind: "timestampLte";
|
|
505
|
+
readonly value: Date;
|
|
506
|
+
} | {
|
|
507
|
+
readonly kind: "search";
|
|
508
|
+
readonly pattern: string;
|
|
509
|
+
} | {
|
|
510
|
+
readonly kind: "compliance";
|
|
511
|
+
readonly tags: string[];
|
|
512
|
+
} | {
|
|
513
|
+
readonly kind: "cursor";
|
|
514
|
+
readonly timestamp: Date;
|
|
515
|
+
readonly id: string;
|
|
516
|
+
readonly sortOrder: "asc" | "desc";
|
|
517
|
+
};
|
|
518
|
+
interface InterpretFiltersOptions {
|
|
519
|
+
cursor?: {
|
|
520
|
+
timestamp: Date;
|
|
521
|
+
id: string;
|
|
522
|
+
} | undefined;
|
|
523
|
+
sortOrder?: "asc" | "desc" | undefined;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Converts `AuditQueryFilters` + optional cursor into a flat array of
|
|
527
|
+
* `FilterCondition` values. Pure, zero-dep, independently testable.
|
|
528
|
+
*
|
|
529
|
+
* Decision logic (shared across all adapters):
|
|
530
|
+
* - Array filters with 1 element → `eq`, >1 → `in`, 0/undefined → skip
|
|
531
|
+
* - Time filters → resolved to absolute `Date` via `resolveTimeFilter()`
|
|
532
|
+
* - Search text → escaped + wrapped with `%` for LIKE/ILIKE
|
|
533
|
+
* - Compliance tags → passed through (adapter handles JSONB vs json_each)
|
|
534
|
+
* - Cursor → emitted with sort order for adapter-specific comparison
|
|
535
|
+
*/
|
|
536
|
+
declare function interpretFilters(filters: AuditQueryFilters, options?: InterpretFiltersOptions): readonly FilterCondition[];
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Safely converts a count value (number, string, or bigint) to a JavaScript number.
|
|
540
|
+
* Handles PostgreSQL bigint-as-string, Prisma BigInt, and SQLite numeric returns.
|
|
541
|
+
*/
|
|
542
|
+
declare function toCount(value: unknown): number;
|
|
543
|
+
/**
|
|
544
|
+
* Assembles raw query result arrays into an `AuditStats` object.
|
|
545
|
+
* Works for both Date objects (PostgreSQL) and strings (SQLite, Prisma raw).
|
|
546
|
+
*/
|
|
547
|
+
declare function assembleStats(summaryRows: Array<{
|
|
548
|
+
totalLogs: unknown;
|
|
549
|
+
tablesAudited: unknown;
|
|
550
|
+
}>, eventsPerDayRows: Array<{
|
|
551
|
+
date: unknown;
|
|
552
|
+
count: unknown;
|
|
553
|
+
}>, topActorsRows: Array<{
|
|
554
|
+
actorId: unknown;
|
|
555
|
+
count: unknown;
|
|
556
|
+
}>, topTablesRows: Array<{
|
|
557
|
+
tableName: unknown;
|
|
558
|
+
count: unknown;
|
|
559
|
+
}>, operationRows: Array<{
|
|
560
|
+
operation: unknown;
|
|
561
|
+
count: unknown;
|
|
562
|
+
}>, severityRows: Array<{
|
|
563
|
+
severity: unknown;
|
|
564
|
+
count: unknown;
|
|
565
|
+
}>): AuditStats;
|
|
566
|
+
|
|
567
|
+
declare const VALID_OPERATIONS: ReadonlySet<string>;
|
|
568
|
+
declare const VALID_SEVERITIES: ReadonlySet<string>;
|
|
569
|
+
declare function isAuditOperation(value: string): value is AuditOperation;
|
|
570
|
+
declare function isAuditSeverity(value: string): value is AuditSeverity;
|
|
571
|
+
|
|
455
572
|
/**
|
|
456
573
|
* Core export engine. Fetches rows in cursor-paginated batches and writes
|
|
457
574
|
* them to the sink as CSV or JSON. Memory stays flat regardless of total rows.
|
|
@@ -482,6 +599,7 @@ declare class EnrichmentRegistry {
|
|
|
482
599
|
/** Flat console-friendly query filters. Single-value fields translated to multi-value internal filters. */
|
|
483
600
|
interface ConsoleQueryFilters {
|
|
484
601
|
tableName?: string;
|
|
602
|
+
recordId?: string;
|
|
485
603
|
operation?: string;
|
|
486
604
|
actorId?: string;
|
|
487
605
|
severity?: string;
|
|
@@ -491,6 +609,10 @@ interface ConsoleQueryFilters {
|
|
|
491
609
|
search?: string;
|
|
492
610
|
limit?: number;
|
|
493
611
|
cursor?: string;
|
|
612
|
+
/** Pagination direction: "after" fetches older entries (default), "before" fetches newer entries. */
|
|
613
|
+
direction?: "after" | "before";
|
|
614
|
+
/** Sort order: "asc" (oldest first) or "desc" (newest first). Overrides direction-based sort when both present. */
|
|
615
|
+
order?: "asc" | "desc";
|
|
494
616
|
}
|
|
495
617
|
/** Serializable summary of an enrichment config (function fields stripped). */
|
|
496
618
|
interface EnrichmentSummary {
|
|
@@ -503,9 +625,10 @@ interface EnrichmentSummary {
|
|
|
503
625
|
redact?: string[];
|
|
504
626
|
include?: string[];
|
|
505
627
|
}
|
|
506
|
-
/** Query result extended with
|
|
628
|
+
/** Query result extended with convenience pagination flags. */
|
|
507
629
|
interface ConsoleQueryResult extends AuditQueryResult {
|
|
508
630
|
hasNextPage: boolean;
|
|
631
|
+
hasPrevPage: boolean;
|
|
509
632
|
}
|
|
510
633
|
/**
|
|
511
634
|
* High-level API consumed by console endpoints.
|
|
@@ -516,11 +639,14 @@ interface ConsoleQueryResult extends AuditQueryResult {
|
|
|
516
639
|
interface AuditApi {
|
|
517
640
|
/** Query audit log entries with optional flat filters and cursor-based pagination. */
|
|
518
641
|
queryLogs(filters?: ConsoleQueryFilters): Promise<ConsoleQueryResult>;
|
|
642
|
+
/** Query audit log entries centered around a specific log ID. */
|
|
643
|
+
queryLogsAround(anchorId: string, filters?: ConsoleQueryFilters): Promise<ConsoleQueryResult>;
|
|
519
644
|
/** Retrieve a single audit log entry by its ID. Returns `null` when not found. */
|
|
520
645
|
getLog(id: string): Promise<AuditLog | null>;
|
|
521
646
|
/** Get aggregated audit statistics. Requires adapter.getStats. */
|
|
522
647
|
getStats(options?: {
|
|
523
648
|
since?: Date;
|
|
649
|
+
until?: Date;
|
|
524
650
|
}): Promise<AuditStats>;
|
|
525
651
|
/** Get serializable summaries of all registered enrichments. */
|
|
526
652
|
getEnrichments(): EnrichmentSummary[];
|
|
@@ -585,6 +711,12 @@ declare function fromCookie(cookieName: string): ValueExtractor;
|
|
|
585
711
|
* @param headerName - Header name (case-insensitive per the Web API)
|
|
586
712
|
*/
|
|
587
713
|
declare function fromHeader(headerName: string): ValueExtractor;
|
|
714
|
+
/** Default extractor: reads `sub` from `Authorization: Bearer <jwt>` as actor. */
|
|
715
|
+
declare const defaultExtractor: ContextExtractor;
|
|
716
|
+
/**
|
|
717
|
+
* Runs an extractor safely, catching errors and returning undefined on failure.
|
|
718
|
+
*/
|
|
719
|
+
declare function safeExtract(extractor: ValueExtractor | undefined, request: Request, onError: ((error: unknown) => void) | undefined): Promise<string | undefined>;
|
|
588
720
|
/**
|
|
589
721
|
* Shared middleware handler used by all framework adapters.
|
|
590
722
|
*
|
|
@@ -597,4 +729,4 @@ declare function fromHeader(headerName: string): ValueExtractor;
|
|
|
597
729
|
*/
|
|
598
730
|
declare function handleMiddleware(extractor: ContextExtractor, request: Request, next: () => Promise<void>, options?: MiddlewareHandlerOptions): Promise<void>;
|
|
599
731
|
|
|
600
|
-
export { AUDIT_LOG_SCHEMA, type AfterLogHook, type AuditApi, type AuditContext, type AuditDatabaseAdapter, type AuditLog, type AuditLogColumnName, type AuditLogSchema, type AuditOperation, AuditQueryBuilder, type AuditQueryFilters, type AuditQueryResult, type AuditQuerySpec, type AuditSeverity, type AuditStats, type BeforeLogHook, type BetterAuditConfig, type BetterAuditInstance, type CaptureLogInput, type ColumnDefinition, type ColumnType, type ConsoleQueryFilters, type ConsoleQueryResult, type ContextExtractor, type EnrichmentConfig, type EnrichmentDescriptionContext, type EnrichmentSummary, type ExportOptions, type ExportResponseOptions, type ExportResult, type MiddlewareHandlerOptions, type QueryExecutor, type ResourceFilter, type TimeFilter, type ValueExtractor, betterAudit, createAuditApi, createAuditConsoleEndpoints, createExportResponse, fromBearerToken, fromCookie, fromHeader, getAuditContext, handleMiddleware, mergeAuditContext, normalizeInput, parseDuration, runExport, runWithAuditContext };
|
|
732
|
+
export { AUDIT_LOG_SCHEMA, type AfterLogHook, type AuditApi, type AuditContext, type AuditDatabaseAdapter, type AuditFilterField, type AuditLog, type AuditLogColumnName, type AuditLogSchema, type AuditOperation, AuditQueryBuilder, type AuditQueryFilters, type AuditQueryResult, type AuditQuerySpec, type AuditSeverity, type AuditStats, type BeforeLogHook, type BetterAuditConfig, type BetterAuditInstance, type CaptureLogInput, type ColumnDefinition, type ColumnType, type ConsoleQueryFilters, type ConsoleQueryResult, type ContextExtractor, type EnrichmentConfig, type EnrichmentDescriptionContext, type EnrichmentSummary, type ExportOptions, type ExportResponseOptions, type ExportResult, type FilterCondition, type InterpretFiltersOptions, type MiddlewareHandlerOptions, type QueryExecutor, type ResourceFilter, type TimeFilter, VALID_OPERATIONS, VALID_SEVERITIES, type ValueExtractor, assembleStats, betterAudit, createAuditApi, createAuditConsoleEndpoints, createExportResponse, decodeCursor, defaultExtractor, encodeCursor, escapeLikePattern, fromBearerToken, fromCookie, fromHeader, getAuditContext, handleMiddleware, interpretFilters, isAuditOperation, isAuditSeverity, mergeAuditContext, normalizeInput, parseDuration, resolveTimeFilter, runExport, runWithAuditContext, safeExtract, toCount };
|
package/dist/index.d.ts
CHANGED
|
@@ -53,6 +53,8 @@ interface AuditQueryResult {
|
|
|
53
53
|
entries: AuditLog[];
|
|
54
54
|
/** If present, more results are available. Pass to `.after()` for the next page. */
|
|
55
55
|
nextCursor?: string;
|
|
56
|
+
/** If present, more results are available in the reverse direction. */
|
|
57
|
+
prevCursor?: string;
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
/** Callback that executes a query spec against the adapter. */
|
|
@@ -170,12 +172,6 @@ interface AuditContext {
|
|
|
170
172
|
/** Arbitrary key-value metadata. Redaction rules do not apply to metadata. */
|
|
171
173
|
metadata?: Record<string, unknown>;
|
|
172
174
|
}
|
|
173
|
-
/**
|
|
174
|
-
* Database adapter provided by ORM packages (e.g. drizzle, prisma).
|
|
175
|
-
* Passed as the `database` field in `BetterAuditConfig`.
|
|
176
|
-
*
|
|
177
|
-
* `writeLog` is required. `queryLogs` is optional — only needed when `query()` is used.
|
|
178
|
-
*/
|
|
179
175
|
/** Aggregated statistics returned by `AuditApi.getStats()`. */
|
|
180
176
|
interface AuditStats {
|
|
181
177
|
totalLogs: number;
|
|
@@ -195,12 +191,19 @@ interface AuditStats {
|
|
|
195
191
|
operationBreakdown: Record<string, number>;
|
|
196
192
|
severityBreakdown: Record<string, number>;
|
|
197
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Database adapter provided by ORM packages (e.g. drizzle, prisma).
|
|
196
|
+
* Passed as the `database` field in `BetterAuditConfig`.
|
|
197
|
+
*
|
|
198
|
+
* `writeLog` is required. `queryLogs` is optional — only needed when `query()` is used.
|
|
199
|
+
*/
|
|
198
200
|
interface AuditDatabaseAdapter {
|
|
199
201
|
writeLog(log: AuditLog): Promise<void>;
|
|
200
202
|
queryLogs?(spec: AuditQuerySpec): Promise<AuditQueryResult>;
|
|
201
203
|
getLogById?(id: string): Promise<AuditLog | null>;
|
|
202
204
|
getStats?(options?: {
|
|
203
205
|
since?: Date;
|
|
206
|
+
until?: Date;
|
|
204
207
|
}): Promise<AuditStats>;
|
|
205
208
|
purgeLogs?(options: {
|
|
206
209
|
before: Date;
|
|
@@ -414,7 +417,7 @@ declare function normalizeInput(operation: AuditOperation, before: Record<string
|
|
|
414
417
|
* ORM adapters translate this into their own migration format.
|
|
415
418
|
* Core never runs SQL — this is a declarative data structure only.
|
|
416
419
|
*/
|
|
417
|
-
type ColumnType = "uuid" | "timestamptz" | "text" | "jsonb" | "boolean";
|
|
420
|
+
type ColumnType = "uuid" | "timestamptz" | "text" | "jsonb" | "boolean" | "integer";
|
|
418
421
|
interface ColumnDefinition {
|
|
419
422
|
type: ColumnType;
|
|
420
423
|
nullable: boolean;
|
|
@@ -452,6 +455,120 @@ declare const AUDIT_LOG_SCHEMA: AuditLogSchema;
|
|
|
452
455
|
*/
|
|
453
456
|
declare function parseDuration(input: string, referenceDate?: Date): Date;
|
|
454
457
|
|
|
458
|
+
/**
|
|
459
|
+
* Encodes a cursor from a timestamp and id.
|
|
460
|
+
* Format: base64(JSON.stringify({ t: iso_string, i: id }))
|
|
461
|
+
*/
|
|
462
|
+
declare function encodeCursor(timestamp: Date, id: string): string;
|
|
463
|
+
/**
|
|
464
|
+
* Decodes an opaque cursor string back to timestamp + id.
|
|
465
|
+
* Throws on invalid input.
|
|
466
|
+
*/
|
|
467
|
+
declare function decodeCursor(cursor: string): {
|
|
468
|
+
timestamp: Date;
|
|
469
|
+
id: string;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Escapes LIKE/ILIKE wildcard characters so they match literally.
|
|
474
|
+
* Backslash is the default escape character in PostgreSQL and SQLite LIKE patterns.
|
|
475
|
+
*/
|
|
476
|
+
declare function escapeLikePattern(input: string): string;
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Resolves a `TimeFilter` to an absolute `Date`.
|
|
480
|
+
* Absolute dates are returned as-is; duration strings are parsed relative to now.
|
|
481
|
+
*/
|
|
482
|
+
declare function resolveTimeFilter(filter: TimeFilter): Date;
|
|
483
|
+
|
|
484
|
+
/** Column names that support simple equality / IN filtering. */
|
|
485
|
+
type AuditFilterField = "tableName" | "recordId" | "actorId" | "severity" | "operation";
|
|
486
|
+
/**
|
|
487
|
+
* Intermediate representation for a single filter condition.
|
|
488
|
+
*
|
|
489
|
+
* Adapters map each variant to their native expression type
|
|
490
|
+
* (Drizzle `SQL`, Prisma `QueryState` fragments, etc.).
|
|
491
|
+
*/
|
|
492
|
+
type FilterCondition = {
|
|
493
|
+
readonly kind: "eq";
|
|
494
|
+
readonly field: AuditFilterField;
|
|
495
|
+
readonly value: string;
|
|
496
|
+
} | {
|
|
497
|
+
readonly kind: "in";
|
|
498
|
+
readonly field: AuditFilterField;
|
|
499
|
+
readonly values: string[];
|
|
500
|
+
} | {
|
|
501
|
+
readonly kind: "timestampGte";
|
|
502
|
+
readonly value: Date;
|
|
503
|
+
} | {
|
|
504
|
+
readonly kind: "timestampLte";
|
|
505
|
+
readonly value: Date;
|
|
506
|
+
} | {
|
|
507
|
+
readonly kind: "search";
|
|
508
|
+
readonly pattern: string;
|
|
509
|
+
} | {
|
|
510
|
+
readonly kind: "compliance";
|
|
511
|
+
readonly tags: string[];
|
|
512
|
+
} | {
|
|
513
|
+
readonly kind: "cursor";
|
|
514
|
+
readonly timestamp: Date;
|
|
515
|
+
readonly id: string;
|
|
516
|
+
readonly sortOrder: "asc" | "desc";
|
|
517
|
+
};
|
|
518
|
+
interface InterpretFiltersOptions {
|
|
519
|
+
cursor?: {
|
|
520
|
+
timestamp: Date;
|
|
521
|
+
id: string;
|
|
522
|
+
} | undefined;
|
|
523
|
+
sortOrder?: "asc" | "desc" | undefined;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Converts `AuditQueryFilters` + optional cursor into a flat array of
|
|
527
|
+
* `FilterCondition` values. Pure, zero-dep, independently testable.
|
|
528
|
+
*
|
|
529
|
+
* Decision logic (shared across all adapters):
|
|
530
|
+
* - Array filters with 1 element → `eq`, >1 → `in`, 0/undefined → skip
|
|
531
|
+
* - Time filters → resolved to absolute `Date` via `resolveTimeFilter()`
|
|
532
|
+
* - Search text → escaped + wrapped with `%` for LIKE/ILIKE
|
|
533
|
+
* - Compliance tags → passed through (adapter handles JSONB vs json_each)
|
|
534
|
+
* - Cursor → emitted with sort order for adapter-specific comparison
|
|
535
|
+
*/
|
|
536
|
+
declare function interpretFilters(filters: AuditQueryFilters, options?: InterpretFiltersOptions): readonly FilterCondition[];
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Safely converts a count value (number, string, or bigint) to a JavaScript number.
|
|
540
|
+
* Handles PostgreSQL bigint-as-string, Prisma BigInt, and SQLite numeric returns.
|
|
541
|
+
*/
|
|
542
|
+
declare function toCount(value: unknown): number;
|
|
543
|
+
/**
|
|
544
|
+
* Assembles raw query result arrays into an `AuditStats` object.
|
|
545
|
+
* Works for both Date objects (PostgreSQL) and strings (SQLite, Prisma raw).
|
|
546
|
+
*/
|
|
547
|
+
declare function assembleStats(summaryRows: Array<{
|
|
548
|
+
totalLogs: unknown;
|
|
549
|
+
tablesAudited: unknown;
|
|
550
|
+
}>, eventsPerDayRows: Array<{
|
|
551
|
+
date: unknown;
|
|
552
|
+
count: unknown;
|
|
553
|
+
}>, topActorsRows: Array<{
|
|
554
|
+
actorId: unknown;
|
|
555
|
+
count: unknown;
|
|
556
|
+
}>, topTablesRows: Array<{
|
|
557
|
+
tableName: unknown;
|
|
558
|
+
count: unknown;
|
|
559
|
+
}>, operationRows: Array<{
|
|
560
|
+
operation: unknown;
|
|
561
|
+
count: unknown;
|
|
562
|
+
}>, severityRows: Array<{
|
|
563
|
+
severity: unknown;
|
|
564
|
+
count: unknown;
|
|
565
|
+
}>): AuditStats;
|
|
566
|
+
|
|
567
|
+
declare const VALID_OPERATIONS: ReadonlySet<string>;
|
|
568
|
+
declare const VALID_SEVERITIES: ReadonlySet<string>;
|
|
569
|
+
declare function isAuditOperation(value: string): value is AuditOperation;
|
|
570
|
+
declare function isAuditSeverity(value: string): value is AuditSeverity;
|
|
571
|
+
|
|
455
572
|
/**
|
|
456
573
|
* Core export engine. Fetches rows in cursor-paginated batches and writes
|
|
457
574
|
* them to the sink as CSV or JSON. Memory stays flat regardless of total rows.
|
|
@@ -482,6 +599,7 @@ declare class EnrichmentRegistry {
|
|
|
482
599
|
/** Flat console-friendly query filters. Single-value fields translated to multi-value internal filters. */
|
|
483
600
|
interface ConsoleQueryFilters {
|
|
484
601
|
tableName?: string;
|
|
602
|
+
recordId?: string;
|
|
485
603
|
operation?: string;
|
|
486
604
|
actorId?: string;
|
|
487
605
|
severity?: string;
|
|
@@ -491,6 +609,10 @@ interface ConsoleQueryFilters {
|
|
|
491
609
|
search?: string;
|
|
492
610
|
limit?: number;
|
|
493
611
|
cursor?: string;
|
|
612
|
+
/** Pagination direction: "after" fetches older entries (default), "before" fetches newer entries. */
|
|
613
|
+
direction?: "after" | "before";
|
|
614
|
+
/** Sort order: "asc" (oldest first) or "desc" (newest first). Overrides direction-based sort when both present. */
|
|
615
|
+
order?: "asc" | "desc";
|
|
494
616
|
}
|
|
495
617
|
/** Serializable summary of an enrichment config (function fields stripped). */
|
|
496
618
|
interface EnrichmentSummary {
|
|
@@ -503,9 +625,10 @@ interface EnrichmentSummary {
|
|
|
503
625
|
redact?: string[];
|
|
504
626
|
include?: string[];
|
|
505
627
|
}
|
|
506
|
-
/** Query result extended with
|
|
628
|
+
/** Query result extended with convenience pagination flags. */
|
|
507
629
|
interface ConsoleQueryResult extends AuditQueryResult {
|
|
508
630
|
hasNextPage: boolean;
|
|
631
|
+
hasPrevPage: boolean;
|
|
509
632
|
}
|
|
510
633
|
/**
|
|
511
634
|
* High-level API consumed by console endpoints.
|
|
@@ -516,11 +639,14 @@ interface ConsoleQueryResult extends AuditQueryResult {
|
|
|
516
639
|
interface AuditApi {
|
|
517
640
|
/** Query audit log entries with optional flat filters and cursor-based pagination. */
|
|
518
641
|
queryLogs(filters?: ConsoleQueryFilters): Promise<ConsoleQueryResult>;
|
|
642
|
+
/** Query audit log entries centered around a specific log ID. */
|
|
643
|
+
queryLogsAround(anchorId: string, filters?: ConsoleQueryFilters): Promise<ConsoleQueryResult>;
|
|
519
644
|
/** Retrieve a single audit log entry by its ID. Returns `null` when not found. */
|
|
520
645
|
getLog(id: string): Promise<AuditLog | null>;
|
|
521
646
|
/** Get aggregated audit statistics. Requires adapter.getStats. */
|
|
522
647
|
getStats(options?: {
|
|
523
648
|
since?: Date;
|
|
649
|
+
until?: Date;
|
|
524
650
|
}): Promise<AuditStats>;
|
|
525
651
|
/** Get serializable summaries of all registered enrichments. */
|
|
526
652
|
getEnrichments(): EnrichmentSummary[];
|
|
@@ -585,6 +711,12 @@ declare function fromCookie(cookieName: string): ValueExtractor;
|
|
|
585
711
|
* @param headerName - Header name (case-insensitive per the Web API)
|
|
586
712
|
*/
|
|
587
713
|
declare function fromHeader(headerName: string): ValueExtractor;
|
|
714
|
+
/** Default extractor: reads `sub` from `Authorization: Bearer <jwt>` as actor. */
|
|
715
|
+
declare const defaultExtractor: ContextExtractor;
|
|
716
|
+
/**
|
|
717
|
+
* Runs an extractor safely, catching errors and returning undefined on failure.
|
|
718
|
+
*/
|
|
719
|
+
declare function safeExtract(extractor: ValueExtractor | undefined, request: Request, onError: ((error: unknown) => void) | undefined): Promise<string | undefined>;
|
|
588
720
|
/**
|
|
589
721
|
* Shared middleware handler used by all framework adapters.
|
|
590
722
|
*
|
|
@@ -597,4 +729,4 @@ declare function fromHeader(headerName: string): ValueExtractor;
|
|
|
597
729
|
*/
|
|
598
730
|
declare function handleMiddleware(extractor: ContextExtractor, request: Request, next: () => Promise<void>, options?: MiddlewareHandlerOptions): Promise<void>;
|
|
599
731
|
|
|
600
|
-
export { AUDIT_LOG_SCHEMA, type AfterLogHook, type AuditApi, type AuditContext, type AuditDatabaseAdapter, type AuditLog, type AuditLogColumnName, type AuditLogSchema, type AuditOperation, AuditQueryBuilder, type AuditQueryFilters, type AuditQueryResult, type AuditQuerySpec, type AuditSeverity, type AuditStats, type BeforeLogHook, type BetterAuditConfig, type BetterAuditInstance, type CaptureLogInput, type ColumnDefinition, type ColumnType, type ConsoleQueryFilters, type ConsoleQueryResult, type ContextExtractor, type EnrichmentConfig, type EnrichmentDescriptionContext, type EnrichmentSummary, type ExportOptions, type ExportResponseOptions, type ExportResult, type MiddlewareHandlerOptions, type QueryExecutor, type ResourceFilter, type TimeFilter, type ValueExtractor, betterAudit, createAuditApi, createAuditConsoleEndpoints, createExportResponse, fromBearerToken, fromCookie, fromHeader, getAuditContext, handleMiddleware, mergeAuditContext, normalizeInput, parseDuration, runExport, runWithAuditContext };
|
|
732
|
+
export { AUDIT_LOG_SCHEMA, type AfterLogHook, type AuditApi, type AuditContext, type AuditDatabaseAdapter, type AuditFilterField, type AuditLog, type AuditLogColumnName, type AuditLogSchema, type AuditOperation, AuditQueryBuilder, type AuditQueryFilters, type AuditQueryResult, type AuditQuerySpec, type AuditSeverity, type AuditStats, type BeforeLogHook, type BetterAuditConfig, type BetterAuditInstance, type CaptureLogInput, type ColumnDefinition, type ColumnType, type ConsoleQueryFilters, type ConsoleQueryResult, type ContextExtractor, type EnrichmentConfig, type EnrichmentDescriptionContext, type EnrichmentSummary, type ExportOptions, type ExportResponseOptions, type ExportResult, type FilterCondition, type InterpretFiltersOptions, type MiddlewareHandlerOptions, type QueryExecutor, type ResourceFilter, type TimeFilter, VALID_OPERATIONS, VALID_SEVERITIES, type ValueExtractor, assembleStats, betterAudit, createAuditApi, createAuditConsoleEndpoints, createExportResponse, decodeCursor, defaultExtractor, encodeCursor, escapeLikePattern, fromBearerToken, fromCookie, fromHeader, getAuditContext, handleMiddleware, interpretFilters, isAuditOperation, isAuditSeverity, mergeAuditContext, normalizeInput, parseDuration, resolveTimeFilter, runExport, runWithAuditContext, safeExtract, toCount };
|