Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import type { JsonValue } from "./errors.js";
|
||||
|
||||
export type AuditEventInput = {
|
||||
tenantId?: string | null;
|
||||
actorId?: string | null;
|
||||
actorType?: string | null;
|
||||
action: string;
|
||||
targetType?: string | null;
|
||||
targetId?: string | null;
|
||||
requestId?: string | null;
|
||||
metadata?: Record<string, JsonValue> | null;
|
||||
};
|
||||
|
||||
export type AuditEventClient = {
|
||||
auditEvent: {
|
||||
create(args: {
|
||||
data: Required<Pick<AuditEventInput, "actorType" | "action">> & Omit<AuditEventInput, "actorType" | "action">;
|
||||
}): Promise<unknown>;
|
||||
};
|
||||
};
|
||||
|
||||
export async function auditEvent(input: AuditEventInput, client: AuditEventClient): Promise<unknown> {
|
||||
return client.auditEvent.create({
|
||||
data: {
|
||||
tenantId: input.tenantId ?? null,
|
||||
actorId: input.actorId ?? null,
|
||||
actorType: input.actorType?.trim() || "system",
|
||||
action: input.action,
|
||||
targetType: input.targetType ?? null,
|
||||
targetId: input.targetId ?? null,
|
||||
requestId: input.requestId ?? null,
|
||||
metadata: input.metadata ?? null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function withAuditMiddleware<TArgs extends readonly unknown[], TResult>(
|
||||
handler: (...args: TArgs) => Promise<TResult>,
|
||||
buildEvent: (result: TResult, args: TArgs) => AuditEventInput | null | undefined,
|
||||
client: AuditEventClient,
|
||||
): (...args: TArgs) => Promise<TResult> {
|
||||
return async (...args) => {
|
||||
const result = await handler(...args);
|
||||
const event = buildEvent(result, args);
|
||||
if (event) await auditEvent(event, client);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user