Support deno 1.3.x/0.67.0; replace handlebars with view_engine library

This commit is contained in:
2020-09-04 09:40:16 -05:00
parent 0f182b592b
commit ff34578d07
23 changed files with 287 additions and 141 deletions

View File

@@ -1,5 +1,5 @@
import { Injectable } from '../../../di/src/decorator/Injection.ts'
import { getCookies, setCookie, delCookie, ServerRequest } from '../external/http.ts'
import { getCookies, setCookie, deleteCookie, ServerRequest } from '../external/http.ts'
import { InMemCache } from '../support/InMemCache.ts'
import { HTTPRequest } from './type/HTTPRequest.ts'
@@ -117,6 +117,6 @@ export class CookieJar {
*/
public async delete(key: string): Promise<void> {
await this._cache.drop(key)
delCookie(this.request.response, key)
deleteCookie(this.request.response, key)
}
}

View File

@@ -14,7 +14,7 @@ export default class HTMLResponseFactory extends ResponseFactory {
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.headers.set('Content-Type', 'text/html')
request.response.headers.set('Content-Type', 'text/html; charset=utf-8')
request.response.body = this.value
return request
}

View File

@@ -1,29 +0,0 @@
import ResponseFactory from './ResponseFactory.ts'
import ViewEngine from '../../unit/ViewEngine.ts'
import {Request} from '../Request.ts'
/**
* Response factory that renders a partial view as HTML.
* @return ResponseFactory
*/
export default class PartialViewResponseFactory extends ResponseFactory {
constructor(
/**
* The view name.
* @type string
*/
public readonly view: string,
/**
* Optionally, the response context.
*/
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

@@ -13,22 +13,19 @@ export default class ViewResponseFactory extends ResponseFactory {
* @type string
*/
public readonly view: string,
/**
* Optionally, the view context.
* Optionally, the view data.
*/
public readonly context?: any,
/**
* Optionally, the layout name.
* @type string
*/
public readonly layout?: string,
public readonly data?: any,
) {
super()
}
public async write(request: Request) {
const views: ViewEngine = this.make(ViewEngine)
request.response.body = await views.render(this.view, this.context, this.layout)
request.response.body = await views.template(this.view, this.data)
request.response.headers.set('Content-Type', 'text/html; charset=utf-8')
return request
}
}

View File

@@ -9,7 +9,6 @@ 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'
/**
* Get a new JSON response factory that writes the given object as JSON.
@@ -69,22 +68,11 @@ export function http(status: HTTPStatus, message?: string): HTTPErrorResponseFac
}
/**
* Get a new view response factory for the given view name, passing along context and layout.
* Get a new view response factory for the given view name, passing along any view data.
* @param {string} view
* @param [context]
* @param {string} [layout]
* @param [data]
* @return ViewResponseFactory
*/
export function view(view: string, context?: any, layout?: string): ViewResponseFactory {
return make(ViewResponseFactory, view, context, layout)
}
/**
* Get a new partial view response factory for the given view name, passing along context.
* @param {string} view
* @param [context]
* @return PartialViewResponseFactory
*/
export function partial(view: string, context?: any): PartialViewResponseFactory {
return make(PartialViewResponseFactory, view, context)
export function view(view: string, data?: any): ViewResponseFactory {
return make(ViewResponseFactory, view, data)
}