From 16d2dc459bcf706906836b9334101427c1b79279 Mon Sep 17 00:00:00 2001 From: Esperanza Horn Date: Fri, 3 May 2024 16:37:03 -0400 Subject: [PATCH] fixup! feat: Stringify config data if it's an object --- src/lib/utils.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index f8254ad..be6eb5c 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,14 +4,25 @@ import { EVENT_MAP } from './constants'; const defaultHanlder = () => null; -export const buildQueryString = ( - config: Record>, -): string => Object.keys(config).map((key) => { - if (typeof config[key] === 'string') { - return `${key}=${config[key]}` +type QueryValue = string | number | boolean | QueryObject; +interface QueryObject { [key: string]: QueryValue; } + +export const buildQueryString = (config: QueryObject, configKey?: string): string => { + const pairs: string[] = []; + + for (const [key, value] of Object.entries(config)) { + const nestedKey = configKey ? `${configKey}[${encodeURIComponent(key)}]` : encodeURIComponent(key); + const valueIsObject = typeof value === 'object' && value !== null && !Array.isArray(value); + + if (valueIsObject) { + pairs.push(buildQueryString(value as QueryObject, nestedKey)); + } else { + pairs.push(`${nestedKey}=${encodeURIComponent(String(value))}`); + } } - return `${key}=${JSON.stringify(config[key])}` -}).join('&'); + + return pairs.join('&'); +} export const buildMessageHandler = (handlers: FintocWidgetEventHandlers) => ( (event: WebViewMessageEvent) => {