UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

148 lines (147 loc) 3.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Collection_1 = __importDefault(require("../../Collections/Collection")); class FormRequest { request; /** * The logger instance. */ _logger; constructor(request) { this.request = request; // } /** * Set the request logger. */ setLogger(logger) { this._logger = logger; } /** * Get the logger instance. */ logger() { return this._logger; } /** * Get params from route. */ segments() { return new Collection_1.default(this.request.params); } /** * Get param from route. */ segment(key, callback) { return this.segments().get(key, callback); } /** * Get params from route for the given key. */ route(key, callback) { return this.segment(key, callback); } /** * Get collection of request attributes. */ collect() { return new Collection_1.default(this.request.query ?? {}).merge(this.request.body ?? {}); } /** * Get value from request. */ get(key, callback) { return this.collect().get(key, callback); } /** * Get all attributes from request body and query. */ all(keys = []) { return keys.length > 0 ? this.only(keys) : this.collect().all(); } /** * Get only given keys from request body and query. */ only(keys = []) { return this.collect() .only(Array.isArray(keys) ? keys : [keys]) .all(); } /** * Get value from request body. */ input(key, callback) { return new Collection_1.default(this.request?.body).get(key, callback); } /** * Get value from query strings. */ query(key, callback) { return new Collection_1.default(this.request?.query).get(key, callback); } /** * Get value from request body and query as string. */ string(key, callback) { return this.exists(key) ? String(this.get(key)) : this.get(key, callback); } /** * Get value from request body and query as string. */ number(key, callback) { return this.exists(key) ? Number(this.get(key)) : this.get(key, callback); } /** * Get value from request body and query as boolean. */ boolean(key, callback) { return Boolean(this.get(key, callback)); } /** * Get value from request body and query as array. */ array(key, callback) { if (!this.exists(key)) { return this.get(key, callback); } const value = this.get(key, callback); return Array.isArray(value) ? value : [value]; } /** * Check if given key exists in request body or query parameters and has valid value. */ filled(keys) { if (!this.exists(keys)) { return false; } return new Collection_1.default(this.only(keys)) .filter((value) => ![null, undefined, '', [], {}].includes(value)) .isNotEmpty(); } /** * Check if given key exists in request body or query parameters. */ exists(keys) { return this.collect().has(keys); } /** * Get the real request instance. */ getRequest() { return this.request; } /** * Check the request method type. */ isMethod(method) { return this.request.method.toLowerCase() === method.toLowerCase(); } } exports.default = FormRequest;