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.

91 lines
3.2 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 PartialViewResponseFactory from './PartialViewResponseFactory.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 context and layout.
* @param {string} view
* @param [context]
* @param {string} [layout]
* @return ViewResponseFactory
*/
export function view(view: string, context?: any, layout?: string): ViewResponseFactory {
return make(ViewResponseFactory, view, context, layout)
}
/**
* Get a new partial view response factory for the given view name, passing along context.
* @param {string} view
* @param [context]
* @return PartialViewResponseFactory
*/
export function partial(view: string, context?: any): PartialViewResponseFactory {
return make(PartialViewResponseFactory, view, context)
}