UNPKG

@athenna/http

Version:

The Athenna Http server. Built on top of fastify.

300 lines (299 loc) 7.18 kB
/** * @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 type { SavedMultipartFile, FastifyMultipartBaseOptions } from '@fastify/multipart'; import type { FastifyRequest } from 'fastify'; import { Macroable } from '@athenna/common'; import type { BusboyConfig } from '@fastify/busboy'; export declare class Request extends Macroable { /** * The fastify request object. */ private request; constructor(request: FastifyRequest); /** * Get the request id. * * @example * ```ts * console.log(request.id) // '12345' * ``` */ get id(): string; /** * Get the request ip. * * @example * ```ts * console.log(request.ip) // '192.168.0.1' * ``` */ get ip(): string; /** * Get the request hostname. * * @example * ```ts * console.log(request.hostname) // 'localhost' * ``` */ get hostname(): string; /** * Get the server port. * * @example * ```ts * console.log(request.port) // 3000 * ``` */ get port(): number; /** * Get the http version. * * @example * ```ts * console.log(request.version) // 1 * ``` */ get version(): string; /** * Get the request protocol. * * @example * ```ts * console.log(request.protocol) // 'http' * ``` */ get protocol(): 'http' | 'https'; /** * Get the request method. * * @example * ```ts * console.log(request.method) // 'GET' * ``` */ get method(): string; /** * Get the route name defined in your route file. * * @example * ```ts * console.log(request.routeName) // 'users' * ``` */ get routeName(): string; /** * Get the base url from request. * * @example * ```ts * console.log(request.baseUrl) // '/users/1' * ``` */ get baseUrl(): string; /** * Get the base url with host and port info from request. * * @example * ```ts * console.log(request.baseHostUrl) // 'http://localhost:3030/users/1' * ``` */ get baseHostUrl(): string; /** * Get the route url from request. * * @example * ```ts * console.log(request.routeUrl) // '/users/:id' * ``` */ get routeUrl(): string; /** * Get the route url with host and port info from request. * * @example * ```ts * console.log(request.routeHostUrl) // 'http://localhost:3030/users/:id' * ``` */ get routeHostUrl(): string; /** * Get the original url from request. * * @example * ```ts * console.log(request.originalUrl) // '/users/1?query=true' * ``` */ get originalUrl(): string; /** * Get the original url with host and port info from request. * * @example * ```ts * console.log(request.originalHostUrl) // 'http://localhost:3000/users/1?query=true' * ``` */ get originalHostUrl(): string; /** * Get all body from request. * * @example * ```ts * const { name, email } = request.body * ``` */ get body(): any | any[]; /** * Get all params from request. * * @example * ```ts * const { id } = request.params * ``` */ get params(): any; /** * Get all queries from request. * * @example * ```ts * const { page, limit } = request.queries * ``` */ get queries(): any; /** * Get all headers from request. * * @example * ```ts * const { accept } = request.headers * ``` */ get headers(): any; /** * Get a value from the request params or return * the default value. * * @example * ```ts * const id = request.param('id', '1') * ``` */ param(param: string, defaultValue?: any): any; /** * Get a value from the request query param or return * the default value. * * @example * ```ts * const page = request.query('page', '1') * ``` */ query(query: string, defaultValue?: any): any; /** * Get a value from the request header or return * the default value. * * @example * ```ts * const accept = request.header('accept', 'application/json') * ``` */ header(header: string, defaultValue?: any): any; /** * Get a value from the request body or return * the default value. * * @example * ```ts * const name = request.input('name', 'lenon') * ``` */ input(key: string, defaultValue?: any): any; /** * Get a value from the request body or return * the default value. * * @example * ```ts * const name = request.payload('name', 'lenon') * ``` */ payload(key: string, defaultValue?: any): any; /** * Get only the selected values from the request body. * * @example * ```ts * const body = request.only(['name', 'email']) * ``` */ only(keys: string[]): any; /** * Get all the values from the request body except the * selected ones. * * @example * ```ts * const body = request.except(['name']) * ``` */ except(keys: string[]): any; /** * Check if the request is multipart. */ isMultipart(): boolean; /** * Get the form data from the request. */ formData(): Promise<FormData>; /** * Get the parts from the request. */ parts(options?: Omit<BusboyConfig, 'headers'>): AsyncIterableIterator<import("@fastify/multipart").Multipart>; /** * Get the file from the request. */ file(options?: Omit<BusboyConfig, 'headers'> | FastifyMultipartBaseOptions): Promise<import("@fastify/multipart").MultipartFile>; /** * Get the files from the request. */ files(options?: Omit<BusboyConfig, 'headers'> | FastifyMultipartBaseOptions): AsyncIterableIterator<import("@fastify/multipart").MultipartFile>; /** * Save the files from the request. */ saveRequestFiles(options?: Omit<BusboyConfig, 'headers'> & { tmpdir?: string; }): Promise<SavedMultipartFile[]>; /** * Clean the files from the request. */ cleanRequestFiles(): Promise<void>; /** * This will get populated as soon as a call to `saveRequestFiles` gets resolved. * Avoiding any future duplicate work */ get savedRequestFiles(): SavedMultipartFile[] | null; /** * Get the original fastify request. */ getFastifyRequest(): FastifyRequest; /** * Add the hostname and port to the url. */ private getHostUrlFor; /** * Get the address info of the server. This method will return the * port used to listen the server, the family (IPv4, IPv6) and the * server address (127.0.0.1). */ private getAddressInfo; }