2021-06-03 03:36:25 +00:00
|
|
|
import {ResponseFactory} from './ResponseFactory'
|
|
|
|
import {Rehydratable} from '../../util'
|
|
|
|
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 DehydratedStateResponseFactory.
|
|
|
|
* @param value
|
|
|
|
*/
|
2021-03-08 16:07:10 +00:00
|
|
|
export function dehydrate(value: Rehydratable): DehydratedStateResponseFactory {
|
|
|
|
return new DehydratedStateResponseFactory(value)
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Response factor that sends a Rehydratable class' data as JSON.
|
|
|
|
*/
|
2021-03-08 16:07:10 +00:00
|
|
|
export class DehydratedStateResponseFactory extends ResponseFactory {
|
|
|
|
constructor(
|
2021-06-03 03:36:25 +00:00
|
|
|
public readonly rehydratable: Rehydratable,
|
|
|
|
) {
|
|
|
|
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.body = JSON.stringify(this.rehydratable.dehydrate())
|
|
|
|
request.response.setHeader('Content-Type', 'application/json')
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
}
|