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
7 changes: 7 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export function createClient(config: CreateClientConfig): Base44Client {
options,
functionsVersion,
headers: optionalHeaders,
disableAnalytics = false,
adapter,
} = config;

// Normalize appBaseUrl to always be a string (empty if not provided or invalid)
Expand Down Expand Up @@ -112,6 +114,7 @@ export function createClient(config: CreateClientConfig): Base44Client {
headers,
token,
onError: options?.onError,
adapter,
});

const functionsAxiosClient = createAxiosClient({
Expand All @@ -120,6 +123,7 @@ export function createClient(config: CreateClientConfig): Base44Client {
token,
interceptResponses: false,
onError: options?.onError,
adapter,
});

const serviceRoleHeaders = {
Expand All @@ -132,13 +136,15 @@ export function createClient(config: CreateClientConfig): Base44Client {
headers: serviceRoleHeaders,
token: serviceToken,
onError: options?.onError,
adapter,
});

const serviceRoleFunctionsAxiosClient = createAxiosClient({
baseURL: `${serverUrl}/api`,
headers: functionHeaders,
token: serviceToken,
interceptResponses: false,
adapter,
});

const userAuthModule = createAuthModule(
Expand Down Expand Up @@ -197,6 +203,7 @@ export function createClient(config: CreateClientConfig): Base44Client {
serverUrl,
appId,
userAuthModule,
disabled: disableAnalytics,
}),
cleanup: () => {
userModules.analytics.cleanup();
Expand Down
23 changes: 23 additions & 0 deletions src/client.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AxiosRequestConfig } from "axios";
import type { EntitiesModule } from "./modules/entities.types.js";
import type { IntegrationsModule } from "./modules/integrations.types.js";
import type { AuthModule } from "./modules/auth.types.js";
Expand Down Expand Up @@ -73,6 +74,28 @@ export interface CreateClientConfig {
* @internal
*/
headers?: Record<string, string>;
/**
* Disables the {@link AnalyticsModule | analytics module} entirely.
*
* When `true`, `base44.analytics.track()` becomes a no-op and no background
* processing or heartbeat timers are started. Set this for server-side
* clients (SSR, edge runtimes) where background timers must not run.
* {@linkcode createServerClient | createServerClient()} sets this automatically.
*
* Analytics is also automatically disabled outside a browser environment.
*
* @defaultValue `false`
*/
disableAnalytics?: boolean;
/**
* Axios adapter override for HTTP requests.
*
* By default, axios picks an adapter based on the runtime. Set this to
* `'fetch'` in edge runtimes such as Cloudflare Workers, where the default
* adapter detection can pick the wrong adapter.
* {@linkcode createServerClient | createServerClient()} sets this automatically.
*/
adapter?: AxiosRequestConfig["adapter"];
/**
* Additional client options.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
type CreateClientConfig,
type CreateClientOptions,
} from "./client.js";
import {
createServerClient,
type CreateServerClientOptions,
} from "./server.js";
import { Base44Error, type Base44ErrorJSON } from "./utils/axios-client.js";
import {
getAccessToken,
Expand All @@ -16,6 +20,7 @@ import {
export {
createClient,
createClientFromRequest,
createServerClient,
Base44Error,
getAccessToken,
saveAccessToken,
Expand All @@ -27,6 +32,7 @@ export type {
Base44Client,
CreateClientConfig,
CreateClientOptions,
CreateServerClientOptions,
Base44ErrorJSON,
};

Expand Down
12 changes: 11 additions & 1 deletion src/modules/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,28 @@ export interface AnalyticsModuleArgs {
serverUrl: string;
appId: string;
userAuthModule: AuthModule;
/** Skips all analytics processing when true (e.g. for server-side clients). */
disabled?: boolean;
}

export const createAnalyticsModule = ({
axiosClient,
serverUrl,
appId,
userAuthModule,
disabled = false,
}: AnalyticsModuleArgs) => {
// prevent overflow of events //
const { maxQueueSize, throttleTime, batchSize } = analyticsSharedState.config;

if (!analyticsSharedState.config?.enabled) {
// Analytics is browser-only. Outside a browser (SSR, Cloudflare Workers,
// Node) timers would leak — or throw at global scope in Workers — so the
// module becomes a no-op there, and when explicitly disabled.
if (
disabled ||
typeof window === "undefined" ||
!analyticsSharedState.config?.enabled
) {
return {
track: () => {},
cleanup: () => {},
Expand Down
10 changes: 10 additions & 0 deletions src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
ChangePasswordParams,
ResetPasswordParams,
} from "./auth.types";
import {
setAccessTokenCookie,
clearAccessTokenCookie,
} from "../utils/auth-utils.js";

function isInsideIframe(): boolean {
if (typeof window === "undefined") return false;
Expand Down Expand Up @@ -171,6 +175,9 @@ export function createAuthModule(
}
}

// Clear the cookie that mirrors the token for SSR
clearAccessTokenCookie();

// Determine the from_url parameter
const fromUrl = redirectUrl || window.location.href;

Expand Down Expand Up @@ -203,6 +210,9 @@ export function createAuthModule(
} catch (e) {
console.error("Failed to save token to localStorage:", e);
}

// Mirror the token into a cookie so document requests carry it to SSR
setAccessTokenCookie(token);
}
},

Expand Down
Loading
Loading