@base44-preview/sdk 0.8.18-pr.91.a4c0940 → 0.8.18-pr.91.cfad1b0

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 CHANGED
@@ -9,31 +9,32 @@ You can use it in two ways:
9
9
 
10
10
  ## Installation
11
11
 
12
- Install the SDK via npm:
12
+ **Inside Base44 apps**: The SDK is already available. No installation needed.
13
+
14
+ **External apps**: Install the SDK via npm:
13
15
 
14
16
  ```bash
15
17
  npm install @base44/sdk
16
18
  ```
17
19
 
18
- > **Note**: In Base44-generated apps, the SDK is already installed for you.
19
-
20
20
  ## Modules
21
21
 
22
22
  The SDK provides access to Base44's functionality through the following modules:
23
23
 
24
24
  - **[`agents`](https://docs.base44.com/developers/references/sdk/docs/interfaces/agents)**: Interact with AI agents and manage conversations.
25
+ - **[`analytics`](https://docs.base44.com/developers/references/sdk/docs/interfaces/analytics)**: Track custom events in your app.
25
26
  - **[`app-logs`](https://docs.base44.com/developers/references/sdk/docs/interfaces/app-logs)**: Access and query app logs.
26
27
  - **[`auth`](https://docs.base44.com/developers/references/sdk/docs/interfaces/auth)**: Manage user authentication, registration, and session handling.
27
28
  - **[`connectors`](https://docs.base44.com/developers/references/sdk/docs/interfaces/connectors)**: Manage OAuth connections and access tokens for third-party services.
28
29
  - **[`entities`](https://docs.base44.com/developers/references/sdk/docs/interfaces/entities)**: Work with your app's data entities using CRUD operations.
29
30
  - **[`functions`](https://docs.base44.com/developers/references/sdk/docs/interfaces/functions)**: Execute backend functions.
30
- - **[`integrations`](https://docs.base44.com/developers/references/sdk/docs/type-aliases/integrations)**: Pre-built integrations for external services.
31
+ - **[`integrations`](https://docs.base44.com/developers/references/sdk/docs/type-aliases/integrations)**: Access pre-built and third-party integrations.
31
32
 
32
- ## Quick starts
33
+ ## Quickstarts
33
34
 
34
- How you get started depends on your context:
35
+ How you get started depends on whether you're working inside a Base44-generated app or building your own.
35
36
 
36
- ### Inside a Base44 app
37
+ ### Inside Base44 apps
37
38
 
38
39
  In Base44-generated apps, the client is pre-configured. Just import and use it:
39
40
 
@@ -58,32 +59,32 @@ const tasks = await base44.entities.Task.list();
58
59
 
59
60
  ### External apps
60
61
 
61
- When using Base44 as a backend for your own app, create and configure the client yourself:
62
+ When using Base44 as a backend for your own app, install the SDK and create the client yourself:
62
63
 
63
64
  ```typescript
64
- import { createClient } from '@base44/sdk';
65
+ import { createClient } from "@base44/sdk";
65
66
 
66
67
  // Create a client for your Base44 app
67
68
  const base44 = createClient({
68
- appId: 'your-app-id' // Find this in the Base44 editor URL
69
+ appId: "your-app-id", // Find this in the Base44 editor URL
69
70
  });
70
71
 
71
- // Read public data (anonymous access)
72
+ // Read public data
72
73
  const products = await base44.entities.Products.list();
73
74
 
74
75
  // Authenticate a user (token is automatically set)
75
- await base44.auth.loginViaEmailPassword('user@example.com', 'password');
76
+ await base44.auth.loginViaEmailPassword("user@example.com", "password");
76
77
 
77
- // Now operations use the authenticated user's permissions
78
+ // Access user's data
78
79
  const userOrders = await base44.entities.Orders.list();
79
80
  ```
80
81
 
81
82
  ### Service role
82
83
 
83
- For backend code that needs admin-level access, use the service role. Service role is only available in Base44-hosted backend functions:
84
+ By default, the client operates with user-level permissions, limiting access to what the current user can see and do. The service role provides elevated permissions for backend operations and is only available in Base44-hosted backend functions. External backends can't use service role permissions.
84
85
 
85
86
  ```typescript
86
- import { createClientFromRequest } from 'npm:@base44/sdk';
87
+ import { createClientFromRequest } from "npm:@base44/sdk";
87
88
 
88
89
  Deno.serve(async (req) => {
89
90
  const base44 = createClientFromRequest(req);
@@ -97,7 +98,15 @@ Deno.serve(async (req) => {
97
98
 
98
99
  ## Learn more
99
100
 
100
- For complete documentation, guides, and API reference, visit the **[Base44 SDK Documentation](https://docs.base44.com/developers/landing)**.
101
+ The best way to get started with the JavaScript SDK is to have Base44 build an app for you. Once you have an app, you can explore the generated code and experiment with the SDK to see how it works in practice. You can also ask Base44 to demonstrate specific features of the SDK.
102
+
103
+ For a deeper understanding, check out these guides:
104
+
105
+ 1. [Base44 client](https://docs.base44.com/developers/references/sdk/getting-started/client) - Work with the client in frontend, backend, and external app contexts.
106
+ 2. [Work with data](https://docs.base44.com/developers/references/sdk/getting-started/work-with-data) - Create, read, update, and delete data.
107
+ 3. [Common SDK patterns](https://docs.base44.com/developers/references/sdk/getting-started/work-with-sdk) - Authentication, integrations, functions, and error handling.
108
+
109
+ For the complete documentation and API reference, visit the **[Base44 Developer Docs](https://docs.base44.com/developers/home)**.
101
110
 
102
111
  ## Development
103
112
 
package/dist/client.js CHANGED
@@ -50,6 +50,8 @@ import { createAnalyticsModule } from "./modules/analytics.js";
50
50
  */
51
51
  export function createClient(config) {
52
52
  const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, } = config;
53
+ // Normalize appBaseUrl to always be a string (empty if not provided or invalid)
54
+ const normalizedAppBaseUrl = typeof appBaseUrl === "string" ? appBaseUrl : "";
53
55
  const socketConfig = {
54
56
  serverUrl,
55
57
  mountPath: "/ws-user-apps/socket.io/",
@@ -102,7 +104,7 @@ export function createClient(config) {
102
104
  interceptResponses: false,
103
105
  });
104
106
  const userAuthModule = createAuthModule(axiosClient, functionsAxiosClient, appId, {
105
- appBaseUrl,
107
+ appBaseUrl: normalizedAppBaseUrl,
106
108
  serverUrl,
107
109
  });
108
110
  const userModules = {
@@ -20,7 +20,6 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
20
20
  },
21
21
  // Redirects the user to the app's login page
22
22
  redirectToLogin(nextUrl) {
23
- var _a;
24
23
  // This function only works in a browser environment
25
24
  if (typeof window === "undefined") {
26
25
  throw new Error("Login method can only be used in a browser environment");
@@ -30,7 +29,7 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
30
29
  ? new URL(nextUrl, window.location.origin).toString()
31
30
  : window.location.href;
32
31
  // Build the login URL
33
- const loginUrl = `${(_a = options.appBaseUrl) !== null && _a !== void 0 ? _a : ""}/login?from_url=${encodeURIComponent(redirectUrl)}`;
32
+ const loginUrl = `${options.appBaseUrl}/login?from_url=${encodeURIComponent(redirectUrl)}`;
34
33
  // Redirect to the login page
35
34
  window.location.href = loginUrl;
36
35
  },
@@ -64,7 +63,7 @@ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
64
63
  // Determine the from_url parameter
65
64
  const fromUrl = redirectUrl || window.location.href;
66
65
  // Redirect to server-side logout endpoint to clear HTTP-only cookies
67
- const logoutUrl = `${options.appBaseUrl}/api/apps/${appId}/auth/logout?from_url=${encodeURIComponent(fromUrl)}`;
66
+ const logoutUrl = `${options.appBaseUrl}/api/apps/auth/logout?from_url=${encodeURIComponent(fromUrl)}`;
68
67
  window.location.href = logoutUrl;
69
68
  }
70
69
  },
@@ -90,8 +90,8 @@ export interface ResetPasswordParams {
90
90
  export interface AuthModuleOptions {
91
91
  /** Server URL for API requests. */
92
92
  serverUrl: string;
93
- /** Optional base URL for the app (used for login redirects). */
94
- appBaseUrl?: string;
93
+ /** Base URL for the app (used for login redirects). */
94
+ appBaseUrl: string;
95
95
  }
96
96
  /**
97
97
  * Authentication module for managing user authentication and authorization. The module automatically stores tokens in local storage when available and manages authorization headers for API requests.
@@ -4,12 +4,14 @@
4
4
  export type RealtimeEventType = "create" | "update" | "delete";
5
5
  /**
6
6
  * Payload received when a realtime event occurs.
7
+ *
8
+ * @typeParam T - The entity type for the data field. Defaults to `any`.
7
9
  */
8
- export interface RealtimeEvent {
10
+ export interface RealtimeEvent<T = any> {
9
11
  /** The type of change that occurred */
10
12
  type: RealtimeEventType;
11
13
  /** The entity data */
12
- data: any;
14
+ data: T;
13
15
  /** The unique identifier of the affected entity */
14
16
  id: string;
15
17
  /** ISO 8601 timestamp of when the event occurred */
@@ -17,14 +19,65 @@ export interface RealtimeEvent {
17
19
  }
18
20
  /**
19
21
  * Callback function invoked when a realtime event occurs.
22
+ *
23
+ * @typeParam T - The entity type for the event data. Defaults to `any`.
24
+ */
25
+ export type RealtimeCallback<T = any> = (event: RealtimeEvent<T>) => void;
26
+ /**
27
+ * Result returned when deleting a single entity.
28
+ */
29
+ export interface DeleteResult {
30
+ /** Whether the deletion was successful */
31
+ success: boolean;
32
+ }
33
+ /**
34
+ * Result returned when deleting multiple entities.
35
+ */
36
+ export interface DeleteManyResult {
37
+ /** Whether the deletion was successful */
38
+ success: boolean;
39
+ /** Number of entities that were deleted */
40
+ deleted: number;
41
+ }
42
+ /**
43
+ * Result returned when importing entities from a file.
44
+ *
45
+ * @typeParam T - The entity type for imported records. Defaults to `any`.
20
46
  */
21
- export type RealtimeCallback = (event: RealtimeEvent) => void;
47
+ export interface ImportResult<T = any> {
48
+ /** Status of the import operation */
49
+ status: "success" | "error";
50
+ /** Details message, e.g., "Successfully imported 3 entities with RLS enforcement" */
51
+ details: string | null;
52
+ /** Array of created entity objects when successful, or null on error */
53
+ output: T[] | null;
54
+ }
55
+ /**
56
+ * Sort field type for entity queries.
57
+ *
58
+ * Supports ascending (no prefix or `'+'`) and descending (`'-'`) sorting.
59
+ *
60
+ * @typeParam T - The entity type to derive sortable fields from.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * // Ascending sort (default)
65
+ * 'created_date'
66
+ * '+created_date'
67
+ *
68
+ * // Descending sort
69
+ * '-created_date'
70
+ * ```
71
+ */
72
+ export type SortField<T> = (keyof T & string) | `+${keyof T & string}` | `-${keyof T & string}`;
22
73
  /**
23
74
  * Entity handler providing CRUD operations for a specific entity type.
24
75
  *
25
76
  * Each entity in the app gets a handler with these methods for managing data.
77
+ *
78
+ * @typeParam T - The entity type. Defaults to `any` for backward compatibility.
26
79
  */
27
- export interface EntityHandler {
80
+ export interface EntityHandler<T = any> {
28
81
  /**
29
82
  * Lists records with optional pagination and sorting.
30
83
  *
@@ -33,11 +86,12 @@ export interface EntityHandler {
33
86
  *
34
87
  * **Note:** The maximum limit is 5,000 items per request.
35
88
  *
89
+ * @typeParam K - The fields to include in the response. Defaults to all fields.
36
90
  * @param sort - Sort parameter, such as `'-created_date'` for descending. Defaults to `'-created_date'`.
37
91
  * @param limit - Maximum number of results to return. Defaults to `50`.
38
92
  * @param skip - Number of results to skip for pagination. Defaults to `0`.
39
93
  * @param fields - Array of field names to include in the response. Defaults to all fields.
40
- * @returns Promise resolving to an array of records.
94
+ * @returns Promise resolving to an array of records with selected fields.
41
95
  *
42
96
  * @example
43
97
  * ```typescript
@@ -64,7 +118,7 @@ export interface EntityHandler {
64
118
  * const fields = await base44.entities.MyEntity.list('-created_date', 10, 0, ['name', 'status']);
65
119
  * ```
66
120
  */
67
- list(sort?: string, limit?: number, skip?: number, fields?: string[]): Promise<any>;
121
+ list<K extends keyof T = keyof T>(sort?: SortField<T>, limit?: number, skip?: number, fields?: K[]): Promise<Pick<T, K>[]>;
68
122
  /**
69
123
  * Filters records based on a query.
70
124
  *
@@ -73,6 +127,7 @@ export interface EntityHandler {
73
127
  *
74
128
  * **Note:** The maximum limit is 5,000 items per request.
75
129
  *
130
+ * @typeParam K - The fields to include in the response. Defaults to all fields.
76
131
  * @param query - Query object with field-value pairs. Each key should be a field name
77
132
  * from your entity schema, and each value is the criteria to match. Records matching all
78
133
  * specified criteria are returned. Field names are case-sensitive.
@@ -80,7 +135,7 @@ export interface EntityHandler {
80
135
  * @param limit - Maximum number of results to return. Defaults to `50`.
81
136
  * @param skip - Number of results to skip for pagination. Defaults to `0`.
82
137
  * @param fields - Array of field names to include in the response. Defaults to all fields.
83
- * @returns Promise resolving to an array of filtered records.
138
+ * @returns Promise resolving to an array of filtered records with selected fields.
84
139
  *
85
140
  * @example
86
141
  * ```typescript
@@ -122,7 +177,7 @@ export interface EntityHandler {
122
177
  * );
123
178
  * ```
124
179
  */
125
- filter(query: Record<string, any>, sort?: string, limit?: number, skip?: number, fields?: string[]): Promise<any>;
180
+ filter<K extends keyof T = keyof T>(query: Partial<T>, sort?: SortField<T>, limit?: number, skip?: number, fields?: K[]): Promise<Pick<T, K>[]>;
126
181
  /**
127
182
  * Gets a single record by ID.
128
183
  *
@@ -138,7 +193,7 @@ export interface EntityHandler {
138
193
  * console.log(record.name);
139
194
  * ```
140
195
  */
141
- get(id: string): Promise<any>;
196
+ get(id: string): Promise<T>;
142
197
  /**
143
198
  * Creates a new record.
144
199
  *
@@ -158,7 +213,7 @@ export interface EntityHandler {
158
213
  * console.log('Created record with ID:', newRecord.id);
159
214
  * ```
160
215
  */
161
- create(data: Record<string, any>): Promise<any>;
216
+ create(data: Partial<T>): Promise<T>;
162
217
  /**
163
218
  * Updates an existing record.
164
219
  *
@@ -187,7 +242,7 @@ export interface EntityHandler {
187
242
  * });
188
243
  * ```
189
244
  */
190
- update(id: string, data: Record<string, any>): Promise<any>;
245
+ update(id: string, data: Partial<T>): Promise<T>;
191
246
  /**
192
247
  * Deletes a single record by ID.
193
248
  *
@@ -200,10 +255,10 @@ export interface EntityHandler {
200
255
  * ```typescript
201
256
  * // Delete a record
202
257
  * const result = await base44.entities.MyEntity.delete('entity-123');
203
- * console.log('Deleted:', result);
258
+ * console.log('Deleted:', result.success);
204
259
  * ```
205
260
  */
206
- delete(id: string): Promise<any>;
261
+ delete(id: string): Promise<DeleteResult>;
207
262
  /**
208
263
  * Deletes multiple records matching a query.
209
264
  *
@@ -224,7 +279,7 @@ export interface EntityHandler {
224
279
  * console.log('Deleted:', result);
225
280
  * ```
226
281
  */
227
- deleteMany(query: Record<string, any>): Promise<any>;
282
+ deleteMany(query: Partial<T>): Promise<DeleteManyResult>;
228
283
  /**
229
284
  * Creates multiple records in a single request.
230
285
  *
@@ -244,7 +299,7 @@ export interface EntityHandler {
244
299
  * ]);
245
300
  * ```
246
301
  */
247
- bulkCreate(data: Record<string, any>[]): Promise<any>;
302
+ bulkCreate(data: Partial<T>[]): Promise<T[]>;
248
303
  /**
249
304
  * Imports records from a file.
250
305
  *
@@ -252,7 +307,7 @@ export interface EntityHandler {
252
307
  * The file format should match your entity structure. Requires a browser environment and can't be used in the backend.
253
308
  *
254
309
  * @param file - File object to import.
255
- * @returns Promise resolving to the import result.
310
+ * @returns Promise resolving to the import result containing status, details, and created records.
256
311
  *
257
312
  * @example
258
313
  * ```typescript
@@ -261,12 +316,14 @@ export interface EntityHandler {
261
316
  * const file = event.target.files?.[0];
262
317
  * if (file) {
263
318
  * const result = await base44.entities.MyEntity.importEntities(file);
264
- * console.log(`Imported ${result.count} records`);
319
+ * if (result.status === 'success' && result.output) {
320
+ * console.log(`Imported ${result.output.length} records`);
321
+ * }
265
322
  * }
266
323
  * };
267
324
  * ```
268
325
  */
269
- importEntities(file: File): Promise<any>;
326
+ importEntities(file: File): Promise<ImportResult<T>>;
270
327
  /**
271
328
  * Subscribes to realtime updates for all records of this entity type.
272
329
  *
@@ -292,7 +349,7 @@ export interface EntityHandler {
292
349
  * unsubscribe();
293
350
  * ```
294
351
  */
295
- subscribe(callback: RealtimeCallback): () => void;
352
+ subscribe(callback: RealtimeCallback<T>): () => void;
296
353
  }
297
354
  /**
298
355
  * Entities module for managing app data.
@@ -340,5 +397,5 @@ export interface EntitiesModule {
340
397
  * base44.entities.AnotherEntity
341
398
  * ```
342
399
  */
343
- [entityName: string]: EntityHandler;
400
+ [entityName: string]: EntityHandler<any>;
344
401
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.18-pr.91.a4c0940",
3
+ "version": "0.8.18-pr.91.cfad1b0",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",