Allow overriding content-type of plaintext response factory

This commit is contained in:
2022-04-05 12:20:18 -05:00
parent 771fed8002
commit c7557cf5b6
2 changed files with 10 additions and 2 deletions

View File

@@ -13,6 +13,8 @@ export function plaintext(value: string): StringResponseFactory {
* Response factory that renders a given string as the response in plaintext.
*/
export class StringResponseFactory extends ResponseFactory {
protected targetContentType = 'text/plain'
constructor(
/** The string to write as the body. */
public readonly value: string,
@@ -22,8 +24,14 @@ export class StringResponseFactory extends ResponseFactory {
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.setHeader('Content-Type', 'text/plain')
request.response.setHeader('Content-Type', this.targetContentType)
request.response.body = this.value
return request
}
/** Override the content type of the string. */
public contentType(type: string): this {
this.targetContentType = type
return this
}
}