From 9cb856ed46b0c54095c036632e447931a107f678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Felipe=20Schulle?= Date: Thu, 1 Aug 2024 23:52:16 -0300 Subject: [PATCH] =?UTF-8?q?feat(remove-object-nullish):=20removendo=20obje?= =?UTF-8?q?tos=20que=20s=C3=A3o=20undefined=20ou=20null=20dos=20parametros?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/utils/object-to-url-params.ts | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d8360b3..ffecf14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@brainylab/fetch-wrapper", - "version": "0.5.2", + "version": "0.5.3", "keywords": [ "fetch", "wrapper", diff --git a/src/utils/object-to-url-params.ts b/src/utils/object-to-url-params.ts index f24cffe..1576caa 100644 --- a/src/utils/object-to-url-params.ts +++ b/src/utils/object-to-url-params.ts @@ -1,9 +1,24 @@ +function cleanObject( + obj: Record, +): Record { + const cleanedObj: Record = {}; + Object.keys(obj).forEach((key) => { + const value = obj[key]; + if (value !== undefined && value !== null) { + cleanedObj[key] = value; + } + }); + + return cleanedObj; +} + export function objectToUrlParams( obj: Record, ): string { + const cleanedObj = cleanObject(obj); const params = new URLSearchParams(); - Object.keys(obj).map((key) => { + Object.keys(cleanedObj).map((key) => { if (Object.prototype.hasOwnProperty.call(obj, key)) { if (Array.isArray(obj[key])) { const arr = obj[key] as string[] | number[];