From e1a2c3253b3c4be21aa7485b1cb1429076aa7e6b Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 10 Jul 2026 14:31:12 -0500 Subject: [PATCH] feat(views): @Html decorator and metadata Signed-off-by: Diego Resendez --- src/index.ts | 17 ++++++++++++++++- src/metadata.ts | 35 +++++++++++++++++++++++++++++++++++ test/metadata.test.ts | 21 +++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index bc8a7ac..de7e9c9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/metadata.ts b/src/metadata.ts index 2bcab34..530dcff 100644 --- a/src/metadata.ts +++ b/src/metadata.ts @@ -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; 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); diff --git a/test/metadata.test.ts b/test/metadata.test.ts index 300d811..857d40f 100644 --- a/test/metadata.test.ts +++ b/test/metadata.test.ts @@ -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(); + }); +});