docs: publish package usage examples for 0.2.24

This commit is contained in:
2026-07-13 20:58:28 +05:30
parent b07ef5058a
commit 379553bdf7
79 changed files with 1542 additions and 894 deletions
+29 -40
View File
@@ -10,12 +10,12 @@ page wrnexusrouter {
<header class="topbar">
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
<nav aria-label="Primary"><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
<div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.2.23</a><button data-wire-theme-toggle class="theme-button" aria-label="Toggle color theme" title="Toggle color theme">◐</button></div>
<div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.2.24</a><button data-wire-theme-toggle class="theme-button" aria-label="Toggle color theme" title="Toggle color theme">◐</button></div>
</header>
<div class="mobile-doc-nav"><details><summary>Browse documentation</summary><nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/language">Language</a><a href="/architecture">Architecture</a><a href="/tutorial">Tutorial</a><a href="/guides/project-structure">Guides</a><a href="/examples">Examples</a><a href="/search">Search</a></nav></details></div>
<main class="page package-page">
<aside class="sidebar"><a href="/packages">← All packages</a><span class="category">Core</span><h2>@wrnexus/router</h2><p>Filesystem discovery, route matching, and typed route generation.</p><span class="status status-beta">Private preview · 0.2.23</span><nav><a href="#access">Access</a><a href="#guide">Guide</a><a href="#api">Complete API</a></nav></aside>
<article id="main" class="documentation"><section class="doc-intro"><span class="eyebrow">Core · Preview</span><h1>@wrnexus/router</h1><p>Filesystem discovery, route matching, and typed route generation.</p><section id="access" class="access-callout"><h2>Private registry access required</h2><p>This package is not available from the public npm registry. After WorkRoot approves access and supplies private registry instructions, install the release-aligned package:</p><pre><code>bun add @wrnexus/router@0.2.23</code><button type="button" class="copy-button" aria-label="Copy installation command">Copy</button></pre><p><a href="/access">Request preview access</a>. Never put registry tokens in source control.</p></section></section><section id="guide" class="prose"><blockquote>File-based router that maps an <code>app/</code> directory onto route tables and matches request paths against them.</blockquote>
<aside class="sidebar"><a href="/packages">← All packages</a><span class="category">Core</span><h2>@wrnexus/router</h2><p>Filesystem discovery, route matching, and typed route generation.</p><span class="status status-beta">Private preview · 0.2.24</span><nav><a href="#access">Access</a><a href="#guide">Guide</a><a href="#api">Complete API</a></nav></aside>
<article id="main" class="documentation"><section class="doc-intro"><span class="eyebrow">Core · Preview</span><h1>@wrnexus/router</h1><p>Filesystem discovery, route matching, and typed route generation.</p><section id="access" class="access-callout"><h2>Private registry access required</h2><p>This package is not available from the public npm registry. After WorkRoot approves access and supplies private registry instructions, install the release-aligned package:</p><pre><code>bun add @wrnexus/router@0.2.24</code><button type="button" class="copy-button" aria-label="Copy installation command">Copy</button></pre><p><a href="/access">Request preview access</a>. Never put registry tokens in source control.</p></section></section><section id="guide" class="prose"><blockquote>File-based router that maps an <code>app/</code> directory onto route tables and matches request paths against them.</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/router</code> scans an application's <code>app/</code> directory once at startup and builds route tables for pages, API endpoints, realtime channels, middleware, server-rendered <code>.wrn</code> components, layouts, and validation schemas. It also compiles URL patterns (<code>/users/[id]</code>) into RegExps and matches request paths against them. Request input is never turned into a file path, which makes the router immune to path traversal. This is a server-side package used by the WRNexusJS runtime to resolve incoming requests, plus a codegen helper for compile-time typed links.</p>
@@ -220,48 +220,37 @@ interface RouterOptions &#123;
declare function buildRouter(appDir: string, opts?: RouterOptions): Router;
export &#123; type ComponentRef, type Route, type RouteMatch, type Router, type RouterOptions, buildRouter, compileRoutePattern, generateRoutesFile, matchRoute, sortRoutes &#125;;
</code></pre></section><section id="examples" class="prose examples"><h2>Examples</h2><p>Examples are taken from this package's installed documentation and must be evaluated with its requirements and stability notes.</p><div class="example-grid"><article class="example-card"><h3>Example 1</h3><pre data-language="bash"><code>bun add @wrnexus/router</code></pre></article><article class="example-card"><h3>Example 2</h3><pre data-language="text"><code>app/pages/index.tsx -&gt; GET /
app/pages/about.tsx -&gt; GET /about
app/pages/users/[id].tsx -&gt; GET /users/:id
app/api/hello.ts -&gt; /api/hello
app/realtime/chat.ts -&gt; /realtime/chat
app/pages/*.wrn (api) -&gt; embedded /api/* routes
app/pages/*.wrn (rt) -&gt; embedded /realtime/* routes
app/middleware/*.ts -&gt; global middleware (alphabetical)
app/components/*.wrn -&gt; server-rendered components (by basename)
app/layouts/*.wrn -&gt; named page layouts
app/schemas/*.ts -&gt; validation schemas</code></pre></article><article class="example-card"><h3>Example 3</h3><pre data-language="ts"><code>function buildRouter(appDir: string, opts?: RouterOptions): Router;
</code></pre></section><section id="examples" class="prose examples"><h2>Examples</h2><p>Examples are taken from this package's installed documentation and must be evaluated with its requirements and stability notes.</p><div class="example-grid"><article class="example-card"><h3>Typical usage</h3><pre data-language="ts"><code>import &#123; buildRouter &#125; from &quot;@wrnexus/router&quot;;
interface RouterOptions &#123;
/** Extra dirs scanned for `.wrn` components (e.g. `@wrnexus/ui`), before
* `app/components`, so an app component of the same name wins. */
componentDirs?: string[];
&#125;</code></pre></article><article class="example-card"><h3>Example 4</h3><pre data-language="ts"><code>interface Router &#123;
pages: Route[];
api: Route[];
realtime: Route[];
/** Absolute paths of middleware modules, in execution order (alphabetical). */
middlewareFiles: string[];
/** Server-rendered `.wrn` components, mounted via `data-component`. */
components: ComponentRef[];
/** Named page layouts (`app/layouts/&lt;name&gt;.wrn`); a page picks one via `layout`. */
layouts: ComponentRef[];
/** Validation schemas (`app/schemas/&lt;name&gt;.ts`) shared by API + forms. */
schemas: ComponentRef[];
matchPage(pathname: string): RouteMatch | null;
matchApi(pathname: string): RouteMatch | null;
matchRealtime(pathname: string): RouteMatch | null;
const router = buildRouter(&quot;./app&quot;, &#123;
componentDirs: [&quot;./node_modules/@wrnexus/ui/components&quot;],
&#125;);
// Resolve an incoming request.
const match = router.matchPage(&quot;/users/42&quot;);
if (match) &#123;
console.log(match.route.file); // absolute path to the page module
console.log(match.params); // &#123; id: &quot;42&quot; &#125;
&#125;
interface ComponentRef &#123;
/** Validated component name (matches a `data-component` attribute). */
name: string;
/** Absolute path to the component's `.wrn` module. */
file: string;
&#125;</code></pre></article></div></section></article>
const api = router.matchApi(&quot;/api/hello&quot;);
const rt = router.matchRealtime(&quot;/realtime/chat&quot;);</code></pre></article><article class="example-card"><h3>Generating the typed-routes file (as wrnexus dev does)</h3><pre data-language="ts"><code>import &#123; generateRoutesFile &#125; from &quot;@wrnexus/router&quot;;
import &#123; writeFileSync &#125; from &quot;node:fs&quot;;
const router = buildRouter(&quot;./app&quot;);
writeFileSync(&quot;./app/routes.gen.ts&quot;, generateRoutesFile(router.pages));</code></pre></article><article class="example-card"><h3>Typical usage</h3><pre data-language="ts"><code>// Then, in app code, links are checked at compile time:
import &#123; href &#125; from &quot;./routes.gen.ts&quot;;
href(&quot;/users/[id]&quot;, &#123; id: &quot;42&quot; &#125;); // &quot;/users/42&quot;
href(&quot;/about&quot;); // &quot;/about&quot;
href(&quot;/nope&quot;); // type error: unknown path</code></pre></article><article class="example-card"><h3>Lower-level pattern matching, if you need it directly</h3><pre data-language="ts"><code>import &#123; compileRoutePattern, matchRoute, sortRoutes, type Route &#125; from &quot;@wrnexus/router&quot;;
const &#123; regex, paramNames &#125; = compileRoutePattern(&quot;/posts/[slug]&quot;);
const routes = sortRoutes([&#123; raw: &quot;/posts/[slug]&quot;, file: &quot;…&quot;, regex, paramNames &#125;]);
const m = matchRoute(routes, &quot;/posts/hello&quot;); // &#123; route, params: &#123; slug: &quot;hello&quot; &#125; &#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="#directory-conventions">Directory conventions</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#buildrouter-appdir-opts-router">buildRouter(appDir, opts?): Router</a><a class="toc-level-4" href="#route-matching">Route matching</a><a class="toc-level-4" href="#typed-routes-codegen">Typed-routes codegen</a><a class="toc-level-4" href="#re-exports">Re-exports</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.23 · Private Developer Preview · Bun-native · Documentation generated from installed package APIs.</footer>
<footer>WRNexusJS 0.2.24 · Private Developer Preview · Bun-native · Documentation generated from installed package APIs.</footer>
</div>
}
}