Add view engine unit - handlebars - and response helpers
This commit is contained in:
18
lib/src/http/response/PartialViewResponseFactory.ts
Normal file
18
lib/src/http/response/PartialViewResponseFactory.ts
Normal 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
|
||||
}
|
||||
}
|
||||
19
lib/src/http/response/ViewResponseFactory.ts
Normal file
19
lib/src/http/response/ViewResponseFactory.ts
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user