TypeDoc all the thngs

This commit is contained in:
2021-03-25 08:50:13 -05:00
parent 7cb0546b01
commit fad1184afe
52 changed files with 976 additions and 3 deletions

View File

@@ -2,8 +2,12 @@ import {ViewEngine} from "./ViewEngine"
import {Injectable} from "@extollo/di"
import * as pug from "pug"
/**
* Implementation of the ViewEngine class that renders Pug/Jade templates.
*/
@Injectable()
export class PugViewEngine extends ViewEngine {
/** A cache of compiled templates. */
protected compileCache: {[key: string]: ((locals?: pug.LocalsObject) => string)} = {}
public renderString(templateString: string, locals: { [p: string]: any }): string | Promise<string> {
@@ -22,6 +26,10 @@ export class PugViewEngine extends ViewEngine {
return compiled(locals)
}
/**
* Get the object of options passed to Pug's compile methods.
* @protected
*/
protected getOptions() {
return {
basedir: this.path.toLocal,

View File

@@ -3,6 +3,9 @@ import {Config} from "../service/Config"
import {Container} from "@extollo/di"
import {UniversalPath} from "@extollo/util"
/**
* Abstract base class for rendering views via different view engines.
*/
export abstract class ViewEngine extends AppClass {
protected readonly config: Config
protected readonly debug: boolean
@@ -14,10 +17,24 @@ export abstract class ViewEngine extends AppClass {
|| this.config.get('server.debug', false))
}
/**
* Get the UniversalPath to the base directory where views are loaded from.
*/
public get path(): UniversalPath {
return this.app().appPath(...['resources', 'views']) // FIXME allow configuring
}
/**
* Given a template string and a set of variables for the view, render the string to HTML and return it.
* @param templateString
* @param locals
*/
public abstract renderString(templateString: string, locals: {[key: string]: any}): string | Promise<string>
/**
* Given the canonical name of a template file, render the file using the provided variables.
* @param templateName
* @param locals
*/
public abstract renderByName(templateName: string, locals: {[key: string]: any}): string | Promise<string>
}

View File

@@ -13,6 +13,10 @@ import {Config} from "../service/Config";
import {ViewEngine} from "./ViewEngine";
import {PugViewEngine} from "./PugViewEngine";
/**
* Dependency factory whose token matches the abstract ViewEngine class, but produces
* a particular ViewEngine implementation based on the configuration.
*/
export class ViewEngineFactory extends AbstractFactory {
protected readonly logging: Logging
protected readonly config: Config
@@ -50,6 +54,10 @@ export class ViewEngineFactory extends AbstractFactory {
return meta
}
/**
* Using the config, get the implementation of the ViewEngine that should be used in the application.
* @protected
*/
protected getViewEngineClass() {
const ViewEngineClass = this.config.get('server.view_engine.driver', PugViewEngine)