TypeDoc all the thngs
This commit is contained in:
@@ -2,10 +2,17 @@ import {ResponseFactory} from "./ResponseFactory"
|
||||
import {Rehydratable} from "@extollo/util"
|
||||
import {Request} from "../lifecycle/Request";
|
||||
|
||||
/**
|
||||
* Helper function that creates a DehydratedStateResponseFactory.
|
||||
* @param value
|
||||
*/
|
||||
export function dehydrate(value: Rehydratable): DehydratedStateResponseFactory {
|
||||
return new DehydratedStateResponseFactory(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Response factor that sends a Rehydratable class' data as JSON.
|
||||
*/
|
||||
export class DehydratedStateResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly rehydratable: Rehydratable
|
||||
|
||||
@@ -3,6 +3,12 @@ import {ErrorWithContext, HTTPStatus} from "@extollo/util"
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import * as api from "./api"
|
||||
|
||||
/**
|
||||
* Helper to create a new ErrorResponseFactory, with the given HTTP status and output format.
|
||||
* @param error
|
||||
* @param status
|
||||
* @param output
|
||||
*/
|
||||
export function error(
|
||||
error: Error | string,
|
||||
status: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
@@ -12,6 +18,9 @@ export function error(
|
||||
return new ErrorResponseFactory(error, status, output)
|
||||
}
|
||||
|
||||
/**
|
||||
* Response factory that renders an Error object to the client in a specified format.
|
||||
*/
|
||||
export class ErrorResponseFactory extends ResponseFactory {
|
||||
protected targetMode: 'json' | 'html' | 'auto' = 'auto'
|
||||
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
|
||||
/**
|
||||
* Helper function that creates a new HTMLResponseFactory.
|
||||
* @param value
|
||||
*/
|
||||
export function html(value: string): HTMLResponseFactory {
|
||||
return new HTMLResponseFactory(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Response factory that writes a string to the response as HTML.
|
||||
*/
|
||||
export class HTMLResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly value: string,
|
||||
|
||||
@@ -2,10 +2,19 @@ import {ErrorResponseFactory} from "./ErrorResponseFactory";
|
||||
import {HTTPError} from "../HTTPError";
|
||||
import {HTTPStatus} from "@extollo/util"
|
||||
|
||||
/**
|
||||
* Helper that generates a new HTTPErrorResponseFactory given the HTTP status and message.
|
||||
* @param status
|
||||
* @param message
|
||||
* @param output
|
||||
*/
|
||||
export function http(status: HTTPStatus, message?: string, output: 'json' | 'html' | 'auto' = 'auto'): HTTPErrorResponseFactory {
|
||||
return new HTTPErrorResponseFactory(new HTTPError(status, message), output)
|
||||
}
|
||||
|
||||
/**
|
||||
* Response factory that renders the given HTTPError in the specified output format.
|
||||
*/
|
||||
export class HTTPErrorResponseFactory extends ErrorResponseFactory {
|
||||
constructor(
|
||||
public readonly error: HTTPError,
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
|
||||
/**
|
||||
* Helper function to create a new JSONResponseFactory of the given value.
|
||||
* @param value
|
||||
*/
|
||||
export function json(value: any): JSONResponseFactory {
|
||||
return new JSONResponseFactory(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Response factory that writes the given object as JSON to the response.
|
||||
*/
|
||||
export class JSONResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly value: any
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
import {HTTPStatus} from "@extollo/util"
|
||||
import {Request} from "../lifecycle/Request"
|
||||
|
||||
/**
|
||||
* Abstract class that defines "factory" that knows how to write a particular
|
||||
* response to the response object.
|
||||
*/
|
||||
export abstract class ResponseFactory {
|
||||
/** The status that should be set on the response. */
|
||||
protected targetStatus: HTTPStatus = HTTPStatus.OK
|
||||
|
||||
/**
|
||||
* Called to write the response data to the HTTP response object.
|
||||
* @param request
|
||||
*/
|
||||
public async write(request: Request): Promise<Request> {
|
||||
request.response.setStatus(this.targetStatus)
|
||||
return request
|
||||
}
|
||||
|
||||
/** Set the target status of this factory. */
|
||||
public status(status: HTTPStatus) {
|
||||
this.targetStatus = status
|
||||
return this
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
|
||||
/**
|
||||
* Helper function that creates a new StringResponseFactory for the given string value.
|
||||
* @param value
|
||||
*/
|
||||
export function plaintext(value: string): StringResponseFactory {
|
||||
return new StringResponseFactory(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Response factory that renders a given string as the response in plaintext.
|
||||
*/
|
||||
export class StringResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
/** The string to write as the body. */
|
||||
public readonly value: string,
|
||||
) { super() }
|
||||
|
||||
|
||||
@@ -2,14 +2,22 @@ import {ResponseFactory} from "./ResponseFactory";
|
||||
import {HTTPStatus} from "@extollo/util";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
|
||||
/**
|
||||
* Helper function to create a new TemporaryRedirectResponseFactory to the given destination.
|
||||
* @param destination
|
||||
*/
|
||||
export function redirect(destination: string): TemporaryRedirectResponseFactory {
|
||||
return new TemporaryRedirectResponseFactory(destination)
|
||||
}
|
||||
|
||||
/**
|
||||
* Response factory that sends an HTTP redirect to the given destination.
|
||||
*/
|
||||
export class TemporaryRedirectResponseFactory extends ResponseFactory {
|
||||
protected targetStatus: HTTPStatus = HTTPStatus.TEMPORARY_REDIRECT
|
||||
|
||||
constructor(
|
||||
/** THe URL where the client should redirect to. */
|
||||
public readonly destination: string
|
||||
) { super() }
|
||||
|
||||
|
||||
@@ -3,13 +3,25 @@ import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {ViewEngine} from "../../views/ViewEngine";
|
||||
|
||||
/**
|
||||
* Helper function that creates a new ViewResponseFactory to render the given view
|
||||
* with the specified data.
|
||||
* @param name
|
||||
* @param data
|
||||
*/
|
||||
export function view(name: string, data?: {[key: string]: any}): ViewResponseFactory {
|
||||
return new ViewResponseFactory(name, data)
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP response factory that uses the ViewEngine service to render a view
|
||||
* and send it as HTML.
|
||||
*/
|
||||
export class ViewResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
/** The name of the view to render. */
|
||||
public readonly viewName: string,
|
||||
/** Optional data that should be passed to the view engine as params. */
|
||||
public readonly data?: {[key: string]: any}
|
||||
) { super() }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user