UNPKG

@athenna/http

Version:

The Athenna Http server. Built on top of fastify.

389 lines (388 loc) 8.72 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 { Is, Json, Macroable } from '@athenna/common'; export class Request extends Macroable { constructor(request) { super(); this.request = request; } /** * Get the request id. * * @example * ```ts * console.log(request.id) // '12345' * ``` */ get id() { return this.request.id; } /** * Get the request ip. * * @example * ```ts * console.log(request.ip) // '192.168.0.1' * ``` */ get ip() { return this.request.ip; } /** * Get the request hostname. * * @example * ```ts * console.log(request.hostname) // 'localhost' * ``` */ get hostname() { return this.request.hostname; } /** * Get the server port. * * @example * ```ts * console.log(request.port) // 3000 * ``` */ get port() { return this.getAddressInfo().port; } /** * Get the http version. * * @example * ```ts * console.log(request.version) // 1 * ``` */ get version() { return this.request.raw.httpVersion; } /** * Get the request protocol. * * @example * ```ts * console.log(request.protocol) // 'http' * ``` */ get protocol() { return this.request.protocol; } /** * Get the request method. * * @example * ```ts * console.log(request.method) // 'GET' * ``` */ get method() { return this.request.method; } /** * Get the route name defined in your route file. * * @example * ```ts * console.log(request.routeName) // 'users' * ``` */ get routeName() { // eslint-disable-next-line // @ts-ignore return this.request.routeOptions.config.name; } /** * Get the base url from request. * * @example * ```ts * console.log(request.baseUrl) // '/users/1' * ``` */ get baseUrl() { return this.request.url.split('?')[0]; } /** * Get the base url with host and port info from request. * * @example * ```ts * console.log(request.baseHostUrl) // 'http://localhost:3030/users/1' * ``` */ get baseHostUrl() { return this.getHostUrlFor(this.baseUrl); } /** * Get the route url from request. * * @example * ```ts * console.log(request.routeUrl) // '/users/:id' * ``` */ get routeUrl() { return this.request.routeOptions.url; } /** * Get the route url with host and port info from request. * * @example * ```ts * console.log(request.routeHostUrl) // 'http://localhost:3030/users/:id' * ``` */ get routeHostUrl() { return this.getHostUrlFor(this.routeUrl); } /** * Get the original url from request. * * @example * ```ts * console.log(request.originalUrl) // '/users/1?query=true' * ``` */ get originalUrl() { return this.request.url; } /** * 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() { return this.getHostUrlFor(this.originalUrl); } /** * Get all body from request. * * @example * ```ts * const { name, email } = request.body * ``` */ get body() { return this.request.body || {}; } /** * Get all params from request. * * @example * ```ts * const { id } = request.params * ``` */ get params() { return this.request.params || {}; } /** * Get all queries from request. * * @example * ```ts * const { page, limit } = request.queries * ``` */ get queries() { return this.request.query || {}; } /** * Get all headers from request. * * @example * ```ts * const { accept } = request.headers * ``` */ get headers() { return this.request.headers || {}; } /** * Get a value from the request params or return * the default value. * * @example * ```ts * const id = request.param('id', '1') * ``` */ param(param, defaultValue) { return Json.get(this.params, param, defaultValue); } /** * Get a value from the request query param or return * the default value. * * @example * ```ts * const page = request.query('page', '1') * ``` */ query(query, defaultValue) { return Json.get(this.queries, query, defaultValue); } /** * Get a value from the request header or return * the default value. * * @example * ```ts * const accept = request.header('accept', 'application/json') * ``` */ header(header, defaultValue) { return Json.get(this.headers, header, defaultValue); } /** * Get a value from the request body or return * the default value. * * @example * ```ts * const name = request.input('name', 'lenon') * ``` */ input(key, defaultValue) { return this.payload(key, defaultValue); } /** * Get a value from the request body or return * the default value. * * @example * ```ts * const name = request.payload('name', 'lenon') * ``` */ payload(key, defaultValue) { return Json.get(this.body, key, defaultValue); } /** * Get only the selected values from the request body. * * @example * ```ts * const body = request.only(['name', 'email']) * ``` */ only(keys) { const body = {}; Object.keys(this.body).forEach(key => { if (!keys.includes(key)) { return; } body[key] = this.body[key]; }); return body; } /** * Get all the values from the request body except the * selected ones. * * @example * ```ts * const body = request.except(['name']) * ``` */ except(keys) { const body = {}; Object.keys(this.body).forEach(key => { if (keys.includes(key)) { return; } body[key] = this.body[key]; }); return body; } /** * Check if the request is multipart. */ isMultipart() { return this.request.isMultipart(); } /** * Get the form data from the request. */ formData() { return this.request.formData(); } /** * Get the parts from the request. */ parts(options) { return this.request.parts(options); } /** * Get the file from the request. */ file(options) { return this.request.file(options); } /** * Get the files from the request. */ files(options) { return this.request.files(options); } /** * Save the files from the request. */ saveRequestFiles(options) { return this.request.saveRequestFiles(options); } /** * Clean the files from the request. */ cleanRequestFiles() { return this.request.cleanRequestFiles(); } /** * This will get populated as soon as a call to `saveRequestFiles` gets resolved. * Avoiding any future duplicate work */ get savedRequestFiles() { return this.request.savedRequestFiles; } /** * Get the original fastify request. */ getFastifyRequest() { return this.request; } /** * Add the hostname and port to the url. */ getHostUrlFor(url) { let { address, port } = this.getAddressInfo(); if (address === '::1') { address = '127.0.0.1'; } if (!Is.Ip(address) && address !== 'localhost') { return `${this.protocol}://${address}${url}`; } return `${this.protocol}://${address}:${port}${url}`; } /** * 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). */ getAddressInfo() { return this.request.server.server.address(); } }