From 0e49e3ef69c59c7e14ba0f79be3e8ac8781bfa7b Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Sun, 18 Aug 2024 10:11:05 +0530 Subject: [PATCH] fix request class bugs --- lib/rest/request.ts | 36 +++++++++++++++++------------------- package-lock.json | 4 ++-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/lib/rest/request.ts b/lib/rest/request.ts index 63f1405..718bdce 100644 --- a/lib/rest/request.ts +++ b/lib/rest/request.ts @@ -5,33 +5,21 @@ import { ulid } from 'ulid'; import { Validator } from '../validator'; export class Request { - private $payload: Record; private $headers: Record; - private $query: Record; - private $pathParams: Record; - private $body: Record; private $dto: any; private id: string; private $user: Record; constructor(private request: ERequest) { - this.$payload = {}; this.$headers = {}; this.initiate(request); this.id = ulid(); this.$user = null; - this.$query = request.query; - this.$body = request.body; - this.$pathParams = request.params; this.$headers = request.headers; } private initiate(request: ERequest) { - this.$query = request.query; - this.$body = request.body; - this.$pathParams = request.params; this.$headers = request.headers; - this.$payload = { ...this.$query, ...this.$pathParams, ...this.$body }; } logger() {} @@ -45,11 +33,16 @@ export class Request { } all(): Record { - return this.$payload; + return { + ...(this.request.query || {}), + ...(this.request.params || {}), + ...(this.request.body || {}), + }; } input(name: string, defaultValue?: T): T { - return name in this.$payload ? this.$payload[name] : defaultValue; + const payload = this.all(); + return name in payload ? payload[name] : defaultValue; } string(name: string): string { @@ -63,12 +56,14 @@ export class Request { } boolean(name: string): boolean { - const val = this.$payload[name] as string; + const payload = this.all(); + const val = payload[name] as string; return [true, 'yes', 'on', '1', 1, 'true'].includes(val.toLowerCase()); } query>(name?: string): T { - return name ? this.$query[name] : this.$query; + const query: Record = this.request.query || {}; + return name ? query[name] : query; } path>(): T { @@ -177,24 +172,27 @@ export class Request { } has(...keys: string[]): boolean { + const payload = this.all(); for (const key of keys) { - if (!(key in this.$payload)) return false; + if (!(key in payload)) return false; } return true; } hasAny(...keys: string[]): boolean { + const payload = this.all(); for (const key of keys) { - if (key in this.$payload) return true; + if (key in payload) return true; } return false; } missing(...keys: string[]): boolean { + const payload = this.all(); for (const key of keys) { - if (key in this.$payload) return false; + if (key in payload) return false; } return true; diff --git a/package-lock.json b/package-lock.json index 7e9e770..3a58977 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@intentjs/core", - "version": "0.1.2", + "version": "0.1.26", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@intentjs/core", - "version": "0.1.2", + "version": "0.1.26", "license": "MIT", "dependencies": { "@nestjs/config": "^3.2.0",