feat(core-shared/instrumentation): withCapture returns Captured<F>

This commit is contained in:
2026-05-12 21:32:42 +02:00
parent 5ac668497f
commit a1fbd16d83
2 changed files with 15 additions and 3 deletions

View File

@@ -1,7 +1,8 @@
import { describe, it, expect, vi } from "vitest";
import { describe, it, expect, expectTypeOf, vi } from "vitest";
import { withCapture } from "@/instrumentation/with-capture";
import type { ILogger } from "@/instrumentation/logger.interface";
import { isReported } from "@/instrumentation/reported-flag";
import type { Captured } from "@/conformance/brands";
function makeLogger(): ILogger & { captureException: ReturnType<typeof vi.fn> } {
return {
@@ -60,3 +61,12 @@ describe("withCapture", () => {
});
});
});
describe("withCapture — brand", () => {
it("returns a Captured<F>", () => {
const logger = makeLogger();
const fn = async (a: number) => a + 1;
const wrapped = withCapture(logger, { layer: "use-case" }, fn);
expectTypeOf(wrapped).toMatchTypeOf<Captured<typeof fn>>();
});
});

View File

@@ -1,4 +1,5 @@
import type { ILogger } from "./logger.interface";
import type { Captured } from "../conformance/brands";
import { isReported, markReported } from "./reported-flag";
/**
@@ -25,8 +26,8 @@ export function withCapture<Args extends unknown[], R>(
logger: ILogger,
tags: Record<string, string>,
fn: (...args: Args) => Promise<R>,
): (...args: Args) => Promise<R> {
return async (...args) => {
): Captured<(...args: Args) => Promise<R>> {
const wrapped: (...args: Args) => Promise<R> = async (...args) => {
try {
return await fn(...args);
} catch (err) {
@@ -37,4 +38,5 @@ export function withCapture<Args extends unknown[], R>(
throw err;
}
};
return wrapped as Captured<(...args: Args) => Promise<R>>;
}