@base44-preview/sdk 0.8.6-pr.52.d6f1bf2 → 0.8.6-pr.57.aa9ac21

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