@samveys/view

The ViewEngine contract, engine registry and ctx.view

Version
1.0.3Nov 5, 2026
Install
pnpm add @samveys/view

This list was read from version 1.0.2; run pnpm sync to refresh it.

Import
import { createViewContext, createViewRegistry, view } from '@samveys/view'

Function (3)

createViewContext Function

The per-request `ctx.view`. Shared data is merged under the data given to `render` (render wins).

function createViewContext(registry: ViewRegistry, http: HttpContext): ViewContext;

Exported from packages/view/src/registry.ts

createViewRegistry Function

Creates an engine registry. The first engine registered becomes the active one unless `use()` picks another.

function createViewRegistry(initial?: string): ViewRegistry;

Exported from packages/view/src/registry.ts

view Function

The `@samveys/view` plugin. Binds `'view.registry'` and adds the lazy `ctx.view`. Engine plugins (`template()`, `vue()`) list it in `requires.plugins` and register themselves with `container.make('view.registry').register(name, engine)`.

function view(options?: ViewOptions): import("@samveys/core").Plugin<ViewOptions>;

Exported from packages/view/src/plugin.ts

Interface (7)

InertiaRoot Interface

What the `@inertia` directive of a template reads from the `$inertia` variable. `@samveys/inertia` passes it when it renders the root view.

interface InertiaRoot {
    /** The page object, serialized into `data-page`. */
    readonly page: unknown;
    /** Id of the root element. Default `app`. */
    readonly id?: string;
    /** Server-rendered markup placed inside the root element. */
    readonly body?: string;
    /** Server-rendered `<head>` markup, printed by `@inertiaHead`. */
    readonly head?: string;
}

Exported from packages/view/src/contracts.ts

ViewContext Interface

Per-request view API exposed as `ctx.view`.

interface ViewContext {
    render(name: string, data?: Record<string, unknown>, init?: ResponseInit): Promise<Response>;
    share(key: string, value: unknown): void;
    readonly shared: Readonly<Record<string, unknown>>;
}

Exported from packages/view/src/contracts.ts

ViewEngine Interface

A pluggable view engine.

interface ViewEngine {
    render(name: string, data: Record<string, unknown>, ctx?: ViewRenderContext): Promise<string>;
}

Exported from packages/view/src/contracts.ts

ViewHelpers Interface

Services other packages contribute and engines consume, so that `@samveys/template` does not depend on `@samveys/vite` or `@samveys/security`. Each plugin sets its own entry when it boots.

interface ViewHelpers {
    /** Tags for Vite entries (`@vite(...)`): set by `@samveys/vite`. */
    vite?: (entries: readonly string[]) => string | Promise<string>;

Exported from packages/view/src/contracts.ts

ViewOptions Interface

No documentation comment.

interface ViewOptions {
    /** Name of the engine to use; default is the first engine registered. */
    readonly engine?: string;
}

Exported from packages/view/src/plugin.ts

ViewRegistry Interface

Registry holding the available engines and which one is active.

interface ViewRegistry {
    register(name: string, engine: ViewEngine): void;
    use(name: string): void;
    has(name: string): boolean;
    /** The named engine, or the active one. Throws `SVS_E109` when none is registered. */
    get(name?: string): ViewEngine;
    readonly active: string | undefined;
    readonly helpers: ViewHelpers;
}

Exported from packages/view/src/contracts.ts

ViewRenderContext Interface

Per-request context handed to an engine so it can read request state (CSRF token, cookies).

interface ViewRenderContext {
    readonly http?: HttpContext;
}

Exported from packages/view/src/contracts.ts