@base44-preview/sdk 0.8.6-pr.57.d5771ed → 0.8.6-pr.57.faeb046
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.js
CHANGED
|
@@ -99,13 +99,14 @@ export function createClient(config) {
|
|
|
99
99
|
token: serviceToken,
|
|
100
100
|
interceptResponses: false,
|
|
101
101
|
});
|
|
102
|
+
const userAuthModule = createAuthModule(axiosClient, functionsAxiosClient, appId, {
|
|
103
|
+
appBaseUrl,
|
|
104
|
+
serverUrl,
|
|
105
|
+
});
|
|
102
106
|
const userModules = {
|
|
103
107
|
entities: createEntitiesModule(axiosClient, appId),
|
|
104
108
|
integrations: createIntegrationsModule(axiosClient, appId),
|
|
105
|
-
auth:
|
|
106
|
-
appBaseUrl,
|
|
107
|
-
serverUrl,
|
|
108
|
-
}),
|
|
109
|
+
auth: userAuthModule,
|
|
109
110
|
functions: createFunctionsModule(functionsAxiosClient, appId),
|
|
110
111
|
agents: createAgentsModule({
|
|
111
112
|
axios: axiosClient,
|
|
@@ -118,8 +119,9 @@ export function createClient(config) {
|
|
|
118
119
|
users: createUsersModule(axiosClient, appId),
|
|
119
120
|
analytics: createAnalyticsModule({
|
|
120
121
|
axiosClient,
|
|
122
|
+
serverUrl,
|
|
121
123
|
appId,
|
|
122
|
-
|
|
124
|
+
userAuthModule,
|
|
123
125
|
}),
|
|
124
126
|
cleanup: () => {
|
|
125
127
|
if (socket) {
|
package/dist/client.types.d.ts
CHANGED
|
@@ -6,7 +6,6 @@ import type { ConnectorsModule } from "./modules/connectors.types.js";
|
|
|
6
6
|
import type { FunctionsModule } from "./modules/functions.types.js";
|
|
7
7
|
import type { AgentsModule } from "./modules/agents.types.js";
|
|
8
8
|
import type { AppLogsModule } from "./modules/app-logs.types.js";
|
|
9
|
-
import type { AnalyticsModuleOptions } from "./modules/analytics.types.js";
|
|
10
9
|
/**
|
|
11
10
|
* Options for creating a Base44 client.
|
|
12
11
|
*/
|
|
@@ -15,7 +14,6 @@ export interface CreateClientOptions {
|
|
|
15
14
|
* Optional error handler that will be called whenever an API error occurs.
|
|
16
15
|
*/
|
|
17
16
|
onError?: (error: Error) => void;
|
|
18
|
-
analytics?: AnalyticsModuleOptions;
|
|
19
17
|
}
|
|
20
18
|
/**
|
|
21
19
|
* Configuration for creating a Base44 client.
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { AxiosInstance } from "axios";
|
|
2
2
|
import { TrackEventParams, AnalyticsModuleOptions } from "./analytics.types";
|
|
3
|
+
import type { AuthModule } from "./auth.types";
|
|
3
4
|
export interface AnalyticsModuleArgs {
|
|
4
5
|
axiosClient: AxiosInstance;
|
|
6
|
+
serverUrl: string;
|
|
5
7
|
appId: string;
|
|
6
|
-
|
|
8
|
+
userAuthModule: AuthModule;
|
|
7
9
|
}
|
|
8
|
-
export declare const createAnalyticsModule: ({ axiosClient, appId,
|
|
10
|
+
export declare const createAnalyticsModule: ({ axiosClient, serverUrl, appId, userAuthModule, }: AnalyticsModuleArgs) => {
|
|
9
11
|
track: (params: TrackEventParams) => void;
|
|
10
12
|
};
|
|
13
|
+
export declare function getAnalyticsModuleOptionsFromUrlParams(): AnalyticsModuleOptions | undefined;
|
|
@@ -1,16 +1,30 @@
|
|
|
1
|
-
import { getSharedInstance,
|
|
1
|
+
import { getSharedInstance, } from "../utils/singleton";
|
|
2
|
+
///////////////////////////////////////////////
|
|
3
|
+
//// shared queue for analytics events ////
|
|
4
|
+
///////////////////////////////////////////////
|
|
2
5
|
const ANALYTICS_SHARED_STATE_NAME = "analytics";
|
|
3
6
|
// shared state//
|
|
4
7
|
const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () => ({
|
|
5
8
|
requestsQueue: [],
|
|
9
|
+
isProcessing: false,
|
|
6
10
|
}));
|
|
7
|
-
export const createAnalyticsModule = ({ axiosClient, appId,
|
|
11
|
+
export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
|
|
12
|
+
var _a;
|
|
8
13
|
// prevent overflow of events //
|
|
9
|
-
const
|
|
10
|
-
const
|
|
14
|
+
const { enabled = true, maxQueueSize = 1000, throttleTime = 1000, batchSize = 30, } = (_a = getAnalyticsModuleOptionsFromUrlParams()) !== null && _a !== void 0 ? _a : {};
|
|
15
|
+
const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
|
|
16
|
+
let sessionContext = null;
|
|
17
|
+
const getSessionContext = async () => {
|
|
18
|
+
if (sessionContext)
|
|
19
|
+
return sessionContext;
|
|
20
|
+
const user = await userAuthModule.me();
|
|
21
|
+
sessionContext = {
|
|
22
|
+
user_id: user.id,
|
|
23
|
+
};
|
|
24
|
+
return sessionContext;
|
|
25
|
+
};
|
|
11
26
|
const track = (params) => {
|
|
12
|
-
if (!
|
|
13
|
-
analyticsSharedState.requestsQueue.length >= MAX_QUEUE_SIZE) {
|
|
27
|
+
if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
|
|
14
28
|
return;
|
|
15
29
|
}
|
|
16
30
|
const intrinsicData = getEventIntrinsicData();
|
|
@@ -19,27 +33,58 @@ export const createAnalyticsModule = ({ axiosClient, appId, options, }) => {
|
|
|
19
33
|
...intrinsicData,
|
|
20
34
|
});
|
|
21
35
|
};
|
|
22
|
-
const
|
|
23
|
-
const apiEvents = eventsData.map(transformEventDataToApiRequestData);
|
|
36
|
+
const batchRequestFallback = async (events) => {
|
|
24
37
|
await axiosClient.request({
|
|
25
38
|
method: "POST",
|
|
26
39
|
url: `/apps/${appId}/analytics/track/batch`,
|
|
27
|
-
data: { events
|
|
40
|
+
data: { events },
|
|
28
41
|
});
|
|
29
42
|
};
|
|
43
|
+
const flush = async (eventsData) => {
|
|
44
|
+
const sessionContext_ = sessionContext !== null && sessionContext !== void 0 ? sessionContext : (await getSessionContext());
|
|
45
|
+
const events = eventsData.map(transformEventDataToApiRequestData(sessionContext_));
|
|
46
|
+
const beaconPayload = JSON.stringify({ events });
|
|
47
|
+
if (typeof navigator === "undefined" ||
|
|
48
|
+
beaconPayload.length > 60000 ||
|
|
49
|
+
!navigator.sendBeacon(trackBatchUrl, beaconPayload)) {
|
|
50
|
+
// beacon didn't work, fallback to axios
|
|
51
|
+
await batchRequestFallback(events);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
if (typeof window !== "undefined" && enabled) {
|
|
55
|
+
window.addEventListener("visibilitychange", () => {
|
|
56
|
+
if (document.visibilityState === "hidden") {
|
|
57
|
+
analyticsSharedState.isProcessing = false;
|
|
58
|
+
// flush entire queue on visibility change and hope for the best //
|
|
59
|
+
const eventsData = analyticsSharedState.requestsQueue.splice(0);
|
|
60
|
+
flush(eventsData);
|
|
61
|
+
}
|
|
62
|
+
else if (document.visibilityState === "visible") {
|
|
63
|
+
startAnalyticsProcessor(flush, {
|
|
64
|
+
throttleTime,
|
|
65
|
+
batchSize,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
30
70
|
// start analytics processor only if it's the first instance and analytics is enabled //
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
71
|
+
if (enabled) {
|
|
72
|
+
startAnalyticsProcessor(flush, {
|
|
73
|
+
throttleTime,
|
|
74
|
+
batchSize,
|
|
75
|
+
});
|
|
34
76
|
}
|
|
35
77
|
return {
|
|
36
78
|
track,
|
|
37
79
|
};
|
|
38
80
|
};
|
|
39
81
|
async function startAnalyticsProcessor(handleTrack, options) {
|
|
82
|
+
if (analyticsSharedState.isProcessing) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
analyticsSharedState.isProcessing = true;
|
|
40
86
|
const { throttleTime = 1000, batchSize = 30 } = options !== null && options !== void 0 ? options : {};
|
|
41
|
-
while (
|
|
42
|
-
await new Promise((resolve) => setTimeout(resolve, throttleTime));
|
|
87
|
+
while (analyticsSharedState.isProcessing) {
|
|
43
88
|
const requests = analyticsSharedState.requestsQueue.splice(0, batchSize);
|
|
44
89
|
if (requests.length > 0) {
|
|
45
90
|
try {
|
|
@@ -50,19 +95,33 @@ async function startAnalyticsProcessor(handleTrack, options) {
|
|
|
50
95
|
console.error("Error processing analytics request:", error);
|
|
51
96
|
}
|
|
52
97
|
}
|
|
98
|
+
await new Promise((resolve) => setTimeout(resolve, throttleTime));
|
|
53
99
|
}
|
|
54
100
|
}
|
|
55
101
|
function getEventIntrinsicData() {
|
|
56
102
|
return {
|
|
57
103
|
timestamp: new Date().toISOString(),
|
|
58
|
-
pageUrl: typeof window !== "undefined" ? window.location.
|
|
104
|
+
pageUrl: typeof window !== "undefined" ? window.location.pathname : null,
|
|
59
105
|
};
|
|
60
106
|
}
|
|
61
|
-
function transformEventDataToApiRequestData(
|
|
62
|
-
return {
|
|
107
|
+
function transformEventDataToApiRequestData(sessionContext) {
|
|
108
|
+
return (eventData) => ({
|
|
63
109
|
event_name: eventData.eventName,
|
|
64
110
|
properties: eventData.properties,
|
|
65
111
|
timestamp: eventData.timestamp,
|
|
66
112
|
page_url: eventData.pageUrl,
|
|
67
|
-
|
|
113
|
+
...sessionContext,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
export function getAnalyticsModuleOptionsFromUrlParams() {
|
|
117
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
118
|
+
const jsonString = urlParams.get("analytics");
|
|
119
|
+
if (!jsonString)
|
|
120
|
+
return undefined;
|
|
121
|
+
try {
|
|
122
|
+
return JSON.parse(jsonString);
|
|
123
|
+
}
|
|
124
|
+
catch (_a) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
68
127
|
}
|
|
@@ -13,12 +13,15 @@ export type TrackEventData = {
|
|
|
13
13
|
properties?: TrackEventProperties;
|
|
14
14
|
eventName: string;
|
|
15
15
|
} & TrackEventIntrinsicData;
|
|
16
|
+
export type SessionContext = {
|
|
17
|
+
user_id?: string | null;
|
|
18
|
+
};
|
|
16
19
|
export type AnalyticsApiRequestData = {
|
|
17
20
|
event_name: string;
|
|
18
21
|
properties?: TrackEventProperties;
|
|
19
22
|
timestamp?: string;
|
|
20
23
|
page_url?: string | null;
|
|
21
|
-
};
|
|
24
|
+
} & SessionContext;
|
|
22
25
|
export type AnalyticsApiBatchRequest = {
|
|
23
26
|
method: "POST";
|
|
24
27
|
url: `/apps/${string}/analytics/track/batch`;
|
|
@@ -28,8 +31,7 @@ export type AnalyticsApiBatchRequest = {
|
|
|
28
31
|
};
|
|
29
32
|
export type AnalyticsModuleOptions = {
|
|
30
33
|
enabled?: boolean;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
};
|
|
34
|
+
maxQueueSize?: number;
|
|
35
|
+
throttleTime?: number;
|
|
36
|
+
batchSize?: number;
|
|
35
37
|
};
|
package/dist/utils/singleton.js
CHANGED
|
@@ -5,8 +5,9 @@ export function getSharedInstance(name, factory) {
|
|
|
5
5
|
windowObj.base44 = {};
|
|
6
6
|
}
|
|
7
7
|
if (!windowObj.base44[name]) {
|
|
8
|
-
windowObj.base44[name] = { instance: factory(), _refCount:
|
|
8
|
+
windowObj.base44[name] = { instance: factory(), _refCount: 0 };
|
|
9
9
|
}
|
|
10
|
+
windowObj.base44[name]._refCount++;
|
|
10
11
|
return windowObj.base44[name].instance;
|
|
11
12
|
}
|
|
12
13
|
export function getSharedInstanceRefCount(name) {
|