# Class PlatformResponse

Class platform
Module
import { PlatformResponse } from "@tsed/common"
Source/packages/common/src/platform/services/PlatformResponse.ts

# Overview

class PlatformResponse<T extends {
    [key: string]: any;
} = any> {
    raw: T;
    platformViews: PlatformViews;
    data: any;
    constructor(raw: T);
get <span class="token function">statusCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">;</span>

get <span class="token function">locals</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">;</span>

<span class="token keyword">static</span> <span class="token function">create</span><span class="token punctuation">(</span>injector<span class="token punctuation">:</span> <a href="/api/di/services/InjectorService.html"><span class="token">InjectorService</span></a><span class="token punctuation">,</span> res<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">)</span><span class="token punctuation">:</span> PlatformResponse&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token function">onFinished</span><span class="token punctuation">(</span>res<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> cb<span class="token punctuation">:</span> Function<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>

<span class="token function">get</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">;</span>
<span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Record&lt;<span class="token keyword">string</span><span class="token punctuation">,</span> <span class="token keyword">number</span> | <span class="token keyword">string</span> | <span class="token keyword">string</span><span class="token punctuation">[</span><span class="token punctuation">]</span>&gt;<span class="token punctuation">;</span>

getResponse&lt;<a href="/api/common/mvc/decorators/params/Res.html"><span class="token">Res</span></a><span class="token punctuation"> = </span>T&gt;<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/common/mvc/decorators/params/Res.html"><span class="token">Res</span></a><span class="token punctuation">;</span>

<span class="token function">getRes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> ServerResponse<span class="token punctuation">;</span>
<span class="token function">hasStatus</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">boolean</span><span class="token punctuation">;</span>

<span class="token function">status</span><span class="token punctuation">(</span>status<span class="token punctuation">:</span> <span class="token keyword">number</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>

<span class="token function">setHeaders</span><span class="token punctuation">(</span>headers<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token punctuation">[</span>key<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>
<span class="token function">setHeader</span><span class="token punctuation">(</span>key<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">,</span> item<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>

<span class="token function">contentType</span><span class="token punctuation">(</span>contentType<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>
<span class="token function">contentLength</span><span class="token punctuation">(</span>length<span class="token punctuation">:</span> <span class="token keyword">number</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>
<span class="token function">getContentLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">number</span> | undefined<span class="token punctuation">;</span>
<span class="token function">getContentType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">;</span>

<span class="token function">attachment</span><span class="token punctuation">(</span>filename<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>

<span class="token function">redirect</span><span class="token punctuation">(</span>status<span class="token punctuation">:</span> <span class="token keyword">number</span><span class="token punctuation">,</span> url<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>

<span class="token function">location</span><span class="token punctuation">(</span>location<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>

<span class="token function">stream</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> ReadableStream | <span class="token keyword">any</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>

<span class="token function">render</span><span class="token punctuation">(</span>path<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">,</span> options?<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Promise&lt;<span class="token keyword">string</span>&gt;<span class="token punctuation">;</span>

<span class="token function">body</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>
<span class="token function">getBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">;</span>

<span class="token function">onEnd</span><span class="token punctuation">(</span>cb<span class="token punctuation">:</span> Function<span class="token punctuation">)</span><span class="token punctuation">:</span> this<span class="token punctuation">;</span>
<span class="token function">isDone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">boolean</span><span class="token punctuation">;</span>
<span class="token function">destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
<span class="token function">isHeadersSent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">boolean</span><span class="token punctuation">;</span>

}

# Description

Platform Response abstraction layer.

# Members

[key: string]: any;

} = any> {

raw: T;

platformViews: PlatformViews;

data: any;

get statusCode(): any;

Get the current statusCode


get locals(): 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.


static create(injector: InjectorService, res: any): PlatformResponse<any>;

Create a new instance of PlatformResponse


static onFinished(res: any, cb: Function): void;

get(name: string): any;

Returns the HTTP response header specified by field. The match is case-insensitive.

response.get('Content-Type') // => "text/plain"
1

getHeaders(): Record<string, number | string | string[]>;

getResponse<Res = T>(): Res;

Return the Framework response object (express, koa, etc...)


getRes(): ServerResponse;

Return the Node.js response object


hasStatus(): boolean;

status(status: number): this;

Sets the HTTP status for the response.


setHeaders(headers: {
     [key: string]: any;
 }): this;

Set header field to val, or pass an object of header fields.

Examples:

response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
1

Aliased as res.header().


setHeader(key: string, item: any): this;

contentType(contentType: string): 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');

contentLength(length: number): this;

getContentLength(): number | undefined;

getContentType(): any;

attachment(filename: string): this;

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.

res.attachment()
// Content-Disposition: attachment

res.attachment('path/to/logo.png')
// Content-Disposition: attachment; filename="logo.png"
// Content-Type: image/png
1
2
3
4
5
6

redirect(status: number, url: string): this;

Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code. If not specified, status defaults to 302 Found.


location(location: string): this;

Sets the response Location HTTP header to the specified path parameter.


stream(data: ReadableStream | any): this;

Stream the given data.


render(path: string, options?: any): Promise<string>;

Renders a view and sends the rendered HTML string to the client.


body(data: any): this;

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.


getBody(): any;

onEnd(cb: Function): this;

Add a listener to handler the end of the request/response.


isDone(): boolean;

destroy(): void;

isHeadersSent(): boolean;

Other topics