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.

29 lines
796 B

import {ResponseFactory} from './ResponseFactory'
import {Request} from '../lifecycle/Request'
* Helper function to create a new JSONResponseFactory of the given value.
* @param value
export function json(value: unknown): JSONResponseFactory {
return new JSONResponseFactory(value)
* Response factory that writes the given object as JSON to the response.
export class JSONResponseFactory extends ResponseFactory {
public readonly value: unknown,
) {
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.setHeader('Content-Type', 'application/json')
request.response.body = JSON.stringify(this.value)
return request