feat(views): built-in template engine + content-type map
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+77
No files matched your search
@@ -18,6 +18,9 @@ export type { GraphView } from './graph-viz';
|
||||
export { buildOpenApi } from './openapi';
|
||||
export type { OpenApiDoc, OpenApiInfo, OpenApiRoute } from './openapi';
|
||||
|
||||
// Views
|
||||
export { render } from './views';
|
||||
|
||||
// Routing & decorators
|
||||
export { Provider, Step, Route, Get, Module, Transformer, Sse, Ws, Stream, Post, Put, Patch, Delete } from './metadata';
|
||||
export type { Transport, HttpMethod } from './metadata';
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// src/views.ts — HTML views: built-in templating, content-type map, and boot-time builders.
|
||||
// fs/path are lazy-required inside functions so the barrel stays edge-loadable (workerd has no filesystem).
|
||||
|
||||
/** Escape the five HTML-significant characters so interpolated data can't inject markup. */
|
||||
export function escapeHtml(s: string): string {
|
||||
const map: Record<string, string> = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
||||
return s.replace(/[&<>"']/g, (c) => map[c]);
|
||||
}
|
||||
|
||||
function stringify(v: unknown): string {
|
||||
return v === null || v === undefined ? '' : String(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal built-in template engine: `{{ key }}` interpolates HTML-escaped, `{{{ key }}}` raw,
|
||||
* `{{ a.b }}` walks nested keys, a missing key renders empty. No loops/conditionals/partials.
|
||||
*/
|
||||
export function render(source: string, data: unknown): string {
|
||||
const lookup = (key: string): unknown =>
|
||||
key.split('.').reduce<any>((obj, k) => (obj === null || obj === undefined ? undefined : obj[k]), data);
|
||||
return source
|
||||
.replace(/\{\{\{\s*([\w.]+)\s*\}\}\}/g, (_, key) => stringify(lookup(key)))
|
||||
.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (_, key) => escapeHtml(stringify(lookup(key))));
|
||||
}
|
||||
|
||||
const CONTENT_TYPES: Record<string, string> = {
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.js': 'text/javascript; charset=utf-8',
|
||||
'.mjs': 'text/javascript; charset=utf-8',
|
||||
'.json': 'application/json; charset=utf-8',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.jpeg': 'image/jpeg',
|
||||
'.gif': 'image/gif',
|
||||
'.webp': 'image/webp',
|
||||
'.ico': 'image/x-icon',
|
||||
'.woff2': 'font/woff2',
|
||||
'.txt': 'text/plain; charset=utf-8',
|
||||
};
|
||||
|
||||
/** Content-type for a file extension (case-insensitive, leading dot); unknown → `application/octet-stream`. */
|
||||
export function contentTypeFor(ext: string): string {
|
||||
return CONTENT_TYPES[ext.toLowerCase()] ?? 'application/octet-stream';
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { render, escapeHtml, contentTypeFor } from '../src/views';
|
||||
|
||||
describe('escapeHtml', () => {
|
||||
it('escapes the five HTML-significant characters', () => {
|
||||
expect(escapeHtml(`<a href="x">&'`)).toBe('<a href="x">&'');
|
||||
});
|
||||
});
|
||||
|
||||
describe('render', () => {
|
||||
it('interpolates and HTML-escapes {{ x }} by default', () => {
|
||||
expect(render('<h1>{{ name }}</h1>', { name: '<b>hi</b>' })).toBe('<h1><b>hi</b></h1>');
|
||||
});
|
||||
it('leaves {{{ x }}} raw (unescaped)', () => {
|
||||
expect(render('{{{ html }}}', { html: '<b>hi</b>' })).toBe('<b>hi</b>');
|
||||
});
|
||||
it('resolves nested keys and renders missing keys as empty', () => {
|
||||
expect(render('{{ user.name }}|{{ nope }}', { user: { name: 'Diego' } })).toBe('Diego|');
|
||||
});
|
||||
});
|
||||
|
||||
describe('contentTypeFor', () => {
|
||||
it('maps known extensions and defaults unknown to octet-stream', () => {
|
||||
expect(contentTypeFor('.css')).toBe('text/css; charset=utf-8');
|
||||
expect(contentTypeFor('.PNG')).toBe('image/png');
|
||||
expect(contentTypeFor('.xyz')).toBe('application/octet-stream');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user