lib/src/http/response/HTMLResponseFactory.ts

29 lines
762 B
TypeScript
Raw Normal View History

2021-06-03 03:36:25 +00:00
import {ResponseFactory} from './ResponseFactory'
import {Request} from '../lifecycle/Request'
2021-03-25 13:50:13 +00:00
/**
* Helper function that creates a new HTMLResponseFactory.
* @param value
*/
export function html(value: string): HTMLResponseFactory {
return new HTMLResponseFactory(value)
}
2021-03-25 13:50:13 +00:00
/**
* Response factory that writes a string to the response as HTML.
*/
export class HTMLResponseFactory extends ResponseFactory {
constructor(
public readonly value: string,
2021-06-03 03:36:25 +00:00
) {
super()
}
2021-06-03 03:36:25 +00:00
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.setHeader('Content-Type', 'text/html; charset=utf-8')
request.response.body = this.value
return request
}
}