Add view engine unit - handlebars - and response helpers

This commit is contained in:
garrettmills
2020-08-07 09:51:25 -05:00
parent 3e4f8f00f2
commit b5bde7d077
14 changed files with 165 additions and 2 deletions

View File

@@ -0,0 +1,18 @@
import ResponseFactory from './ResponseFactory.ts'
import ViewEngine from '../../unit/ViewEngine.ts'
import {Request} from '../Request.ts'
export default class PartialViewResponseFactory extends ResponseFactory {
constructor(
public readonly view: string,
public readonly context?: any,
) {
super()
}
public async write(request: Request) {
const views: ViewEngine = this.make(ViewEngine)
request.response.body = await views.partial(this.view, this.context)
return request
}
}

View File

@@ -0,0 +1,19 @@
import ResponseFactory from './ResponseFactory.ts'
import ViewEngine from '../../unit/ViewEngine.ts'
import {Request} from '../Request.ts'
export default class ViewResponseFactory extends ResponseFactory {
constructor(
public readonly view: string,
public readonly context?: any,
public readonly layout?: string,
) {
super()
}
public async write(request: Request) {
const views: ViewEngine = this.make(ViewEngine)
request.response.body = await views.render(this.view, this.context, this.layout)
return request
}
}

View File

@@ -8,6 +8,8 @@ import TemporaryRedirectResponseFactory from './TemporaryRedirectResponseFactory
import {HTTPStatus} from '../../const/http.ts'
import HTTPErrorResponseFactory from './HTTPErrorResponseFactory.ts'
import HTTPError from '../../error/HTTPError.ts'
import ViewResponseFactory from './ViewResponseFactory.ts'
import PartialViewResponseFactory from './PartialViewResponseFactory.ts'
export function json(value: any): JSONResponseFactory {
return make(JSONResponseFactory, value)
@@ -33,3 +35,11 @@ export function redirect(destination: string): TemporaryRedirectResponseFactory
export function http(status: HTTPStatus, message?: string): HTTPErrorResponseFactory {
return make(HTTPErrorResponseFactory, new HTTPError(status, message))
}
export function view(view: string, context?: any, layout?: string): ViewResponseFactory {
return make(ViewResponseFactory, view, context, layout)
}
export function partial(view: string, context?: any): PartialViewResponseFactory {
return make(PartialViewResponseFactory, view, context)
}