Improve response factories and add helper functions
This commit is contained in:
parent
0d4881de1f
commit
0764085ef9
@ -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!')
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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')
|
||||
|
17
lib/src/http/response/HTMLResponseFactory.ts
Normal file
17
lib/src/http/response/HTMLResponseFactory.ts
Normal 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
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
18
lib/src/http/response/TemporaryRedirectResponseFactory.ts
Normal file
18
lib/src/http/response/TemporaryRedirectResponseFactory.ts
Normal 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
|
||||
}
|
||||
}
|
28
lib/src/http/response/helpers.ts
Normal file
28
lib/src/http/response/helpers.ts
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user