UNPKG

@tsed/platform-fastify

Version:
73 lines (72 loc) 1.73 kB
import { PlatformResponse } from "@tsed/platform-http"; import contentDisposition from "content-disposition"; /** * @platform * @fastify */ export class PlatformFastifyResponse extends PlatformResponse { constructor($ctx) { super($ctx); this.$ctx = $ctx; this.raw.locals = {}; } /** * Return the Node.js response object */ getRes() { return this.raw.raw; } /** * Sets the HTTP status for the response. * * @param status */ status(status) { this.raw.code(status); return 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) { this.raw.type(contentType); return this; } /** * Returns the HTTP response header specified by field. The match is case-insensitive. * * ```typescript * response.get('Content-Type') // => "text/plain" * ``` * * @param name */ get(name) { return this.raw.getHeader(name); } attachment(filename) { this.setHeader("Content-Disposition", contentDisposition(filename)); return this; } setHeader(key, item) { this.raw.header(key, this.formatHeader(key, item)); return this; } stream(data) { this.raw.send(data); return this; } json(data) { this.raw.send(data); return this; } }