You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.1 KiB

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'
import {HTTPStatus} from '../../const/http.ts'
import HTTPErrorResponseFactory from './HTTPErrorResponseFactory.ts'
import HTTPError from '../../error/HTTPError.ts'
import ViewResponseFactory from './ViewResponseFactory.ts'
import FileResponseFactory from './FileResponseFactory.ts'
import {PathLike} from '../../support/UniversalPath.ts'
/**
* Get a new JSON response factory that writes the given object as JSON.
* @param value
* @return JSONResponseFactory
*/
export function json(value: any): JSONResponseFactory {
return make(JSONResponseFactory, value)
}
/**
* Get a new HTML response factory that writes the given string as HTML.
* @param value
* @return HTMLResponseFactory
*/
export function html(value: string): HTMLResponseFactory {
return make(HTMLResponseFactory, value)
}
/**
* Get a new Error response factory that writes the given error.
* @param {Error|string} error
* @param {number} [status = 500] - the HTTP response status
* @return ErrorResponseFactory
*/
export function error(error: Error | string, status: number = 500): ErrorResponseFactory {
if ( typeof error === 'string' ) error = new Error(error)
return make(ErrorResponseFactory, error, status)
}
/**
* Get a new dehydrated response factory that dehydrates the given object and writes its state as JSON.
* @param {Rehydratable} value
* @return DehydratedStateResponseFactory
*/
export function dehydrate(value: Rehydratable): DehydratedStateResponseFactory {
return make(DehydratedStateResponseFactory, value)
}
/**
* Get a new temporary redirect response factory that redirects to the given destination.
* @param {string} destination
* @return TemporaryRedirectResponseFactory
*/
export function redirect(destination: string): TemporaryRedirectResponseFactory {
return make(TemporaryRedirectResponseFactory, destination)
}
/**
* Get a new http error response factory for the given http status code.
* @param {HTTPStatus} status
* @param {string} [message]
* @return HTTPErrorResponseFactory
*/
export function http(status: HTTPStatus, message?: string): HTTPErrorResponseFactory {
return make(HTTPErrorResponseFactory, new HTTPError(status, message))
}
/**
* Get a new view response factory for the given view name, passing along any view data.
* @param {string} view
* @param [data]
* @return ViewResponseFactory
*/
export function view(view: string, data?: any): ViewResponseFactory {
return make(ViewResponseFactory, view, data)
}
/**
* Get a new file response factory for the given file path.
* @param {string} path
* @return FileResponseFactory
*/
export function file(path: PathLike): FileResponseFactory {
return make(FileResponseFactory, path)
}