Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export function createClient(config: {
serverUrl,
appId,
requiresAuth,
token,
};
},

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
removeAccessToken,
getLoginUrl,
} from "./utils/auth-utils.js";
import { appParams } from "./utils/app-params.js";

export {
createClient,
Expand All @@ -15,6 +16,7 @@ export {
saveAccessToken,
removeAccessToken,
getLoginUrl,
appParams
};

export type { Base44Client };
Expand Down
60 changes: 60 additions & 0 deletions src/utils/app-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { isBrowser } from './common';

const toSnakeCase = (str: string) => {
return str.replace(/([A-Z])/g, '_$1').toLowerCase();
}

const getAppParamValue = (
paramName: string,
{ defaultValue = undefined, removeFromUrl = false }: { defaultValue?: any, removeFromUrl?: boolean } = {}
) => {
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) {
localStorage.setItem(storageKey, searchParam);
return searchParam;
}

if (defaultValue) {
localStorage.setItem(storageKey, defaultValue);
return defaultValue;
}

const storedValue = localStorage.getItem(storageKey);

if (storedValue) {
return storedValue;
}

return null;
}

const getAppParams = () => {
if (!isBrowser) {
return {};
}

if (getAppParamValue("clear_access_token") === 'true') {
localStorage.removeItem('base44_access_token');
localStorage.removeItem('token');
}

return {
appId: getAppParamValue("app_id"),
token: getAppParamValue("access_token", { removeFromUrl: true }),
fromUrl: getAppParamValue("from_url", { defaultValue: window.location.href }),
functionsVersion: getAppParamValue("functions_version", { defaultValue: import.meta.env.VITE_BASE44_FUNCTIONS_VERSION }),
}
}


export const appParams = getAppParams()
3 changes: 2 additions & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const isNode = typeof window === "undefined";
export const isInIFrame = !isNode && window.self !== window.top;
export const isBrowser = !isNode;
export const isInIFrame = isBrowser && window.self !== window.top;
11 changes: 11 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_BASE44_APP_ID?: string;
readonly VITE_BASE44_BACKEND_URL?: string;
readonly VITE_BASE44_FUNCTIONS_VERSION?: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}