@base44-preview/sdk 0.8.5-pr.52.e60528e → 0.8.6-pr.48.d625f02
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +90 -237
- package/dist/client.js +141 -25
- package/dist/client.types.d.ts +131 -0
- package/dist/client.types.js +1 -0
- package/dist/index.d.ts +13 -5
- package/dist/index.js +2 -3
- package/dist/modules/agents.d.ts +2 -23
- package/dist/modules/agents.js +3 -1
- package/dist/modules/agents.types.d.ts +330 -34
- 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/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 +0 -1
- package/dist/utils/common.js +1 -2
- package/dist/utils/socket-utils.d.ts +2 -2
- package/package.json +12 -3
- package/dist/utils/app-params.d.ts +0 -11
- package/dist/utils/app-params.js +0 -43
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
|
@@ -11,15 +11,39 @@ 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
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
14
|
+
* Creates a Base44 client.
|
|
15
|
+
*
|
|
16
|
+
* 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}.
|
|
17
|
+
*
|
|
18
|
+
* 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.
|
|
19
|
+
*
|
|
20
|
+
* The client supports three authentication modes:
|
|
21
|
+
* - **Anonymous**: Access modules anonymously without authentication using `base44.moduleName`. Operations are scoped to public data and permissions.
|
|
22
|
+
* - **User authentication**: Access modules with user-level permissions using `base44.moduleName`. Operations are scoped to the authenticated user's data and permissions.
|
|
23
|
+
* - **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.
|
|
24
|
+
*
|
|
25
|
+
* For example, when using the {@linkcode EntitiesModule | entities} module:
|
|
26
|
+
* - **Anonymous**: Can only read public data.
|
|
27
|
+
* - **User authentication**: Can access the current user's data.
|
|
28
|
+
* - **Service role authentication**: Can access all data that admins can access.
|
|
29
|
+
*
|
|
30
|
+
* Most modules are available in all three modes, but with different permission levels. However, some modules are only available in specific authentication modes.
|
|
31
|
+
*
|
|
32
|
+
* @param config - Configuration object for the client.
|
|
33
|
+
* @returns A configured Base44 client instance with access to all SDK modules.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Create a client for your app
|
|
38
|
+
* import { createClient } from '@base44/sdk';
|
|
39
|
+
*
|
|
40
|
+
* const base44 = createClient({
|
|
41
|
+
* appId: 'my-app-id'
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* // Use the client to access your data
|
|
45
|
+
* const products = await base44.entities.Products.list();
|
|
46
|
+
* ```
|
|
23
47
|
*/
|
|
24
48
|
export function createClient(config) {
|
|
25
49
|
const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, } = config;
|
|
@@ -30,9 +54,15 @@ export function createClient(config) {
|
|
|
30
54
|
appId,
|
|
31
55
|
token,
|
|
32
56
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
57
|
+
let socket = null;
|
|
58
|
+
const getSocket = () => {
|
|
59
|
+
if (!socket) {
|
|
60
|
+
socket = RoomsSocket({
|
|
61
|
+
config: socketConfig,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return socket;
|
|
65
|
+
};
|
|
36
66
|
const headers = {
|
|
37
67
|
...optionalHeaders,
|
|
38
68
|
"X-App-Id": String(appId),
|
|
@@ -78,7 +108,7 @@ export function createClient(config) {
|
|
|
78
108
|
functions: createFunctionsModule(functionsAxiosClient, appId),
|
|
79
109
|
agents: createAgentsModule({
|
|
80
110
|
axios: axiosClient,
|
|
81
|
-
|
|
111
|
+
getSocket,
|
|
82
112
|
appId,
|
|
83
113
|
serverUrl,
|
|
84
114
|
token,
|
|
@@ -86,7 +116,9 @@ export function createClient(config) {
|
|
|
86
116
|
appLogs: createAppLogsModule(axiosClient, appId),
|
|
87
117
|
users: createUsersModule(axiosClient, appId),
|
|
88
118
|
cleanup: () => {
|
|
89
|
-
socket
|
|
119
|
+
if (socket) {
|
|
120
|
+
socket.disconnect();
|
|
121
|
+
}
|
|
90
122
|
},
|
|
91
123
|
};
|
|
92
124
|
const serviceRoleModules = {
|
|
@@ -97,14 +129,16 @@ export function createClient(config) {
|
|
|
97
129
|
functions: createFunctionsModule(serviceRoleFunctionsAxiosClient, appId),
|
|
98
130
|
agents: createAgentsModule({
|
|
99
131
|
axios: serviceRoleAxiosClient,
|
|
100
|
-
|
|
132
|
+
getSocket,
|
|
101
133
|
appId,
|
|
102
134
|
serverUrl,
|
|
103
135
|
token,
|
|
104
136
|
}),
|
|
105
137
|
appLogs: createAppLogsModule(serviceRoleAxiosClient, appId),
|
|
106
138
|
cleanup: () => {
|
|
107
|
-
socket
|
|
139
|
+
if (socket) {
|
|
140
|
+
socket.disconnect();
|
|
141
|
+
}
|
|
108
142
|
},
|
|
109
143
|
};
|
|
110
144
|
// Always try to get token from localStorage or URL parameters
|
|
@@ -135,18 +169,33 @@ export function createClient(config) {
|
|
|
135
169
|
const client = {
|
|
136
170
|
...userModules,
|
|
137
171
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
172
|
+
* Sets a new authentication token for all subsequent requests.
|
|
173
|
+
*
|
|
174
|
+
* @param newToken - The new authentication token
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* // Update token after login
|
|
179
|
+
* const { access_token } = await base44.auth.loginViaEmailPassword(
|
|
180
|
+
* 'user@example.com',
|
|
181
|
+
* 'password'
|
|
182
|
+
* );
|
|
183
|
+
* base44.setToken(access_token);
|
|
184
|
+
* ```
|
|
140
185
|
*/
|
|
141
186
|
setToken(newToken) {
|
|
142
187
|
userModules.auth.setToken(newToken);
|
|
143
|
-
socket
|
|
144
|
-
|
|
145
|
-
|
|
188
|
+
if (socket) {
|
|
189
|
+
socket.updateConfig({
|
|
190
|
+
token: newToken,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
socketConfig.token = newToken;
|
|
146
194
|
},
|
|
147
195
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
196
|
+
* Gets the current client configuration.
|
|
197
|
+
*
|
|
198
|
+
* @internal
|
|
150
199
|
*/
|
|
151
200
|
getConfig() {
|
|
152
201
|
return {
|
|
@@ -156,8 +205,22 @@ export function createClient(config) {
|
|
|
156
205
|
};
|
|
157
206
|
},
|
|
158
207
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
208
|
+
* Provides access to service role modules.
|
|
209
|
+
*
|
|
210
|
+
* 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.
|
|
211
|
+
*
|
|
212
|
+
* @throws {Error} When accessed without providing a serviceToken during client creation.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* const base44 = createClient({
|
|
217
|
+
* appId: 'my-app-id',
|
|
218
|
+
* serviceToken: 'service-role-token'
|
|
219
|
+
* });
|
|
220
|
+
*
|
|
221
|
+
* // Also access a module with elevated permissions
|
|
222
|
+
* const allUsers = await base44.asServiceRole.entities.User.list();
|
|
223
|
+
* ```
|
|
161
224
|
*/
|
|
162
225
|
get asServiceRole() {
|
|
163
226
|
if (!serviceToken) {
|
|
@@ -168,6 +231,59 @@ export function createClient(config) {
|
|
|
168
231
|
};
|
|
169
232
|
return client;
|
|
170
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Creates a Base44 client from an HTTP request.
|
|
236
|
+
*
|
|
237
|
+
* 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.
|
|
238
|
+
*
|
|
239
|
+
* To learn more about the Base44 client, see {@linkcode createClient | createClient()}.
|
|
240
|
+
*
|
|
241
|
+
* @param request - The incoming HTTP request object containing Base44 authentication headers.
|
|
242
|
+
* @returns A configured Base44 client instance with authentication from the incoming request.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```typescript
|
|
246
|
+
* // User authentication in backend function
|
|
247
|
+
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
248
|
+
*
|
|
249
|
+
* Deno.serve(async (req) => {
|
|
250
|
+
* try {
|
|
251
|
+
* const base44 = createClientFromRequest(req);
|
|
252
|
+
* const user = await base44.auth.me();
|
|
253
|
+
*
|
|
254
|
+
* if (!user) {
|
|
255
|
+
* return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
|
256
|
+
* }
|
|
257
|
+
*
|
|
258
|
+
* // Access user's data
|
|
259
|
+
* const userOrders = await base44.entities.Orders.filter({ userId: user.id });
|
|
260
|
+
* return Response.json({ orders: userOrders });
|
|
261
|
+
* } catch (error) {
|
|
262
|
+
* return Response.json({ error: error.message }, { status: 500 });
|
|
263
|
+
* }
|
|
264
|
+
* });
|
|
265
|
+
* ```
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* // Service role authentication in backend function
|
|
270
|
+
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
271
|
+
*
|
|
272
|
+
* Deno.serve(async (req) => {
|
|
273
|
+
* try {
|
|
274
|
+
* const base44 = createClientFromRequest(req);
|
|
275
|
+
*
|
|
276
|
+
* // Access admin data with service role permissions
|
|
277
|
+
* const recentOrders = await base44.asServiceRole.entities.Orders.list('-created_at', 50);
|
|
278
|
+
*
|
|
279
|
+
* return Response.json({ orders: recentOrders });
|
|
280
|
+
* } catch (error) {
|
|
281
|
+
* return Response.json({ error: error.message }, { status: 500 });
|
|
282
|
+
* }
|
|
283
|
+
* });
|
|
284
|
+
* ```
|
|
285
|
+
*
|
|
286
|
+
*/
|
|
171
287
|
export function createClientFromRequest(request) {
|
|
172
288
|
const authHeader = request.headers.get("Authorization");
|
|
173
289
|
const serviceRoleAuthHeader = request.headers.get("Base44-Service-Authorization");
|