2021-06-03 03:36:25 +00:00
|
|
|
import {ResponseFactory} from './ResponseFactory'
|
|
|
|
import {Request} from '../lifecycle/Request'
|
2021-03-08 16:07:10 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Helper function that creates a new StringResponseFactory for the given string value.
|
|
|
|
* @param value
|
|
|
|
*/
|
2021-03-08 16:07:10 +00:00
|
|
|
export function plaintext(value: string): StringResponseFactory {
|
|
|
|
return new StringResponseFactory(value)
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Response factory that renders a given string as the response in plaintext.
|
|
|
|
*/
|
2021-03-08 16:07:10 +00:00
|
|
|
export class StringResponseFactory extends ResponseFactory {
|
|
|
|
constructor(
|
2021-03-25 13:50:13 +00:00
|
|
|
/** The string to write as the body. */
|
2021-03-08 16:07:10 +00:00
|
|
|
public readonly value: string,
|
2021-06-03 03:36:25 +00:00
|
|
|
) {
|
|
|
|
super()
|
|
|
|
}
|
2021-03-08 16:07:10 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public async write(request: Request): Promise<Request> {
|
2021-03-08 16:07:10 +00:00
|
|
|
request = await super.write(request)
|
|
|
|
request.response.setHeader('Content-Type', 'text/plain')
|
|
|
|
request.response.body = this.value
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
}
|