Skip to content

Commit d3d56cb

Browse files
committed
app params
1 parent 84de8a5 commit d3d56cb

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
removeAccessToken,
77
getLoginUrl,
88
} from "./utils/auth-utils.js";
9+
import { appParams } from "./utils/app-params.js";
910

1011
export {
1112
createClient,
@@ -15,6 +16,7 @@ export {
1516
saveAccessToken,
1617
removeAccessToken,
1718
getLoginUrl,
19+
appParams
1820
};
1921

2022
export type { Base44Client };

src/utils/app-params.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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()

src/utils/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const isNode = typeof window === "undefined";
2-
export const isInIFrame = !isNode && window.self !== window.top;
2+
export const isBrowser = !isNode;
3+
export const isInIFrame = isBrowser && window.self !== window.top;

src/vite-env.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference types="vite/client" />
2+
3+
interface ImportMetaEnv {
4+
readonly VITE_BASE44_APP_ID?: string;
5+
readonly VITE_BASE44_BACKEND_URL?: string;
6+
readonly VITE_BASE44_FUNCTIONS_VERSION?: string;
7+
}
8+
9+
interface ImportMeta {
10+
readonly env: ImportMetaEnv;
11+
}

0 commit comments

Comments
 (0)