import {ResponseFactory} from './ResponseFactory' import {Rehydratable} from '../../util' import {Request} from '../lifecycle/Request' /** * Helper function that creates a DehydratedStateResponseFactory. * @param value */ export function dehydrate(value: Rehydratable): DehydratedStateResponseFactory { return new DehydratedStateResponseFactory(value) } /** * Response factor that sends a Rehydratable class' data as JSON. */ export class DehydratedStateResponseFactory extends ResponseFactory { constructor( public readonly rehydratable: Rehydratable, ) { super() } public async write(request: Request): Promise { request = await super.write(request) request.response.body = JSON.stringify(this.rehydratable.dehydrate()) request.response.setHeader('Content-Type', 'application/json') return request } }