From 744808cf66707a27f7bdc694f5519c80c94758b0 Mon Sep 17 00:00:00 2001 From: Kamil Gabryjelski Date: Thu, 21 Mar 2024 19:17:39 +0100 Subject: [PATCH] feat: Support urlParams (#20) * feat: Support urlParams * 0.1.9 --- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 18 +++++++++++------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 10babb2..ef2d2b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@preset-sdk/embedded", - "version": "0.1.8", + "version": "0.1.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@preset-sdk/embedded", - "version": "0.1.8", + "version": "0.1.9", "license": "UNLICENSED", "dependencies": { "@superset-ui/switchboard": "^0.18.26-0" diff --git a/package.json b/package.json index fff43ab..832f292 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@preset-sdk/embedded", - "version": "0.1.8", + "version": "0.1.9", "description": "Frontend SDK for embedding Preset data analytics into your own application", "access": "public", "keywords": [ diff --git a/src/index.ts b/src/index.ts index fadda32..da12bd9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,9 @@ export type UiConfigType = { visible?: boolean expanded?: boolean } + urlParams?: { + [key: string]: any + } } export type EmbedDashboardParams = { @@ -92,14 +95,15 @@ export async function embedDashboard({ async function mountIframe(): Promise { return new Promise(resolve => { const iframe = document.createElement('iframe'); - const dashboardConfig = dashboardUiConfig ? `?uiConfig=${calculateConfig()}` : "" + const dashboardConfigUrlParams = dashboardUiConfig ? {uiConfig: `${calculateConfig()}`} : undefined; const filterConfig = dashboardUiConfig?.filters || {} const filterConfigKeys = Object.keys(filterConfig) - const filterConfigUrlParams = filterConfigKeys.length > 0 - ? "&" - + filterConfigKeys - .map(key => DASHBOARD_UI_FILTER_CONFIG_URL_PARAM_KEY[key] + '=' + filterConfig[key]).join('&') - : "" + const filterConfigUrlParams = Object.fromEntries(filterConfigKeys.map( + key => [DASHBOARD_UI_FILTER_CONFIG_URL_PARAM_KEY[key], filterConfig[key]])) + + // Allow url query parameters from dashboardUiConfig.urlParams to override the ones from filterConfig + const urlParams = {...dashboardConfigUrlParams, ...filterConfigUrlParams, ...dashboardUiConfig?.urlParams} + const urlParamsString = Object.keys(urlParams).length ? '?' + new URLSearchParams(urlParams).toString() : '' // setup the iframe's sandbox configuration iframe.sandbox.add("allow-same-origin"); // needed for postMessage to work @@ -117,7 +121,7 @@ export async function embedDashboard({ resolve(switchboard); }); - iframe.src = `${supersetDomain}/embedded/${id}${dashboardConfig}${filterConfigUrlParams}`; + iframe.src = `${supersetDomain}/embedded/${id}${urlParamsString}`; mountPoint?.replaceChildren(iframe); log('placed the iframe') });