import Router from "@koa/router"; import { Context } from "koa"; import { FlatTemplatable, tempstream } from "tempstream"; import type { FormDataValue } from "../forms/form-types.js"; import { Fields, MountableWithFields, PageErrorMessage, } from "./mountable-with-fields.js"; import { AllQueryParams, PagePropsParser } from "./props-parser.js"; export abstract class Page< F extends Fields = Record, > extends MountableWithFields { propsParser: PagePropsParser> = new AllQueryParams(); getControls = () => []; mount(router: Router, path: string): void { router.get(path, async (ctx) => { ctx.body = await this.render(ctx); }); } async canAccess(_: Context) { return { canAccess: true, message: "" }; } async renderError( _: Context, error: PageErrorMessage ): Promise { return tempstream /* HTML */ `
${error.message}
`; } public abstract render(ctx: Context): Promise; async extractRawValues( ctx: Context ): Promise> { return this.propsParser.decode(ctx) || {}; } }