import { Request } from 'express'; import HeroSection from './sections/hero'; import { Template } from './template'; import { LocaleLanguage } from './locale'; import { Page } from './page'; const SECTION_TYPES = { 'hero': HeroSection } export type Section

= { properties:P; validate:(properties:P) => P; }; export type SectionType = keyof typeof SECTION_TYPES; export type SectionTypeFor = ( typeof SECTION_TYPES[T] ); export type SectionProperties = ( SectionTypeFor['properties'] ); export type SectionData = { type:T; properties:SectionProperties; } export type SectionRenderer = (p:{ properties:SectionProperties; template:Template; language:LocaleLanguage; request:Request; page:Page; }) => Promise; export const sectionRender = async (p:{ request:Request, section:SectionData; template:Template; language:LocaleLanguage; page:Page; }):Promise => { if(!p.template.sections[p.section.type]) { console.warn(`No section renderer found for section type "${p.section.type}" in template "${p.template.name}".`); return '';c } const renderer = p.template.sections[p.section.type] as SectionRenderer; const properties = p.section.properties; return await renderer({ ...p, properties: p.section.properties }); }