Files
WRNexusJSDoc/app/pages/packages/tracking.wrn
T

158 lines
11 KiB
Plaintext

page wrnexustracking {
seo {
title = "@wrnexus/tracking"
description = "Error/event capture, middleware, filtering, and sinks."
}
view {
<div class="docs-shell">
<header class="topbar">
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
<nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
<button data-wire-theme-toggle class="theme-button" aria-label="Toggle theme">Theme</button>
</header>
<main class="page package-page">
<aside class="sidebar"><a href="/packages">← All packages</a><span class="category">Runtime</span><h1>@wrnexus/tracking</h1><p>Error/event capture, middleware, filtering, and sinks.</p><code>bun add @wrnexus/tracking@0.2.14</code><nav><a href="#guide">Guide</a><a href="#api">Complete API</a></nav></aside>
<article class="documentation"><section class="doc-intro"><span class="eyebrow">Runtime</span><h1>@wrnexus/tracking</h1><p>Error/event capture, middleware, filtering, and sinks.</p><pre><code>bun add @wrnexus/tracking@0.2.14</code></pre></section><section id="guide" class="prose"><blockquote>Error tracking for WRNexusJS apps: capture exceptions manually or via middleware and fan them out to pluggable sinks.</blockquote>
<p>Part of the <strong>WRNexusJS</strong> framework — an SSR-first, Bun-native full-stack web framework.</p>
<h3 id="overview">Overview</h3>
<p><code>@wrnexus/tracking</code> is a small, server-side error-capture layer. You create a tracker with one or more <strong>sinks</strong>, then feed it errors — either manually with <code>tracker.capture(err, context)</code> or automatically by mounting <code>tracker.middleware()</code> in your request pipeline. A <code>consoleSink</code> is included; forwarding to Sentry, Datadog, or any other backend is just a matter of writing a tiny sink. Reach for it when you want a single, sink-agnostic place to route application errors. Sinks run best-effort — a throwing sink never breaks the request.</p>
<h3 id="installation">Installation</h3>
<pre data-language="bash"><code>bun add @wrnexus/tracking</code></pre>
<blockquote>Private package — the machine must be authenticated to the <code>wrnexus</code> npm org</blockquote>
<blockquote>(a read token in <code>~/.npmrc</code>). Requires <strong>Bun</strong> (Node is not supported).</blockquote>
<h3 id="api">API</h3>
<h4 id="createtracker-options-tracker"><code>createTracker(options?): Tracker</code></h4>
<p>Creates a tracker. <code>TrackerOptions</code>:</p>
<div class="table-wrap"><table>
<thead><tr><th>Option</th><th>Type</th><th>Description</th></tr></thead>
<tbody><tr><td><code>sinks</code></td><td><code>ErrorSink[]</code></td><td>Initial sinks to fan events out to. Defaults to <code>[]</code>.</td></tr><tr><td><code>now</code></td><td><code>() =&gt; number</code></td><td>Clock used for <code>event.timestamp</code> (epoch ms). Defaults to <code>Date.now</code>.</td></tr><tr><td><code>beforeSend</code></td><td>`(event: ErrorEvent) =&gt; ErrorEvent \</td><td>null`</td><td>Scrub/enrich an event before it reaches any sink. Return <code>null</code> to drop it.</td></tr></tbody></table></div>
<p>The returned <code>Tracker</code>:</p>
<div class="table-wrap"><table>
<thead><tr><th>Member</th><th>Signature</th><th>Description</th></tr></thead>
<tbody><tr><td><code>capture</code></td><td><code>(error: unknown, context?: Record&lt;string, unknown&gt;) =&gt; Promise&lt;void&gt;</code></td><td>Normalizes any thrown value into an <code>Error</code>, builds an <code>ErrorEvent</code>, runs <code>beforeSend</code>, then dispatches to all sinks. Non-<code>Error</code> values are wrapped in an <code>Error</code> named <code>NonError</code>.</td></tr><tr><td><code>addSink</code></td><td><code>(sink: ErrorSink) =&gt; void</code></td><td>Registers an additional sink at runtime.</td></tr><tr><td><code>middleware</code></td><td><code>() =&gt; Middleware</code></td><td>Returns a WRNexusJS <code>Middleware</code> that captures any error thrown downstream, then re-throws it so the framework's error handler still produces the response.</td></tr></tbody></table></div>
<p>The middleware attaches this context to captured events:</p>
<pre data-language="ts"><code>&#123; method: ctx.req.method, path: ctx.url.pathname, requestId: ctx.locals.requestId &#125;</code></pre>
<h4 id="consolesink-errorsink"><code>consoleSink: ErrorSink</code></h4>
<p>A built-in sink that logs a compact one-line message via <code>console.error</code>, e.g. <code>[error] TypeError: cannot read x &#123;&quot;userId&quot;:42&#125;</code>.</p>
<h4 id="types">Types</h4>
<pre data-language="ts"><code>interface ErrorEvent &#123;
error: Error;
context: Record&lt;string, unknown&gt;; // request info, user id, tags…
timestamp: number; // epoch ms
&#125;
interface ErrorSink &#123;
name?: string;
capture(event: ErrorEvent): void | Promise&lt;void&gt;;
&#125;</code></pre>
<h3 id="usage">Usage</h3>
<p>Manual capture:</p>
<pre data-language="ts"><code>import &#123; createTracker, consoleSink &#125; from &quot;@wrnexus/tracking&quot;;
const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
try &#123;
await doWork();
&#125; catch (err) &#123;
await tracker.capture(err, &#123; userId: 42, op: &quot;doWork&quot; &#125;);
throw err;
&#125;</code></pre>
<p>As request middleware:</p>
<pre data-language="ts"><code>import &#123; createTracker, consoleSink &#125; from &quot;@wrnexus/tracking&quot;;
const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
app.use(tracker.middleware()); // captures + re-throws downstream errors</code></pre>
<p>A custom sink with <code>beforeSend</code> scrubbing:</p>
<pre data-language="ts"><code>import &#123; createTracker, type ErrorSink &#125; from &quot;@wrnexus/tracking&quot;;
const sentrySink: ErrorSink = &#123;
name: &quot;sentry&quot;,
async capture(event) &#123;
await Sentry.captureException(event.error, &#123; extra: event.context &#125;);
&#125;,
&#125;;
const tracker = createTracker(&#123;
sinks: [sentrySink],
beforeSend(event) &#123;
delete event.context.password; // scrub secrets
return event; // return null to drop the event entirely
&#125;,
&#125;);
tracker.addSink(anotherSink); // add more sinks later</code></pre>
<h3 id="requirements-notes">Requirements / Notes</h3>
<ul>
<li>Runs on <strong>Bun</strong> only (Node is not supported).</li>
<li>Peer package: [<code>@wrnexus/core</code>](../core) — the <code>Context</code> and <code>Middleware</code> types</li>
<p>used by <code>tracker.middleware()</code> come from there.</p>
<li>Sink dispatch is fire-and-forget-safe: all sinks run via <code>Promise.all</code>, and a</li>
<p>sink that throws is swallowed so it can never break the app.</p>
</ul></section><section id="api" class="prose api"><h2>Complete TypeScript API</h2><p>This declaration is generated from the exact published package and lists its exported functions, classes, interfaces, and types.</p><pre data-language="typescript"><code>import &#123; Middleware &#125; from '@wrnexus/core';
/**
* @wrnexus/tracking — error tracking with pluggable sinks. Capture exceptions
* manually or via middleware, and fan them out to any sink (console by default;
* write a small sink to forward to Sentry/Datadog/etc.).
*
* const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
* app-middleware: tracker.middleware() // captures + re-throws request errors
* tracker.capture(err, &#123; userId &#125;); // manual
*/
interface ErrorEvent &#123;
error: Error;
/** Arbitrary structured context (request info, user id, tags…). */
context: Record&lt;string, unknown&gt;;
/** Epoch ms. */
timestamp: number;
&#125;
interface ErrorSink &#123;
name?: string;
capture(event: ErrorEvent): void | Promise&lt;void&gt;;
&#125;
interface Tracker &#123;
capture(error: unknown, context?: Record&lt;string, unknown&gt;): Promise&lt;void&gt;;
addSink(sink: ErrorSink): void;
/** Middleware that captures errors thrown downstream, then re-throws them. */
middleware(): Middleware;
&#125;
interface TrackerOptions &#123;
sinks?: ErrorSink[];
now?: () =&gt; number;
/** Scrub/enrich an event before it hits sinks (return null to drop it). */
beforeSend?: (event: ErrorEvent) =&gt; ErrorEvent | null;
&#125;
/** A sink that logs a compact one-line error to the console. */
declare const consoleSink: ErrorSink;
declare function createTracker(options?: TrackerOptions): Tracker;
export &#123; type ErrorEvent, type ErrorSink, type Tracker, type TrackerOptions, consoleSink, createTracker &#125;;
</code></pre></section><section id="examples" class="prose examples"><h2>Examples</h2><p>Copy-ready examples taken from this package's published documentation.</p><div class="example-grid"><article class="example-card"><h3>Example 1</h3><pre data-language="bash"><code>bun add @wrnexus/tracking</code></pre></article><article class="example-card"><h3>Example 2</h3><pre data-language="ts"><code>&#123; method: ctx.req.method, path: ctx.url.pathname, requestId: ctx.locals.requestId &#125;</code></pre></article><article class="example-card"><h3>Example 3</h3><pre data-language="ts"><code>interface ErrorEvent &#123;
error: Error;
context: Record&lt;string, unknown&gt;; // request info, user id, tags…
timestamp: number; // epoch ms
&#125;
interface ErrorSink &#123;
name?: string;
capture(event: ErrorEvent): void | Promise&lt;void&gt;;
&#125;</code></pre></article><article class="example-card"><h3>Example 4</h3><pre data-language="ts"><code>import &#123; createTracker, consoleSink &#125; from &quot;@wrnexus/tracking&quot;;
const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
try &#123;
await doWork();
&#125; catch (err) &#123;
await tracker.capture(err, &#123; userId: 42, op: &quot;doWork&quot; &#125;);
throw err;
&#125;</code></pre></article></div></section></article>
<aside class="on-this-page"><h2>On this page</h2><nav><a class="toc-level-2" href="#guide">Guide</a><a class="toc-level-3" href="#overview">Overview</a><a class="toc-level-3" href="#installation">Installation</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#createtracker-options-tracker">createTracker(options?): Tracker</a><a class="toc-level-4" href="#consolesink-errorsink">consoleSink: ErrorSink</a><a class="toc-level-4" href="#types">Types</a><a class="toc-level-3" href="#usage">Usage</a><a class="toc-level-3" href="#requirements-notes">Requirements / Notes</a><a class="toc-level-2" href="#api">Complete API</a><a class="toc-level-2" href="#examples">Examples</a></nav></aside>
</main>
<footer>WRNexusJS 0.2.14 · SSR-first · Bun-native · Documentation generated from published package APIs.</footer>
</div>
}
}