diff --git a/test/views-html.test.ts b/test/views-html.test.ts index 72a165f..7eafeb1 100644 --- a/test/views-html.test.ts +++ b/test/views-html.test.ts @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'; import { writeFileSync, mkdirSync } from 'node:fs'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; -import { createApp, Route, Get, Post, Sse, Transformer, Html, Module } from '../src/index'; +import { createApp, Route, Get, Sse, Transformer, Html, Module } from '../src/index'; const dir = join(tmpdir(), 'gt-views-test'); mkdirSync(dir, { recursive: true }); diff --git a/website/src/content/docs/guides/html.md b/website/src/content/docs/guides/html.md index 823f619..2914c37 100644 --- a/website/src/content/docs/guides/html.md +++ b/website/src/content/docs/guides/html.md @@ -62,6 +62,12 @@ class UserController { Same filesystem requirement as file mode: Node/Deno/Bun only. +:::caution[Return a string or data, never an `AsyncIterable`] +An `@Html` handler must return a string (bare mode) or a data object (template mode). Don't +return an `AsyncIterable` — the pipeline detects async-iterable returns and streams them raw, +bypassing the HTML transformer entirely, so your markup never gets wrapped or rendered. +::: + ## The `views` base directory `@Html('file.html')` paths resolve against `createApp({ views })` — a relative path joins `views`, an absolute path is used as-is (handy for a template stored outside the project, e.g. on an NFS mount). `views` defaults to `process.cwd()` when unset. diff --git a/website/src/content/docs/reference/createapp.md b/website/src/content/docs/reference/createapp.md index 2bb4e26..9a78f67 100644 --- a/website/src/content/docs/reference/createapp.md +++ b/website/src/content/docs/reference/createapp.md @@ -23,6 +23,9 @@ const app = createApp(options); | `cors` | `CorsOptions` | — | CORS handling | | `bodyDuplicates` | `'array' \| 'last'` | `'last'` | policy for repeated form fields | | `onError` | `ErrorRenderer` | — | render errors your way ([error handling](/guides/errors/)); returns a response or `undefined` to fall back to JSON | +| `views` | `string` | `process.cwd()` | base dir `@Html('file.html')` paths resolve against — relative paths join it, absolute paths are used as-is ([HTML & views](/guides/html/)) | +| `viewEngine` | `(source: string, data: unknown) => string` | built-in `render` | swap in your own template engine for `@Html(..., { template: true })`; template mode only ([HTML & views](/guides/html/)) | +| `static` | `boolean \| string` | — | serve a directory as a `GET`/`HEAD` fallback (after declared routes, before `404`); `true` → `./public`, a string → that dir; path-traversal-safe; needs a filesystem — Node/Deno/Bun only ([HTML & views](/guides/html/)) | | `mesh` | `MeshConfig` | — | distributed DI — **requires `experimental: true`** ([mesh](/guides/mesh/)) | | `experimental` | `boolean` | `false` | opt in to alpha features (currently gates `mesh`) | diff --git a/website/src/content/docs/reference/decorators.md b/website/src/content/docs/reference/decorators.md index d0d60f4..c3233a5 100644 --- a/website/src/content/docs/reference/decorators.md +++ b/website/src/content/docs/reference/decorators.md @@ -46,6 +46,18 @@ A request-scope transform that merges keys into the context. Same options as `@P Method decorator. Turns a handler's return value into `{ status?, headers?, body }`. `JsonTransformer` is the default. +### `@Html` / `@Html(path, opts?)` + +Method decorator. Marks a buffered `GET`/`POST` handler as serving HTML instead of going through the JSON transformer. Three modes ([details](/guides/html/)): + +| Form | Behavior | +|---|---| +| `@Html` (bare) | sends the handler's returned string as `text/html` | +| `@Html('file.html')` | serves that file verbatim; the handler's return value is ignored | +| `@Html('file.html', { template: true })` | renders the file with the handler's returned data, via the built-in `render` engine or `viewEngine` ([createApp](/reference/createapp/)) | + +Only buffered `GET`/`POST` routes are supported, and `@Html` can't be combined with `@Transformer` on the same handler — both are validated at boot, not per-request. + ## Routes Method decorators on a controller. All accept `(path, opts?)` where `opts = { export?: boolean, duplicates?: 'array' | 'last', maxBodyBytes?: number, maxParts?: number }`. `maxBodyBytes` / `maxParts` override the server-wide [`limits`](/reference/createapp/) for this route only.