|
| 1 | +import { isBrowser } from './common'; |
| 2 | + |
| 3 | +const toSnakeCase = (str: string) => { |
| 4 | + return str.replace(/([A-Z])/g, '_$1').toLowerCase(); |
| 5 | +} |
| 6 | + |
| 7 | +const getAppParamValue = ( |
| 8 | + paramName: string, |
| 9 | + { defaultValue = undefined, removeFromUrl = false }: { defaultValue?: any, removeFromUrl?: boolean } = {} |
| 10 | +) => { |
| 11 | + const storageKey = `base44_${toSnakeCase(paramName)}`; |
| 12 | + const urlParams = new URLSearchParams(window.location.search); |
| 13 | + const searchParam = urlParams.get(paramName); |
| 14 | + |
| 15 | + if (removeFromUrl) { |
| 16 | + urlParams.delete(paramName); |
| 17 | + const newUrl = `${window.location.pathname}${urlParams.toString() ? `?${urlParams.toString()}` : "" |
| 18 | + }${window.location.hash}`; |
| 19 | + window.history.replaceState({}, document.title, newUrl); |
| 20 | + } |
| 21 | + |
| 22 | + if (searchParam) { |
| 23 | + localStorage.setItem(storageKey, searchParam); |
| 24 | + return searchParam; |
| 25 | + } |
| 26 | + |
| 27 | + if (defaultValue) { |
| 28 | + localStorage.setItem(storageKey, defaultValue); |
| 29 | + return defaultValue; |
| 30 | + } |
| 31 | + |
| 32 | + const storedValue = localStorage.getItem(storageKey); |
| 33 | + |
| 34 | + if (storedValue) { |
| 35 | + return storedValue; |
| 36 | + } |
| 37 | + |
| 38 | + return null; |
| 39 | +} |
| 40 | + |
| 41 | +const getAppParams = () => { |
| 42 | + if (!isBrowser) { |
| 43 | + return {}; |
| 44 | + } |
| 45 | + |
| 46 | + if (getAppParamValue("clear_access_token") === 'true') { |
| 47 | + localStorage.removeItem('base44_access_token'); |
| 48 | + localStorage.removeItem('token'); |
| 49 | + } |
| 50 | + |
| 51 | + return { |
| 52 | + appId: getAppParamValue("app_id", { defaultValue: import.meta.env.VITE_BASE44_APP_ID }), |
| 53 | + serverUrl: getAppParamValue("server_url", { defaultValue: import.meta.env.VITE_BASE44_BACKEND_URL }), |
| 54 | + token: getAppParamValue("access_token", { removeFromUrl: true }), |
| 55 | + fromUrl: getAppParamValue("from_url", { defaultValue: window.location.href }), |
| 56 | + functionsVersion: getAppParamValue("functions_version", { defaultValue: import.meta.env.VITE_BASE44_FUNCTIONS_VERSION }), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +export const appParams = getAppParams() |
0 commit comments