You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/http/response/StringResponseFactory.ts

38 lines
1.0 KiB

import {ResponseFactory} from './ResponseFactory'
import {Request} from '../lifecycle/Request'
/**
* Helper function that creates a new StringResponseFactory for the given string value.
* @param value
*/
export function plaintext(value: string): StringResponseFactory {
return new StringResponseFactory(value)
}
/**
* 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,
) {
super()
}
public async write(request: Request): Promise<Request> {
request = await super.write(request)
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
}
}