@base44-preview/sdk 0.8.6-pr.56.6f7d483 → 0.8.6-pr.57.d5771ed

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