@athenna/http
Version:
The Athenna Http server. Built on top of fastify.
232 lines (231 loc) • 6.05 kB
TypeScript
/**
* @athenna/http
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Macroable } from '@athenna/common';
import type { FastifyReply } from 'fastify';
import type { SendOptions } from '@fastify/static';
import type { Request } from '#src/context/Request';
import type { FastifyHelmetOptions } from '@fastify/helmet';
export declare class Response extends Macroable {
/**
* The fastify response object.
*/
private response;
/**
* The request object from request context.
*/
private request;
constructor(response: FastifyReply, request?: Request);
/**
* Verify if the response has been already sent. Keep
* in mind that this method will only return `true`
* after `response.send()`, `response.view()`,
* `response.sendFile()` or `response.download()` methods
* call.
*
* @example
* ```ts
* if (response.sent) {
* // do something
* }
* ```
*/
get sent(): boolean;
/**
* Get the response body sent in response. Keep
* in mind that this method will only return `true`
* after `response.send()`, `response.view()`,
* `response.sendFile()` or `response.download()` methods
* call.
*
* @example
* ```ts
* const { createdAt, updatedAt } = response.body
* ```
*/
get body(): any | any[];
/**
* Get the status code sent in response. Keep
* in mind that this method will only return `true`
* after `response.send()`, `response.view()`,
* `response.sendFile()` or `response.download()` methods
* call.
*
* @example
* ```ts
* if (response.statusCode !== 200) {
* // do something
* }
* ```
*/
get statusCode(): number;
/**
* Get the headers set in the response.
*
* @example
* ```ts
* const headers = response.headers
* ```
*/
get headers(): any;
/**
* Get the time in MS of how much the request has
* taken to response. Keep in mind that this method
* will only return `true` after `response.send()`,
* `response.view()`, `response.sendFile()` or
* `response.download()` methods call.
*
* @example
* ```ts
* console.log(response.responseTime) // 1000
* ```
*/
get responseTime(): number;
/**
* Terminate the request sending an HTML content to be rendered.
*
* @example
* ```ts
* return response.html('<h1>Hello World!</h1>')
* ```
*/
html(html: string): Promise<this>;
/**
* Terminate the request sending a view to be rendered.
*
* @example
* ```ts
* return response.view('welcome', { name: 'lenon' })
* ```
*/
view(view: string, data?: any): Promise<this>;
/**
* Terminate the request sending the response body or not.
*
* @example
* ```ts
* return response.send({ name: 'lenon' })
* ```
*/
send(data?: any): Promise<Response>;
private getRouteZodSchemas;
/**
* @example
* ```ts
* return response.sendFile('img.png')
* ```
*/
sendFile(filename: string, filepath?: string): Promise<Response>;
/**
* @example
* ```ts
* return response.sendFile('img.png', { cacheControl: false })
* ```
*/
sendFile(filename: string, options?: string | SendOptions): Promise<Response>;
/**
* @example
* ```ts
* return response.sendFile('img.png', Path.tmp(), {
* cacheControl: false
* })
* ```
*/
sendFile(filename: string, filepath?: string, options?: SendOptions): Promise<Response>;
/**
* @example
* ```ts
* return response.download('img.png', 'custom-img.png')
* ```
*/
download(filepath: string, filename: string): Promise<Response>;
/**
* @example
* ```ts
* return response.download('img.png', 'custom-img.png', {
* cacheControl: false
* })
* ```
*/
download(filepath: string, filename: string, options?: SendOptions): Promise<Response>;
/**
* Set the response status code.
*
* @example
* ```ts
* return response.status(200).send()
* ```
*/
status(code: number): Response;
/**
* Add some header to the response.
*
* @example
* ```ts
* response.header('content-type', 'application/json')
*
* return response.header('accept-encoding', 'gzip').send(user)
* ```
*/
header(header: string, value: any): Response;
/**
* Verify if response has some header.
*
* @example
* ```ts
* if (response.hasHeader('content-type')) {
* // do something
* }
* ```
*/
hasHeader(header: string): boolean;
/**
* Add some header safely to the response. This means that
* the header is not going to be added if is already set.
*
* @example
* ```ts
* response.safeHeader('content-type', 'application/json')
* ```
*/
safeHeader(header: string, value: any): Response;
/**
* Remove some header from the response.
*
* @example
* ```ts
* response.removeHeader('content-type')
* ```
*/
removeHeader(header: string): Response;
/**
* Redirect the response to other url. You can also set a
* different status code for the redirect.
*
* @example
* ```ts
* return response.redirect('users', 304)
* ```
*/
redirectTo(url: string, status?: number): Promise<Response>;
/**
* Apply helmet in response.
*
* @example
* ```ts
* return response
* .helmet({ enableCSPNonces: false })
* .view('profile', user)
* ```
*/
helmet(options: FastifyHelmetOptions): Response;
/**
* Get the original fastify response.
*/
getFastifyResponse(): FastifyReply;
}