Files
lib/src/http/response/DehydratedStateResponseFactory.ts

28 lines
866 B
TypeScript
Raw Normal View History

import {ResponseFactory} from "./ResponseFactory"
import {Rehydratable} from "@extollo/util"
import {Request} from "../lifecycle/Request";
2021-03-25 08:50:13 -05:00
/**
* Helper function that creates a DehydratedStateResponseFactory.
* @param value
*/
export function dehydrate(value: Rehydratable): DehydratedStateResponseFactory {
return new DehydratedStateResponseFactory(value)
}
2021-03-25 08:50:13 -05:00
/**
* 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) {
request = await super.write(request)
request.response.body = JSON.stringify(this.rehydratable.dehydrate())
request.response.setHeader('Content-Type', 'application/json')
return request
}
}