feat(views): @Html decorator and metadata
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+72
-1
No files matched your search
+16
-1
@@ -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';
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user