feat(views): @Html decorator and metadata

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-10 14:31:12 -05:00
1 parent da5cc936a3
commit e1a2c3253b
3 files changed
+72 -1

No files matched your search

+16 -1
View File
@@ -22,7 +22,22 @@ export type { OpenApiDoc, OpenApiInfo, OpenApiRoute } from './openapi';
export { render } from './views';
// Routing & decorators
export { Provider, Step, Route, Get, Module, Transformer, Sse, Ws, Stream, Post, Put, Patch, Delete } from './metadata';
export {
Provider,
Step,
Route,
Get,
Module,
Transformer,
Sse,
Ws,
Stream,
Post,
Put,
Patch,
Delete,
Html,
} from './metadata';
export type { Transport, HttpMethod } from './metadata';
export type { Plugin } from './plugin';
+35
View File
@@ -5,6 +5,11 @@ export type Ctor = new (...args: any[]) => any;
/** Serializes a handler's return value into an HTTP response envelope. */
export type TransformerFn = (value: unknown) => { status?: number; headers?: Record<string, string>; body: string };
/** Metadata attached by `@Html`: optional file path and whether it is a template. */
export interface HtmlMeta {
path?: string;
template?: boolean;
}
/** Metadata attached by `@Provider`: what it provides, what it needs, and its visibility. */
export interface ProviderMeta {
provides: string;
@@ -49,6 +54,7 @@ const K = {
routes: Symbol('gt:routes'),
module: Symbol('gt:module'),
transformer: Symbol('gt:transformer'),
html: Symbol('gt:html'),
};
/** Marks a class as a provider: a lazily-built dependency addressable by its `provides` key. */
@@ -147,6 +153,35 @@ export function Module(opts: ModuleOptions): ClassDecorator {
};
}
/**
* Marks a buffered GET/POST handler as serving HTML. Bare `@Html` sends the handler's string return as
* `text/html`. `@Html('file.html')` serves that file (return ignored). `@Html('file.html', { template: true })`
* renders the file with the handler's returned data. Placement is validated at boot (buffered GET/POST only).
*/
export function Html(path: string, opts?: { template?: boolean }): MethodDecorator;
export function Html(target: object, propertyKey: string | symbol): void;
export function Html(a: unknown, b?: unknown): MethodDecorator | void {
if (typeof a !== 'string') {
// Bare usage: TS calls (prototype, propertyKey, descriptor?).
Reflect.defineMetadata(K.html, {}, (a as { constructor: Ctor }).constructor, String(b));
return;
}
const path = a;
const opts = b as { template?: boolean } | undefined;
return (target, propertyKey) => {
const meta: HtmlMeta = { path, template: opts?.template };
Reflect.defineMetadata(K.html, meta, (target as { constructor: Ctor }).constructor, String(propertyKey));
};
}
/** Reads the `@Html` metadata registered for a handler method, if any. */
export function getHtmlMeta(cls: Ctor, methodName: string): HtmlMeta | undefined {
return Reflect.getMetadata(K.html, cls, methodName);
}
/** Reads the `@Provider` metadata off a class, if any. */
export function getProviderMeta(cls: Ctor): ProviderMeta | undefined {
return Reflect.getMetadata(K.provider, cls);
+21
View File
@@ -74,3 +74,24 @@ describe('mesh export flags', () => {
expect(byName.priv.export).toBe(false);
});
});
import { Html, getHtmlMeta } from '../src/metadata';
describe('@Html', () => {
it('records empty meta for bare @Html', () => {
class A { @Html m() { return ''; } }
expect(getHtmlMeta(A, 'm')).toEqual({});
});
it('records path and template for called @Html', () => {
class B {
@Html('views/x.html') a() {}
@Html('views/y.html', { template: true }) c() { return {}; }
}
expect(getHtmlMeta(B, 'a')).toEqual({ path: 'views/x.html', template: undefined });
expect(getHtmlMeta(B, 'c')).toEqual({ path: 'views/y.html', template: true });
});
it('returns undefined when @Html is absent', () => {
class C { m() {} }
expect(getHtmlMeta(C, 'm')).toBeUndefined();
});
});