import { ServerResponse } from "@rill/http"; import { Types as T } from "./_types"; import Context from "./context"; export default class Response { ctx: Context; original: ServerResponse; /** The status code for the response (eg: 404) */ status?: number; /** The status message for the response (eg: Not Found) */ message?: string; /** An object with all of the http headers for the response. */ headers: T.Headers; /** The body to send out at the end of the request. */ body: any; /** This is set to true once the response has been sent. */ finished: boolean; /** Set this to false to disable sending any response. */ respond: boolean; /** Set this to false to disable ending the request after sending headers. */ end: boolean; /** Allow tools to attach methods/properties. */ [x: string]: any; /** * @description * Wrapper around nodejs `ServerResponse`. * * @param ctx The related Rill Context for the response. * @param original The original ServerResponse from rill/http. */ constructor(ctx: Context, original: ServerResponse); /** * Utility to retrieve a header from the response headers. * * @example * response.get('Content-Type') */ get(name: string): string | string[] | undefined; /** * Utility to overwrite a header on the response headers. * * @example * response.set('Content-Type', 'text/html') */ set(name: string, value: string | string[]): void; /** * Utility to add or set a header on the response headers. * * @example * response.append('Set-Cookie', 'a=1') * response.append('Set-Cookie', 'b=1') * response.get('Set-Cookie') // -> ['a=1', 'b=1'] */ append(name: string, value: string | string[]): void; /** * Utility to remove a header from the response headers. * * @example * response.remove('Content-Type') */ remove(name: string): void; /** * Appends to the current set-cookie header, adding a new cookie with options. * * @example * response.cookie('auth-token', 'abc123', { httoOnly: true }) * * @param name - The name of the cookie. * @param value - The value for the cookie. * @param [options] - Options for the cookie. */ cookie(name: string, value: string, options?: T.CookieOptions): void; /** * Deletes a cookie from the current set-cookie header. * * @example * response.clearCookie('auth-token') */ clearCookie(name: string, options?: T.CookieOptions): void; /** * Attaches location headers relative to the current request to perform a redirect. * Will redirect to the referrer if "back" is supplied as a url. * * @example * response.redirect('/home') // redirect back to home page. */ redirect(url: string, alt?: string): void; /** * Attaches refresh headers relative to the current request to perform a timed refresh of the page. * Will refresh to the referrer if "back" is supplied as a url. * * @example * response.refresh(2, '/home') // redirect the user home after 2 seconds. */ refresh(delay?: number | string, url?: string, alt?: string): void; }