feat(core-audit): withAudit wraps and attaches runtime __audited marker
This commit is contained in:
@@ -1,25 +1,24 @@
|
||||
import type { IAuditLog } from "./audit-log.interface";
|
||||
import { attachBrand } from "@repo/core-shared/conformance";
|
||||
|
||||
/**
|
||||
* Phantom-type brand attached at wrap time by `withAudit`. The conformance
|
||||
* system uses this as the type-level seam for mutating use cases that
|
||||
* declare `audits: [...]` in their manifest — without `__audited`, the
|
||||
* binding is not assignable to `ProductionUseCase<I, O, M>` when M demands
|
||||
* it.
|
||||
* it. At runtime the brand is a non-enumerable property attached by
|
||||
* `attachBrand` from `@repo/core-shared/conformance`, so the boot-time
|
||||
* assertion can verify the binding went through the audit-aware path.
|
||||
*/
|
||||
export type Audited<F> = F & { readonly __audited: true };
|
||||
|
||||
/**
|
||||
* Use-case wrapper applied at DI bind time. In milestone i this is a
|
||||
* brand-only attachment: it does not yet automatically call `auditLog.record`.
|
||||
* Use cases continue to call `auditLog.record(...)` in their own bodies; the
|
||||
* wrapper exists to make "binding was bound through the audit-aware path"
|
||||
* type-checkable at compile time.
|
||||
*
|
||||
* A future story may move auditing logic out of factory bodies and into the
|
||||
* wrapper itself (driven by manifest declarations) — but that requires the
|
||||
* manifest's `audits[]` entries to fully specify what gets recorded, which
|
||||
* is out of scope here.
|
||||
* Use-case wrapper applied at DI bind time. The wrapper is a thin closure
|
||||
* that forwards to `fn` unchanged and carries the `__audited` brand. The
|
||||
* forward closure (instead of returning `fn` directly) keeps the brand on
|
||||
* a fresh function so the caller's original `fn` is not mutated — important
|
||||
* when the same factory output is used elsewhere unwrapped (dev-seed paths,
|
||||
* tests).
|
||||
*/
|
||||
export function withAudit<Args extends unknown[], R>(
|
||||
// TODO(conformance milestone iii+): wire automated recording from manifest
|
||||
@@ -31,5 +30,7 @@ export function withAudit<Args extends unknown[], R>(
|
||||
fn: (...args: Args) => Promise<R>,
|
||||
): Audited<(...args: Args) => Promise<R>> {
|
||||
void auditLog;
|
||||
return fn as Audited<(...args: Args) => Promise<R>>;
|
||||
const wrapped: (...args: Args) => Promise<R> = (...args) => fn(...args);
|
||||
attachBrand(wrapped, "__audited");
|
||||
return wrapped as Audited<(...args: Args) => Promise<R>>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user