Improve response factories and add helper functions

This commit is contained in:
garrettmills 2020-07-29 22:55:17 -05:00
parent 0d4881de1f
commit 0764085ef9
No known key found for this signature in database
GPG Key ID: 6ACD58D6ADACFC6E
9 changed files with 102 additions and 12 deletions

View File

@ -1,13 +1,18 @@
import Controller from '../../../lib/src/http/Controller.ts'
import {Request} from '../../../lib/src/http/Request.ts'
import {error, html, redirect} from '../../../lib/src/http/response/helpers.ts'
export default class TestController extends Controller {
get_home(req: any) {
req.response.body = 'Hello!'
get_home(request: Request) {
return html('<h1>Hi!</h1>')
}
get_user_home(req: any) {
throw new Error('Hello, world!')
}
create_user_home(req: any) {}
get_user_home(request: Request) {
return redirect('/home')
}
create_user_home(request: Request) {
return error('Not implemented!')
}
}

View File

@ -10,6 +10,7 @@ export default class DehydratedStateResponseFactory extends ResponseFactory {
}
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.body = JSON.stringify(this.rehydratable.dehydrate())
return request
}

View File

@ -1,17 +1,26 @@
import ResponseFactory from "./ResponseFactory.ts";
import {Request} from "../Request.ts";
import ResponseFactory from './ResponseFactory.ts'
import {Request} from '../Request.ts'
export default class ErrorResponseFactory extends ResponseFactory {
protected target_mode: 'json' | 'html' = 'html'
constructor(
public readonly error: Error,
public readonly output: 'json' | 'html' = 'html',
public readonly status: number = 500,
status: number = 500,
output: 'json' | 'html' = 'html',
) {
super()
this.status(status)
this.mode(output)
}
public mode(output: 'json' | 'html'): ErrorResponseFactory {
this.target_mode = output
return this
}
public async write(request: Request): Promise<Request> {
request.response.status = this.status
request = await super.write(request)
if ( this.output === 'json' ) {
request.response.headers.set('Content-Type', 'application/json')

View File

@ -0,0 +1,17 @@
import ResponseFactory from './ResponseFactory.ts'
import {Request} from '../Request.ts'
export default class HTMLResponseFactory extends ResponseFactory {
constructor(
public readonly value: string,
) {
super()
}
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.headers.set('Content-Type', 'text/html')
request.response.body = this.value
return request
}
}

View File

@ -9,6 +9,7 @@ export default class JSONResponseFactory extends ResponseFactory {
}
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.headers.set('Content-Type', 'application/json')
request.response.body = JSON.stringify(this.value)
return request

View File

@ -2,5 +2,15 @@ import AppClass from '../../lifecycle/AppClass.ts'
import {Request} from '../Request.ts'
export default abstract class ResponseFactory extends AppClass {
public abstract async write(request: Request): Promise<Request>
protected target_status: number = 200
public async write(request: Request): Promise<Request> {
request.response.status = this.target_status
return request
}
public status(status: number): ResponseFactory {
this.target_status = status
return this
}
}

View File

@ -9,6 +9,7 @@ export default class StringResponseFactory extends ResponseFactory {
}
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.body = this.value
return request
}

View File

@ -0,0 +1,18 @@
import ResponseFactory from './ResponseFactory.ts'
import {Request} from '../Request.ts'
export default class TemporaryRedirectResponseFactory extends ResponseFactory {
protected target_status: number = 301
constructor(
public readonly destination: string,
) {
super()
}
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.headers.set('Location', this.destination)
return request
}
}

View File

@ -0,0 +1,28 @@
import JSONResponseFactory from './JSONResponseFactory.ts'
import {make} from '../../../../di/src/global.ts'
import HTMLResponseFactory from './HTMLResponseFactory.ts'
import ErrorResponseFactory from './ErrorResponseFactory.ts'
import {Rehydratable} from '../../support/Rehydratable.ts'
import DehydratedStateResponseFactory from './DehydratedStateResponseFactory.ts'
import TemporaryRedirectResponseFactory from './TemporaryRedirectResponseFactory.ts'
export function json(value: any): JSONResponseFactory {
return make(JSONResponseFactory, value)
}
export function html(value: string): HTMLResponseFactory {
return make(HTMLResponseFactory, value)
}
export function error(error: Error | string, status: number = 500): ErrorResponseFactory {
if ( typeof error === 'string' ) error = new Error(error)
return make(ErrorResponseFactory, error, status)
}
export function dehydrate(value: Rehydratable): DehydratedStateResponseFactory {
return make(DehydratedStateResponseFactory, value)
}
export function redirect(destination: string): TemporaryRedirectResponseFactory {
return make(TemporaryRedirectResponseFactory, destination)
}