@base44/sdk 0.8.6 → 0.8.7
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/README.md +47 -600
- package/dist/client.d.ts +90 -237
- package/dist/client.js +131 -19
- package/dist/client.types.d.ts +134 -0
- package/dist/client.types.js +1 -0
- package/dist/index.d.ts +12 -3
- package/dist/index.js +1 -1
- package/dist/modules/agents.d.ts +2 -23
- package/dist/modules/agents.types.d.ts +330 -34
- package/dist/modules/analytics.d.ts +18 -0
- package/dist/modules/analytics.js +213 -0
- package/dist/modules/analytics.types.d.ts +42 -0
- package/dist/modules/analytics.types.js +1 -0
- package/dist/modules/app-logs.d.ts +8 -24
- package/dist/modules/app-logs.js +9 -19
- package/dist/modules/app-logs.types.d.ts +44 -0
- package/dist/modules/app-logs.types.js +1 -0
- package/dist/modules/app.types.d.ts +27 -0
- package/dist/modules/auth.d.ts +10 -78
- package/dist/modules/auth.js +24 -42
- package/dist/modules/auth.types.d.ts +436 -0
- package/dist/modules/auth.types.js +1 -0
- package/dist/modules/connectors.d.ts +6 -11
- package/dist/modules/connectors.js +6 -7
- package/dist/modules/connectors.types.d.ts +68 -2
- package/dist/modules/entities.d.ts +8 -5
- package/dist/modules/entities.js +22 -62
- package/dist/modules/entities.types.d.ts +293 -0
- package/dist/modules/entities.types.js +1 -0
- package/dist/modules/functions.d.ts +8 -7
- package/dist/modules/functions.js +7 -5
- package/dist/modules/functions.types.d.ts +50 -0
- package/dist/modules/functions.types.js +1 -0
- package/dist/modules/integrations.d.ts +8 -5
- package/dist/modules/integrations.js +7 -5
- package/dist/modules/integrations.types.d.ts +352 -0
- package/dist/modules/integrations.types.js +1 -0
- package/dist/modules/sso.d.ts +9 -14
- package/dist/modules/sso.js +9 -12
- package/dist/modules/sso.types.d.ts +44 -0
- package/dist/modules/sso.types.js +1 -0
- package/dist/modules/types.d.ts +1 -0
- package/dist/modules/types.js +1 -0
- package/dist/types.d.ts +65 -2
- package/dist/utils/auth-utils.d.ts +107 -45
- package/dist/utils/auth-utils.js +107 -33
- package/dist/utils/auth-utils.types.d.ts +146 -0
- package/dist/utils/auth-utils.types.js +1 -0
- package/dist/utils/axios-client.d.ts +84 -16
- package/dist/utils/axios-client.js +74 -13
- package/dist/utils/axios-client.types.d.ts +28 -0
- package/dist/utils/axios-client.types.js +1 -0
- package/dist/utils/common.d.ts +1 -0
- package/dist/utils/common.js +4 -0
- package/dist/utils/sharedInstance.d.ts +1 -0
- package/dist/utils/sharedInstance.js +15 -0
- package/dist/utils/socket-utils.d.ts +2 -2
- package/package.json +12 -3
package/dist/client.d.ts
CHANGED
|
@@ -1,239 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
};
|
|
4
|
-
export type Base44Client = ReturnType<typeof createClient>;
|
|
1
|
+
import type { Base44Client, CreateClientConfig, CreateClientOptions } from "./client.types.js";
|
|
2
|
+
export type { Base44Client, CreateClientConfig, CreateClientOptions };
|
|
5
3
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
4
|
+
* Creates a Base44 client.
|
|
5
|
+
*
|
|
6
|
+
* This is the main entry point for the Base44 SDK. It creates a client that provides access to the SDK's modules, such as {@linkcode EntitiesModule | entities}, {@linkcode AuthModule | auth}, and {@linkcode FunctionsModule | functions}.
|
|
7
|
+
*
|
|
8
|
+
* Typically, you don't need to call this function because Base44 creates the client for you. You can then import and use the client to make API calls. The client takes care of managing authentication for you.
|
|
9
|
+
*
|
|
10
|
+
* The client supports three authentication modes:
|
|
11
|
+
* - **Anonymous**: Access modules anonymously without authentication using `base44.moduleName`. Operations are scoped to public data and permissions.
|
|
12
|
+
* - **User authentication**: Access modules with user-level permissions using `base44.moduleName`. Operations are scoped to the authenticated user's data and permissions.
|
|
13
|
+
* - **Service role authentication**: Access modules with elevated permissions using `base44.asServiceRole.moduleName`. Operations can access any data available to the app's admin. Can only be used in the backend. Typically, you create a client with service role authentication using the {@linkcode createClientFromRequest | createClientFromRequest()} function in your backend functions.
|
|
14
|
+
*
|
|
15
|
+
* For example, when using the {@linkcode EntitiesModule | entities} module:
|
|
16
|
+
* - **Anonymous**: Can only read public data.
|
|
17
|
+
* - **User authentication**: Can access the current user's data.
|
|
18
|
+
* - **Service role authentication**: Can access all data that admins can access.
|
|
19
|
+
*
|
|
20
|
+
* Most modules are available in all three modes, but with different permission levels. However, some modules are only available in specific authentication modes.
|
|
21
|
+
*
|
|
22
|
+
* @param config - Configuration object for the client.
|
|
23
|
+
* @returns A configured Base44 client instance with access to all SDK modules.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // Create a client for your app
|
|
28
|
+
* import { createClient } from '@base44/sdk';
|
|
29
|
+
*
|
|
30
|
+
* const base44 = createClient({
|
|
31
|
+
* appId: 'my-app-id'
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // Use the client to access your data
|
|
35
|
+
* const products = await base44.entities.Products.list();
|
|
36
|
+
* ```
|
|
15
37
|
*/
|
|
16
|
-
export declare function createClient(config:
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
};
|
|
72
|
-
entities: {};
|
|
73
|
-
integrations: {};
|
|
74
|
-
auth: {
|
|
75
|
-
me(): Promise<import("axios").AxiosResponse<any, any>>;
|
|
76
|
-
updateMe(data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
77
|
-
redirectToLogin(nextUrl: string): void;
|
|
78
|
-
logout(redirectUrl?: string): void;
|
|
79
|
-
setToken(token: string, saveToStorage?: boolean): void;
|
|
80
|
-
loginViaEmailPassword(email: string, password: string, turnstileToken?: string): Promise<{
|
|
81
|
-
access_token: string;
|
|
82
|
-
user: any;
|
|
83
|
-
}>;
|
|
84
|
-
isAuthenticated(): Promise<boolean>;
|
|
85
|
-
inviteUser(userEmail: string, role: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
86
|
-
register(payload: {
|
|
87
|
-
email: string;
|
|
88
|
-
password: string;
|
|
89
|
-
turnstile_token?: string | null;
|
|
90
|
-
referral_code?: string | null;
|
|
91
|
-
}): Promise<import("axios").AxiosResponse<any, any>>;
|
|
92
|
-
verifyOtp({ email, otpCode }: {
|
|
93
|
-
email: string;
|
|
94
|
-
otpCode: string;
|
|
95
|
-
}): Promise<import("axios").AxiosResponse<any, any>>;
|
|
96
|
-
resendOtp(email: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
97
|
-
resetPasswordRequest(email: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
98
|
-
resetPassword({ resetToken, newPassword, }: {
|
|
99
|
-
resetToken: string;
|
|
100
|
-
newPassword: string;
|
|
101
|
-
}): Promise<import("axios").AxiosResponse<any, any>>;
|
|
102
|
-
changePassword({ userId, currentPassword, newPassword, }: {
|
|
103
|
-
userId: string;
|
|
104
|
-
currentPassword: string;
|
|
105
|
-
newPassword: string;
|
|
106
|
-
}): Promise<import("axios").AxiosResponse<any, any>>;
|
|
107
|
-
};
|
|
108
|
-
functions: {
|
|
109
|
-
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
110
|
-
};
|
|
111
|
-
agents: {
|
|
112
|
-
getConversations: () => Promise<import("./types.js").AgentConversation[]>;
|
|
113
|
-
getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
|
|
114
|
-
listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
|
|
115
|
-
createConversation: (conversation: {
|
|
116
|
-
agent_name: string;
|
|
117
|
-
metadata?: Record<string, any>;
|
|
118
|
-
}) => Promise<import("./types.js").AgentConversation>;
|
|
119
|
-
addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
|
|
120
|
-
subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
|
|
121
|
-
getWhatsAppConnectURL: (agentName: string) => string;
|
|
122
|
-
};
|
|
123
|
-
appLogs: {
|
|
124
|
-
logUserInApp(pageName: string): Promise<void>;
|
|
125
|
-
fetchLogs(params?: Record<string, any>): Promise<any>;
|
|
126
|
-
getStats(params?: Record<string, any>): Promise<any>;
|
|
127
|
-
};
|
|
128
|
-
users: {
|
|
129
|
-
inviteUser(user_email: string, role: "user" | "admin"): Promise<any>;
|
|
130
|
-
};
|
|
131
|
-
cleanup: () => void;
|
|
132
|
-
};
|
|
133
|
-
export declare function createClientFromRequest(request: Request): {
|
|
134
|
-
/**
|
|
135
|
-
* Set authentication token for all requests
|
|
136
|
-
* @param {string} newToken - New auth token
|
|
137
|
-
*/
|
|
138
|
-
setToken(newToken: string): void;
|
|
139
|
-
/**
|
|
140
|
-
* Get current configuration
|
|
141
|
-
* @returns {Object} Current configuration
|
|
142
|
-
*/
|
|
143
|
-
getConfig(): {
|
|
144
|
-
serverUrl: string;
|
|
145
|
-
appId: string;
|
|
146
|
-
requiresAuth: boolean;
|
|
147
|
-
};
|
|
148
|
-
asServiceRole: {
|
|
149
|
-
entities: {};
|
|
150
|
-
integrations: {};
|
|
151
|
-
sso: {
|
|
152
|
-
getAccessToken(userid: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
153
|
-
};
|
|
154
|
-
connectors: {
|
|
155
|
-
getAccessToken(integrationType: import("./types.js").ConnectorIntegrationType): Promise<import("./types.js").ConnectorAccessTokenResponse>;
|
|
156
|
-
};
|
|
157
|
-
functions: {
|
|
158
|
-
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
159
|
-
};
|
|
160
|
-
agents: {
|
|
161
|
-
getConversations: () => Promise<import("./types.js").AgentConversation[]>;
|
|
162
|
-
getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
|
|
163
|
-
listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
|
|
164
|
-
createConversation: (conversation: {
|
|
165
|
-
agent_name: string;
|
|
166
|
-
metadata?: Record<string, any>;
|
|
167
|
-
}) => Promise<import("./types.js").AgentConversation>;
|
|
168
|
-
addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
|
|
169
|
-
subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
|
|
170
|
-
getWhatsAppConnectURL: (agentName: string) => string;
|
|
171
|
-
};
|
|
172
|
-
appLogs: {
|
|
173
|
-
logUserInApp(pageName: string): Promise<void>;
|
|
174
|
-
fetchLogs(params?: Record<string, any>): Promise<any>;
|
|
175
|
-
getStats(params?: Record<string, any>): Promise<any>;
|
|
176
|
-
};
|
|
177
|
-
cleanup: () => void;
|
|
178
|
-
};
|
|
179
|
-
entities: {};
|
|
180
|
-
integrations: {};
|
|
181
|
-
auth: {
|
|
182
|
-
me(): Promise<import("axios").AxiosResponse<any, any>>;
|
|
183
|
-
updateMe(data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
184
|
-
redirectToLogin(nextUrl: string): void;
|
|
185
|
-
logout(redirectUrl?: string): void;
|
|
186
|
-
setToken(token: string, saveToStorage?: boolean): void;
|
|
187
|
-
loginViaEmailPassword(email: string, password: string, turnstileToken?: string): Promise<{
|
|
188
|
-
access_token: string;
|
|
189
|
-
user: any;
|
|
190
|
-
}>;
|
|
191
|
-
isAuthenticated(): Promise<boolean>;
|
|
192
|
-
inviteUser(userEmail: string, role: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
193
|
-
register(payload: {
|
|
194
|
-
email: string;
|
|
195
|
-
password: string;
|
|
196
|
-
turnstile_token?: string | null;
|
|
197
|
-
referral_code?: string | null;
|
|
198
|
-
}): Promise<import("axios").AxiosResponse<any, any>>;
|
|
199
|
-
verifyOtp({ email, otpCode }: {
|
|
200
|
-
email: string;
|
|
201
|
-
otpCode: string;
|
|
202
|
-
}): Promise<import("axios").AxiosResponse<any, any>>;
|
|
203
|
-
resendOtp(email: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
204
|
-
resetPasswordRequest(email: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
205
|
-
resetPassword({ resetToken, newPassword, }: {
|
|
206
|
-
resetToken: string;
|
|
207
|
-
newPassword: string;
|
|
208
|
-
}): Promise<import("axios").AxiosResponse<any, any>>;
|
|
209
|
-
changePassword({ userId, currentPassword, newPassword, }: {
|
|
210
|
-
userId: string;
|
|
211
|
-
currentPassword: string;
|
|
212
|
-
newPassword: string;
|
|
213
|
-
}): Promise<import("axios").AxiosResponse<any, any>>;
|
|
214
|
-
};
|
|
215
|
-
functions: {
|
|
216
|
-
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
217
|
-
};
|
|
218
|
-
agents: {
|
|
219
|
-
getConversations: () => Promise<import("./types.js").AgentConversation[]>;
|
|
220
|
-
getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
|
|
221
|
-
listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
|
|
222
|
-
createConversation: (conversation: {
|
|
223
|
-
agent_name: string;
|
|
224
|
-
metadata?: Record<string, any>;
|
|
225
|
-
}) => Promise<import("./types.js").AgentConversation>;
|
|
226
|
-
addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
|
|
227
|
-
subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
|
|
228
|
-
getWhatsAppConnectURL: (agentName: string) => string;
|
|
229
|
-
};
|
|
230
|
-
appLogs: {
|
|
231
|
-
logUserInApp(pageName: string): Promise<void>;
|
|
232
|
-
fetchLogs(params?: Record<string, any>): Promise<any>;
|
|
233
|
-
getStats(params?: Record<string, any>): Promise<any>;
|
|
234
|
-
};
|
|
235
|
-
users: {
|
|
236
|
-
inviteUser(user_email: string, role: "user" | "admin"): Promise<any>;
|
|
237
|
-
};
|
|
238
|
-
cleanup: () => void;
|
|
239
|
-
};
|
|
38
|
+
export declare function createClient(config: CreateClientConfig): Base44Client;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a Base44 client from an HTTP request.
|
|
41
|
+
*
|
|
42
|
+
* The client is created by automatically extracting authentication tokens from a request to a backend function. Base44 inserts the necessary headers when forwarding requests to backend functions.
|
|
43
|
+
*
|
|
44
|
+
* To learn more about the Base44 client, see {@linkcode createClient | createClient()}.
|
|
45
|
+
*
|
|
46
|
+
* @param request - The incoming HTTP request object containing Base44 authentication headers.
|
|
47
|
+
* @returns A configured Base44 client instance with authentication from the incoming request.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* // User authentication in backend function
|
|
52
|
+
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
53
|
+
*
|
|
54
|
+
* Deno.serve(async (req) => {
|
|
55
|
+
* try {
|
|
56
|
+
* const base44 = createClientFromRequest(req);
|
|
57
|
+
* const user = await base44.auth.me();
|
|
58
|
+
*
|
|
59
|
+
* if (!user) {
|
|
60
|
+
* return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* // Access user's data
|
|
64
|
+
* const userOrders = await base44.entities.Orders.filter({ userId: user.id });
|
|
65
|
+
* return Response.json({ orders: userOrders });
|
|
66
|
+
* } catch (error) {
|
|
67
|
+
* return Response.json({ error: error.message }, { status: 500 });
|
|
68
|
+
* }
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Service role authentication in backend function
|
|
75
|
+
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
76
|
+
*
|
|
77
|
+
* Deno.serve(async (req) => {
|
|
78
|
+
* try {
|
|
79
|
+
* const base44 = createClientFromRequest(req);
|
|
80
|
+
*
|
|
81
|
+
* // Access admin data with service role permissions
|
|
82
|
+
* const recentOrders = await base44.asServiceRole.entities.Orders.list('-created_at', 50);
|
|
83
|
+
*
|
|
84
|
+
* return Response.json({ orders: recentOrders });
|
|
85
|
+
* } catch (error) {
|
|
86
|
+
* return Response.json({ error: error.message }, { status: 500 });
|
|
87
|
+
* }
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
*/
|
|
92
|
+
export declare function createClientFromRequest(request: Request): Base44Client;
|
package/dist/client.js
CHANGED
|
@@ -10,16 +10,41 @@ import { createAgentsModule } from "./modules/agents.js";
|
|
|
10
10
|
import { createAppLogsModule } from "./modules/app-logs.js";
|
|
11
11
|
import { createUsersModule } from "./modules/users.js";
|
|
12
12
|
import { RoomsSocket } from "./utils/socket-utils.js";
|
|
13
|
+
import { createAnalyticsModule } from "./modules/analytics.js";
|
|
13
14
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
15
|
+
* Creates a Base44 client.
|
|
16
|
+
*
|
|
17
|
+
* This is the main entry point for the Base44 SDK. It creates a client that provides access to the SDK's modules, such as {@linkcode EntitiesModule | entities}, {@linkcode AuthModule | auth}, and {@linkcode FunctionsModule | functions}.
|
|
18
|
+
*
|
|
19
|
+
* Typically, you don't need to call this function because Base44 creates the client for you. You can then import and use the client to make API calls. The client takes care of managing authentication for you.
|
|
20
|
+
*
|
|
21
|
+
* The client supports three authentication modes:
|
|
22
|
+
* - **Anonymous**: Access modules anonymously without authentication using `base44.moduleName`. Operations are scoped to public data and permissions.
|
|
23
|
+
* - **User authentication**: Access modules with user-level permissions using `base44.moduleName`. Operations are scoped to the authenticated user's data and permissions.
|
|
24
|
+
* - **Service role authentication**: Access modules with elevated permissions using `base44.asServiceRole.moduleName`. Operations can access any data available to the app's admin. Can only be used in the backend. Typically, you create a client with service role authentication using the {@linkcode createClientFromRequest | createClientFromRequest()} function in your backend functions.
|
|
25
|
+
*
|
|
26
|
+
* For example, when using the {@linkcode EntitiesModule | entities} module:
|
|
27
|
+
* - **Anonymous**: Can only read public data.
|
|
28
|
+
* - **User authentication**: Can access the current user's data.
|
|
29
|
+
* - **Service role authentication**: Can access all data that admins can access.
|
|
30
|
+
*
|
|
31
|
+
* Most modules are available in all three modes, but with different permission levels. However, some modules are only available in specific authentication modes.
|
|
32
|
+
*
|
|
33
|
+
* @param config - Configuration object for the client.
|
|
34
|
+
* @returns A configured Base44 client instance with access to all SDK modules.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* // Create a client for your app
|
|
39
|
+
* import { createClient } from '@base44/sdk';
|
|
40
|
+
*
|
|
41
|
+
* const base44 = createClient({
|
|
42
|
+
* appId: 'my-app-id'
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Use the client to access your data
|
|
46
|
+
* const products = await base44.entities.Products.list();
|
|
47
|
+
* ```
|
|
23
48
|
*/
|
|
24
49
|
export function createClient(config) {
|
|
25
50
|
const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, } = config;
|
|
@@ -74,13 +99,14 @@ export function createClient(config) {
|
|
|
74
99
|
token: serviceToken,
|
|
75
100
|
interceptResponses: false,
|
|
76
101
|
});
|
|
102
|
+
const userAuthModule = createAuthModule(axiosClient, functionsAxiosClient, appId, {
|
|
103
|
+
appBaseUrl,
|
|
104
|
+
serverUrl,
|
|
105
|
+
});
|
|
77
106
|
const userModules = {
|
|
78
107
|
entities: createEntitiesModule(axiosClient, appId),
|
|
79
108
|
integrations: createIntegrationsModule(axiosClient, appId),
|
|
80
|
-
auth:
|
|
81
|
-
appBaseUrl,
|
|
82
|
-
serverUrl,
|
|
83
|
-
}),
|
|
109
|
+
auth: userAuthModule,
|
|
84
110
|
functions: createFunctionsModule(functionsAxiosClient, appId),
|
|
85
111
|
agents: createAgentsModule({
|
|
86
112
|
axios: axiosClient,
|
|
@@ -91,7 +117,14 @@ export function createClient(config) {
|
|
|
91
117
|
}),
|
|
92
118
|
appLogs: createAppLogsModule(axiosClient, appId),
|
|
93
119
|
users: createUsersModule(axiosClient, appId),
|
|
120
|
+
analytics: createAnalyticsModule({
|
|
121
|
+
axiosClient,
|
|
122
|
+
serverUrl,
|
|
123
|
+
appId,
|
|
124
|
+
userAuthModule,
|
|
125
|
+
}),
|
|
94
126
|
cleanup: () => {
|
|
127
|
+
userModules.analytics.cleanup();
|
|
95
128
|
if (socket) {
|
|
96
129
|
socket.disconnect();
|
|
97
130
|
}
|
|
@@ -145,8 +178,19 @@ export function createClient(config) {
|
|
|
145
178
|
const client = {
|
|
146
179
|
...userModules,
|
|
147
180
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
181
|
+
* Sets a new authentication token for all subsequent requests.
|
|
182
|
+
*
|
|
183
|
+
* @param newToken - The new authentication token
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* // Update token after login
|
|
188
|
+
* const { access_token } = await base44.auth.loginViaEmailPassword(
|
|
189
|
+
* 'user@example.com',
|
|
190
|
+
* 'password'
|
|
191
|
+
* );
|
|
192
|
+
* base44.setToken(access_token);
|
|
193
|
+
* ```
|
|
150
194
|
*/
|
|
151
195
|
setToken(newToken) {
|
|
152
196
|
userModules.auth.setToken(newToken);
|
|
@@ -158,8 +202,9 @@ export function createClient(config) {
|
|
|
158
202
|
socketConfig.token = newToken;
|
|
159
203
|
},
|
|
160
204
|
/**
|
|
161
|
-
*
|
|
162
|
-
*
|
|
205
|
+
* Gets the current client configuration.
|
|
206
|
+
*
|
|
207
|
+
* @internal
|
|
163
208
|
*/
|
|
164
209
|
getConfig() {
|
|
165
210
|
return {
|
|
@@ -169,8 +214,22 @@ export function createClient(config) {
|
|
|
169
214
|
};
|
|
170
215
|
},
|
|
171
216
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
217
|
+
* Provides access to service role modules.
|
|
218
|
+
*
|
|
219
|
+
* Service role authentication provides elevated permissions for server-side operations. Unlike user authentication, which is scoped to a specific user's permissions, service role authentication has access to the data and operations available to the app's admin.
|
|
220
|
+
*
|
|
221
|
+
* @throws {Error} When accessed without providing a serviceToken during client creation.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```typescript
|
|
225
|
+
* const base44 = createClient({
|
|
226
|
+
* appId: 'my-app-id',
|
|
227
|
+
* serviceToken: 'service-role-token'
|
|
228
|
+
* });
|
|
229
|
+
*
|
|
230
|
+
* // Also access a module with elevated permissions
|
|
231
|
+
* const allUsers = await base44.asServiceRole.entities.User.list();
|
|
232
|
+
* ```
|
|
174
233
|
*/
|
|
175
234
|
get asServiceRole() {
|
|
176
235
|
if (!serviceToken) {
|
|
@@ -181,6 +240,59 @@ export function createClient(config) {
|
|
|
181
240
|
};
|
|
182
241
|
return client;
|
|
183
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Creates a Base44 client from an HTTP request.
|
|
245
|
+
*
|
|
246
|
+
* The client is created by automatically extracting authentication tokens from a request to a backend function. Base44 inserts the necessary headers when forwarding requests to backend functions.
|
|
247
|
+
*
|
|
248
|
+
* To learn more about the Base44 client, see {@linkcode createClient | createClient()}.
|
|
249
|
+
*
|
|
250
|
+
* @param request - The incoming HTTP request object containing Base44 authentication headers.
|
|
251
|
+
* @returns A configured Base44 client instance with authentication from the incoming request.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```typescript
|
|
255
|
+
* // User authentication in backend function
|
|
256
|
+
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
257
|
+
*
|
|
258
|
+
* Deno.serve(async (req) => {
|
|
259
|
+
* try {
|
|
260
|
+
* const base44 = createClientFromRequest(req);
|
|
261
|
+
* const user = await base44.auth.me();
|
|
262
|
+
*
|
|
263
|
+
* if (!user) {
|
|
264
|
+
* return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
|
265
|
+
* }
|
|
266
|
+
*
|
|
267
|
+
* // Access user's data
|
|
268
|
+
* const userOrders = await base44.entities.Orders.filter({ userId: user.id });
|
|
269
|
+
* return Response.json({ orders: userOrders });
|
|
270
|
+
* } catch (error) {
|
|
271
|
+
* return Response.json({ error: error.message }, { status: 500 });
|
|
272
|
+
* }
|
|
273
|
+
* });
|
|
274
|
+
* ```
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```typescript
|
|
278
|
+
* // Service role authentication in backend function
|
|
279
|
+
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
280
|
+
*
|
|
281
|
+
* Deno.serve(async (req) => {
|
|
282
|
+
* try {
|
|
283
|
+
* const base44 = createClientFromRequest(req);
|
|
284
|
+
*
|
|
285
|
+
* // Access admin data with service role permissions
|
|
286
|
+
* const recentOrders = await base44.asServiceRole.entities.Orders.list('-created_at', 50);
|
|
287
|
+
*
|
|
288
|
+
* return Response.json({ orders: recentOrders });
|
|
289
|
+
* } catch (error) {
|
|
290
|
+
* return Response.json({ error: error.message }, { status: 500 });
|
|
291
|
+
* }
|
|
292
|
+
* });
|
|
293
|
+
* ```
|
|
294
|
+
*
|
|
295
|
+
*/
|
|
184
296
|
export function createClientFromRequest(request) {
|
|
185
297
|
const authHeader = request.headers.get("Authorization");
|
|
186
298
|
const serviceRoleAuthHeader = request.headers.get("Base44-Service-Authorization");
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { EntitiesModule } from "./modules/entities.types.js";
|
|
2
|
+
import type { IntegrationsModule } from "./modules/integrations.types.js";
|
|
3
|
+
import type { AuthModule } from "./modules/auth.types.js";
|
|
4
|
+
import type { SsoModule } from "./modules/sso.types.js";
|
|
5
|
+
import type { ConnectorsModule } from "./modules/connectors.types.js";
|
|
6
|
+
import type { FunctionsModule } from "./modules/functions.types.js";
|
|
7
|
+
import type { AgentsModule } from "./modules/agents.types.js";
|
|
8
|
+
import type { AppLogsModule } from "./modules/app-logs.types.js";
|
|
9
|
+
import type { AnalyticsModule } from "./modules/analytics.types.js";
|
|
10
|
+
/**
|
|
11
|
+
* Options for creating a Base44 client.
|
|
12
|
+
*/
|
|
13
|
+
export interface CreateClientOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Optional error handler that will be called whenever an API error occurs.
|
|
16
|
+
*/
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Configuration for creating a Base44 client.
|
|
21
|
+
*/
|
|
22
|
+
export interface CreateClientConfig {
|
|
23
|
+
/**
|
|
24
|
+
* The Base44 server URL. Defaults to "https://base44.app".
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
serverUrl?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The base URL of the app, which is used for login redirects.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
appBaseUrl?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The Base44 app ID.
|
|
35
|
+
*
|
|
36
|
+
* You can find the `appId` in the browser URL when you're in the app editor.
|
|
37
|
+
* It's the string between `/apps/` and `/editor/`.
|
|
38
|
+
*/
|
|
39
|
+
appId: string;
|
|
40
|
+
/**
|
|
41
|
+
* User authentication token. Used to authenticate as a specific user.
|
|
42
|
+
*/
|
|
43
|
+
token?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Service role authentication token. Use this in the backend when you need elevated permissions to access data available to the app's admin or perform admin operations. This token should be kept secret and never exposed in the app's frontend. Typically, you get this token from a request to a backend function using {@linkcode createClientFromRequest | createClientFromRequest()}.
|
|
46
|
+
*/
|
|
47
|
+
serviceToken?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Whether authentication is required. If true, redirects to login if not authenticated.
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
requiresAuth?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Version string for functions API.
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
functionsVersion?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Additional headers to include in API requests.
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
headers?: Record<string, string>;
|
|
63
|
+
/**
|
|
64
|
+
* Additional client options.
|
|
65
|
+
*/
|
|
66
|
+
options?: CreateClientOptions;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The Base44 client instance.
|
|
70
|
+
*
|
|
71
|
+
* Provides access to all SDK modules for interacting with the app.
|
|
72
|
+
*/
|
|
73
|
+
export interface Base44Client {
|
|
74
|
+
/** {@link EntitiesModule | Entities module} for CRUD operations on your data models. */
|
|
75
|
+
entities: EntitiesModule;
|
|
76
|
+
/** {@link IntegrationsModule | Integrations module} for calling pre-built integration endpoints. */
|
|
77
|
+
integrations: IntegrationsModule;
|
|
78
|
+
/** {@link AuthModule | Auth module} for user authentication and management. */
|
|
79
|
+
auth: AuthModule;
|
|
80
|
+
/** {@link FunctionsModule | Functions module} for invoking custom backend functions. */
|
|
81
|
+
functions: FunctionsModule;
|
|
82
|
+
/** {@link AgentsModule | Agents module} for managing AI agent conversations. */
|
|
83
|
+
agents: AgentsModule;
|
|
84
|
+
/** {@link AppLogsModule | App logs module} for tracking app usage. */
|
|
85
|
+
appLogs: AppLogsModule;
|
|
86
|
+
/** {@link AnalyticsModule | Analytics module} for tracking app usage. */
|
|
87
|
+
analytics: AnalyticsModule;
|
|
88
|
+
/** Cleanup function to disconnect WebSocket connections. Call when you're done with the client. */
|
|
89
|
+
cleanup: () => void;
|
|
90
|
+
/**
|
|
91
|
+
* Sets a new authentication token for all subsequent requests.
|
|
92
|
+
*
|
|
93
|
+
* Updates the token for both HTTP requests and WebSocket connections.
|
|
94
|
+
*
|
|
95
|
+
* @param newToken - The new authentication token.
|
|
96
|
+
*/
|
|
97
|
+
setToken(newToken: string): void;
|
|
98
|
+
/**
|
|
99
|
+
* Gets the current client configuration.
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
getConfig(): {
|
|
103
|
+
serverUrl: string;
|
|
104
|
+
appId: string;
|
|
105
|
+
requiresAuth: boolean;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Provides access to supported modules with elevated permissions.
|
|
109
|
+
*
|
|
110
|
+
* Service role authentication provides elevated permissions for backend operations. Unlike user authentication, which is scoped to a specific user's permissions, service role authentication has access to the data and operations available to the app's admin.
|
|
111
|
+
*
|
|
112
|
+
* @throws {Error} When accessed without providing a serviceToken during client creation
|
|
113
|
+
*/
|
|
114
|
+
readonly asServiceRole: {
|
|
115
|
+
/** {@link EntitiesModule | Entities module} with elevated permissions. */
|
|
116
|
+
entities: EntitiesModule;
|
|
117
|
+
/** {@link IntegrationsModule | Integrations module} with elevated permissions. */
|
|
118
|
+
integrations: IntegrationsModule;
|
|
119
|
+
/** {@link SsoModule | SSO module} for generating SSO tokens.
|
|
120
|
+
* @internal
|
|
121
|
+
*/
|
|
122
|
+
sso: SsoModule;
|
|
123
|
+
/** {@link ConnectorsModule | Connectors module} for OAuth token retrieval. */
|
|
124
|
+
connectors: ConnectorsModule;
|
|
125
|
+
/** {@link FunctionsModule | Functions module} with elevated permissions. */
|
|
126
|
+
functions: FunctionsModule;
|
|
127
|
+
/** {@link AgentsModule | Agents module} with elevated permissions. */
|
|
128
|
+
agents: AgentsModule;
|
|
129
|
+
/** {@link AppLogsModule | App logs module} with elevated permissions. */
|
|
130
|
+
appLogs: AppLogsModule;
|
|
131
|
+
/** Cleanup function to disconnect WebSocket connections. */
|
|
132
|
+
cleanup: () => void;
|
|
133
|
+
};
|
|
134
|
+
}
|