UNPKG

@tsed/common

Version:
155 lines (154 loc) 4.62 kB
/// <reference types="node" /> import { InjectorService } from "@tsed/di"; import { ServerResponse } from "http"; import { PlatformViews } from "./PlatformViews"; declare global { namespace TsED { interface Response { } } } /** * Platform Response abstraction layer. * @platform */ export declare class PlatformResponse<T extends { [key: string]: any; } = any> { raw: T; platformViews: PlatformViews; constructor(raw: T); /** * Get the current statusCode */ get statusCode(): any; /** * An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals. * * This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on. */ get locals(): any; /** * Create a new instance of PlatformResponse * @param injector * @param res */ static create(injector: InjectorService, res: any): PlatformResponse<any>; static onFinished(res: any, cb: Function): void; /** * Returns the HTTP response header specified by field. The match is case-insensitive. * * ```typescript * response.get('Content-Type') // => "text/plain" * ``` * * @param name */ get(name: string): any; /** * Return the Framework response object (express, koa, etc...) */ getResponse<Res = T>(): Res; /** * Return the Node.js response object */ getRes(): ServerResponse; hasStatus(): boolean; /** * Sets the HTTP status for the response. * * @param status */ status(status: number): this; /** * Set header `field` to `val`, or pass * an object of header fields. * * Examples: * ```typescript * response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' }); * ``` * * Aliased as `res.header()`. */ setHeaders(headers: { [key: string]: any; }): this; setHeader(key: string, item: any): this; /** * Set `Content-Type` response header with `type` through `mime.lookup()` * when it does not contain "/", or set the Content-Type to `type` otherwise. * * Examples: * * res.type('.html'); * res.type('html'); * res.type('json'); * res.type('application/json'); * res.type('png'); */ contentType(contentType: string): this; contentLength(length: number): this; getContentLength(): number | undefined; getContentType(): any; /** * Sets the HTTP response Content-Disposition header field to “attachment”. * If a filename is given, then it sets the Content-Type based on the extension name via res.type(), and sets the Content-Disposition “filename=” parameter. * * ```typescript * res.attachment() * // Content-Disposition: attachment * * res.attachment('path/to/logo.png') * // Content-Disposition: attachment; filename="logo.png" * // Content-Type: image/png * ``` * * @param filename */ attachment(filename: string): this; /** * Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an [HTTP status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html). * If not specified, status defaults to `302 Found`. * * @param status * @param url */ redirect(status: number, url: string): this; /** * Sets the response Location HTTP header to the specified path parameter. * * @param location */ location(location: string): this; /** * Stream the given data. * * @param data */ stream(data: ReadableStream | any): this; /** * Renders a view and sends the rendered HTML string to the client. * * @param path * @param options */ render(path: string, options?: any): Promise<any>; /** * Send any data to your consumer. * * This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data. * It choose the better way to send the data. * * @param data */ body(data: any): this; /** * Add a listener to handler the end of the request/response. * @param cb */ onEnd(cb: Function): this; isDone(): boolean; destroy(): void; isHeadersSent(): boolean; }