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 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 {
|
export default class TestController extends Controller {
|
||||||
|
|
||||||
get_home(req: any) {
|
get_home(request: Request) {
|
||||||
req.response.body = 'Hello!'
|
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> {
|
public async write(request: Request): Promise<Request> {
|
||||||
|
request = await super.write(request)
|
||||||
request.response.body = JSON.stringify(this.rehydratable.dehydrate())
|
request.response.body = JSON.stringify(this.rehydratable.dehydrate())
|
||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,26 @@
|
|||||||
import ResponseFactory from "./ResponseFactory.ts";
|
import ResponseFactory from './ResponseFactory.ts'
|
||||||
import {Request} from "../Request.ts";
|
import {Request} from '../Request.ts'
|
||||||
|
|
||||||
export default class ErrorResponseFactory extends ResponseFactory {
|
export default class ErrorResponseFactory extends ResponseFactory {
|
||||||
|
protected target_mode: 'json' | 'html' = 'html'
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public readonly error: Error,
|
public readonly error: Error,
|
||||||
public readonly output: 'json' | 'html' = 'html',
|
status: number = 500,
|
||||||
public readonly status: number = 500,
|
output: 'json' | 'html' = 'html',
|
||||||
) {
|
) {
|
||||||
super()
|
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> {
|
public async write(request: Request): Promise<Request> {
|
||||||
request.response.status = this.status
|
request = await super.write(request)
|
||||||
|
|
||||||
if ( this.output === 'json' ) {
|
if ( this.output === 'json' ) {
|
||||||
request.response.headers.set('Content-Type', 'application/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> {
|
public async write(request: Request): Promise<Request> {
|
||||||
|
request = await super.write(request)
|
||||||
request.response.headers.set('Content-Type', 'application/json')
|
request.response.headers.set('Content-Type', 'application/json')
|
||||||
request.response.body = JSON.stringify(this.value)
|
request.response.body = JSON.stringify(this.value)
|
||||||
return request
|
return request
|
||||||
|
@ -2,5 +2,15 @@ import AppClass from '../../lifecycle/AppClass.ts'
|
|||||||
import {Request} from '../Request.ts'
|
import {Request} from '../Request.ts'
|
||||||
|
|
||||||
export default abstract class ResponseFactory extends AppClass {
|
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> {
|
public async write(request: Request): Promise<Request> {
|
||||||
|
request = await super.write(request)
|
||||||
request.response.body = this.value
|
request.response.body = this.value
|
||||||
return request
|
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