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!')
|
get_user_home(request: Request) {
|
||||||
|
return redirect('/home')
|
||||||
}
|
}
|
||||||
create_user_home(req: any) {}
|
|
||||||
|
|
||||||
|
create_user_home(request: Request) {
|
||||||
|
return error('Not implemented!')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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