23 lines
843 B
TypeScript
23 lines
843 B
TypeScript
|
import {Container} from "@extollo/di";
|
||
|
import {ResponseFactory} from "./ResponseFactory";
|
||
|
import {Request} from "../lifecycle/Request";
|
||
|
import {ViewEngine} from "../../views/ViewEngine";
|
||
|
|
||
|
export function view(name: string, data?: {[key: string]: any}): ViewResponseFactory {
|
||
|
return new ViewResponseFactory(name, data)
|
||
|
}
|
||
|
|
||
|
export class ViewResponseFactory extends ResponseFactory {
|
||
|
constructor(
|
||
|
public readonly viewName: string,
|
||
|
public readonly data?: {[key: string]: any}
|
||
|
) { super() }
|
||
|
|
||
|
public async write(request: Request) {
|
||
|
const viewEngine = <ViewEngine> Container.getContainer().make(ViewEngine)
|
||
|
request.response.body = await viewEngine.renderByName(this.viewName, this.data || {})
|
||
|
request.response.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||
|
return request
|
||
|
}
|
||
|
}
|