first commit
This commit is contained in:
50
src/lib/app-params.js
Normal file
50
src/lib/app-params.js
Normal file
@@ -0,0 +1,50 @@
|
||||
const isNode = typeof window === 'undefined';
|
||||
const windowObj = isNode ? { localStorage: new Map() } : window;
|
||||
const storage = windowObj.localStorage;
|
||||
|
||||
const toSnakeCase = (str) => {
|
||||
return str.replace(/([A-Z])/g, '_$1').toLowerCase();
|
||||
}
|
||||
|
||||
const getAppParamValue = (paramName, { defaultValue = undefined, removeFromUrl = false } = {}) => {
|
||||
if (isNode) {
|
||||
return defaultValue;
|
||||
}
|
||||
const storageKey = `base44_${toSnakeCase(paramName)}`;
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const searchParam = urlParams.get(paramName);
|
||||
if (removeFromUrl) {
|
||||
urlParams.delete(paramName);
|
||||
const newUrl = `${window.location.pathname}${urlParams.toString() ? `?${urlParams.toString()}` : ""
|
||||
}${window.location.hash}`;
|
||||
window.history.replaceState({}, document.title, newUrl);
|
||||
}
|
||||
if (searchParam) {
|
||||
storage.setItem(storageKey, searchParam);
|
||||
return searchParam;
|
||||
}
|
||||
if (defaultValue) {
|
||||
storage.setItem(storageKey, defaultValue);
|
||||
return defaultValue;
|
||||
}
|
||||
const storedValue = storage.getItem(storageKey);
|
||||
if (storedValue) {
|
||||
return storedValue;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const getAppParams = () => {
|
||||
return {
|
||||
appId: getAppParamValue("app_id", { defaultValue: import.meta.env.VITE_BASE44_APP_ID }),
|
||||
serverUrl: getAppParamValue("server_url", { defaultValue: import.meta.env.VITE_BASE44_BACKEND_URL }),
|
||||
token: getAppParamValue("access_token", { removeFromUrl: true }),
|
||||
fromUrl: getAppParamValue("from_url", { defaultValue: window.location.href }),
|
||||
functionsVersion: getAppParamValue("functions_version"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const appParams = {
|
||||
...getAppParams()
|
||||
}
|
||||
Reference in New Issue
Block a user