@base44-preview/sdk 0.7.0-pr.27.eb342e0 → 0.7.0-pr.28.d052301

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 CHANGED
@@ -1,6 +1,3 @@
1
- export type CreateClientOptions = {
2
- onError?: (error: Error) => void;
3
- };
4
1
  /**
5
2
  * Create a Base44 client instance
6
3
  * @param {Object} config - Client configuration
@@ -17,7 +14,6 @@ export declare function createClient(config: {
17
14
  token?: string;
18
15
  serviceToken?: string;
19
16
  requiresAuth?: boolean;
20
- options?: CreateClientOptions;
21
17
  }): {
22
18
  /**
23
19
  * Set authentication token for all requests
@@ -42,15 +38,6 @@ export declare function createClient(config: {
42
38
  functions: {
43
39
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
44
40
  };
45
- agents: {
46
- getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
47
- getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
48
- listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
49
- createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
50
- addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
51
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
52
- };
53
- cleanup: () => void;
54
41
  };
55
42
  entities: {};
56
43
  integrations: {};
@@ -60,7 +47,10 @@ export declare function createClient(config: {
60
47
  redirectToLogin(nextUrl: string): void;
61
48
  logout(redirectUrl?: string): void;
62
49
  setToken(token: string, saveToStorage?: boolean): void;
63
- loginViaEmailPassword(email: string, password: string, turnstileToken?: string): Promise<{
50
+ loginViaEmailPassword(email: string, password: string, options?: {
51
+ turnstileToken?: string;
52
+ fromUrl?: string;
53
+ }): Promise<{
64
54
  access_token: string;
65
55
  user: any;
66
56
  }>;
@@ -69,15 +59,6 @@ export declare function createClient(config: {
69
59
  functions: {
70
60
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
71
61
  };
72
- agents: {
73
- getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
74
- getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
75
- listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
76
- createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
77
- addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
78
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
79
- };
80
- cleanup: () => void;
81
62
  };
82
63
  export declare function createClientFromRequest(request: Request): {
83
64
  /**
@@ -103,15 +84,6 @@ export declare function createClientFromRequest(request: Request): {
103
84
  functions: {
104
85
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
105
86
  };
106
- agents: {
107
- getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
108
- getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
109
- listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
110
- createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
111
- addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
112
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
113
- };
114
- cleanup: () => void;
115
87
  };
116
88
  entities: {};
117
89
  integrations: {};
@@ -121,7 +93,10 @@ export declare function createClientFromRequest(request: Request): {
121
93
  redirectToLogin(nextUrl: string): void;
122
94
  logout(redirectUrl?: string): void;
123
95
  setToken(token: string, saveToStorage?: boolean): void;
124
- loginViaEmailPassword(email: string, password: string, turnstileToken?: string): Promise<{
96
+ loginViaEmailPassword(email: string, password: string, options?: {
97
+ turnstileToken?: string;
98
+ fromUrl?: string;
99
+ }): Promise<{
125
100
  access_token: string;
126
101
  user: any;
127
102
  }>;
@@ -130,13 +105,4 @@ export declare function createClientFromRequest(request: Request): {
130
105
  functions: {
131
106
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
132
107
  };
133
- agents: {
134
- getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
135
- getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
136
- listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
137
- createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
138
- addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
139
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
140
- };
141
- cleanup: () => void;
142
108
  };
package/dist/client.js CHANGED
@@ -5,8 +5,6 @@ import { createAuthModule } from "./modules/auth.js";
5
5
  import { createSsoModule } from "./modules/sso.js";
6
6
  import { getAccessToken } from "./utils/auth-utils.js";
7
7
  import { createFunctionsModule } from "./modules/functions.js";
8
- import { createAgentsModule } from "./modules/agents.js";
9
- import { RoomsSocket } from "./utils/socket-utils.js";
10
8
  /**
11
9
  * Create a Base44 client instance
12
10
  * @param {Object} config - Client configuration
@@ -18,17 +16,7 @@ import { RoomsSocket } from "./utils/socket-utils.js";
18
16
  * @returns {Object} Base44 client instance
19
17
  */
20
18
  export function createClient(config) {
21
- const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, options, } = config;
22
- const socketConfig = {
23
- serverUrl,
24
- mountPath: "/ws-user-apps/socket.io/",
25
- transports: ["websocket"],
26
- appId,
27
- token,
28
- };
29
- const socket = RoomsSocket({
30
- config: socketConfig,
31
- });
19
+ const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, } = config;
32
20
  const axiosClient = createAxiosClient({
33
21
  baseURL: `${serverUrl}/api`,
34
22
  headers: {
@@ -38,7 +26,6 @@ export function createClient(config) {
38
26
  requiresAuth,
39
27
  appId,
40
28
  serverUrl,
41
- onError: options === null || options === void 0 ? void 0 : options.onError,
42
29
  });
43
30
  const functionsAxiosClient = createAxiosClient({
44
31
  baseURL: `${serverUrl}/api`,
@@ -50,7 +37,6 @@ export function createClient(config) {
50
37
  appId,
51
38
  serverUrl,
52
39
  interceptResponses: false,
53
- onError: options === null || options === void 0 ? void 0 : options.onError,
54
40
  });
55
41
  const serviceRoleAxiosClient = createAxiosClient({
56
42
  baseURL: `${serverUrl}/api`,
@@ -60,7 +46,6 @@ export function createClient(config) {
60
46
  token: serviceToken,
61
47
  serverUrl,
62
48
  appId,
63
- onError: options === null || options === void 0 ? void 0 : options.onError,
64
49
  });
65
50
  const serviceRoleFunctionsAxiosClient = createAxiosClient({
66
51
  baseURL: `${serverUrl}/api`,
@@ -77,28 +62,12 @@ export function createClient(config) {
77
62
  integrations: createIntegrationsModule(axiosClient, appId),
78
63
  auth: createAuthModule(axiosClient, functionsAxiosClient, appId),
79
64
  functions: createFunctionsModule(functionsAxiosClient, appId),
80
- agents: createAgentsModule({
81
- axios: axiosClient,
82
- socket,
83
- appId,
84
- }),
85
- cleanup: () => {
86
- socket.disconnect();
87
- },
88
65
  };
89
66
  const serviceRoleModules = {
90
67
  entities: createEntitiesModule(serviceRoleAxiosClient, appId),
91
68
  integrations: createIntegrationsModule(serviceRoleAxiosClient, appId),
92
69
  sso: createSsoModule(serviceRoleAxiosClient, appId, token),
93
70
  functions: createFunctionsModule(serviceRoleFunctionsAxiosClient, appId),
94
- agents: createAgentsModule({
95
- axios: serviceRoleAxiosClient,
96
- socket,
97
- appId,
98
- }),
99
- cleanup: () => {
100
- socket.disconnect();
101
- },
102
71
  };
103
72
  // Always try to get token from localStorage or URL parameters
104
73
  if (typeof window !== "undefined") {
@@ -133,9 +102,6 @@ export function createClient(config) {
133
102
  */
134
103
  setToken(newToken) {
135
104
  userModules.auth.setToken(newToken);
136
- socket.updateConfig({
137
- token: newToken,
138
- });
139
105
  },
140
106
  /**
141
107
  * Get current configuration
@@ -154,10 +120,10 @@ export function createClient(config) {
154
120
  */
155
121
  get asServiceRole() {
156
122
  if (!serviceToken) {
157
- throw new Error("Service token is required to use asServiceRole. Please provide a serviceToken when creating the client.");
123
+ throw new Error('Service token is required to use asServiceRole. Please provide a serviceToken when creating the client.');
158
124
  }
159
125
  return serviceRoleModules;
160
- },
126
+ }
161
127
  };
162
128
  return client;
163
129
  }
@@ -173,20 +139,16 @@ export function createClientFromRequest(request) {
173
139
  let serviceRoleToken;
174
140
  let userToken;
175
141
  if (serviceRoleAuthHeader !== null) {
176
- if (serviceRoleAuthHeader === "" ||
177
- !serviceRoleAuthHeader.startsWith("Bearer ") ||
178
- serviceRoleAuthHeader.split(" ").length !== 2) {
142
+ if (serviceRoleAuthHeader === '' || !serviceRoleAuthHeader.startsWith('Bearer ') || serviceRoleAuthHeader.split(' ').length !== 2) {
179
143
  throw new Error('Invalid authorization header format. Expected "Bearer <token>"');
180
144
  }
181
- serviceRoleToken = serviceRoleAuthHeader.split(" ")[1];
145
+ serviceRoleToken = serviceRoleAuthHeader.split(' ')[1];
182
146
  }
183
147
  if (authHeader !== null) {
184
- if (authHeader === "" ||
185
- !authHeader.startsWith("Bearer ") ||
186
- authHeader.split(" ").length !== 2) {
148
+ if (authHeader === '' || !authHeader.startsWith('Bearer ') || authHeader.split(' ').length !== 2) {
187
149
  throw new Error('Invalid authorization header format. Expected "Bearer <token>"');
188
150
  }
189
- userToken = authHeader.split(" ")[1];
151
+ userToken = authHeader.split(' ')[1];
190
152
  }
191
153
  return createClient({
192
154
  serverUrl: serverUrlHeader || "https://base44.app",
@@ -41,10 +41,15 @@ export declare function createAuthModule(axios: AxiosInstance, functionsAxiosCli
41
41
  * Login via username and password
42
42
  * @param email - User email
43
43
  * @param password - User password
44
- * @param turnstileToken - Optional Turnstile captcha token
44
+ * @param options - Optional login options
45
+ * @param options.turnstileToken - Optional Turnstile captcha token
46
+ * @param options.fromUrl - Optional URL to redirect to after login
45
47
  * @returns Login response with access_token and user
46
48
  */
47
- loginViaEmailPassword(email: string, password: string, turnstileToken?: string): Promise<{
49
+ loginViaEmailPassword(email: string, password: string, options?: {
50
+ turnstileToken?: string;
51
+ fromUrl?: string;
52
+ }): Promise<{
48
53
  access_token: string;
49
54
  user: any;
50
55
  }>;
@@ -77,7 +77,6 @@ export function createAuthModule(axios, functionsAxiosClient, appId) {
77
77
  setToken(token, saveToStorage = true) {
78
78
  if (!token)
79
79
  return;
80
- // handle token change for axios clients
81
80
  axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
82
81
  functionsAxiosClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
83
82
  // Save token to localStorage if requested
@@ -96,16 +95,19 @@ export function createAuthModule(axios, functionsAxiosClient, appId) {
96
95
  * Login via username and password
97
96
  * @param email - User email
98
97
  * @param password - User password
99
- * @param turnstileToken - Optional Turnstile captcha token
98
+ * @param options - Optional login options
99
+ * @param options.turnstileToken - Optional Turnstile captcha token
100
+ * @param options.fromUrl - Optional URL to redirect to after login
100
101
  * @returns Login response with access_token and user
101
102
  */
102
- async loginViaEmailPassword(email, password, turnstileToken) {
103
+ async loginViaEmailPassword(email, password, options) {
103
104
  var _a;
104
105
  try {
105
106
  const response = await axios.post(`/apps/${appId}/auth/login`, {
106
107
  email,
107
108
  password,
108
- ...(turnstileToken && { turnstile_token: turnstileToken }),
109
+ ...((options === null || options === void 0 ? void 0 : options.turnstileToken) && { turnstile_token: options.turnstileToken }),
110
+ ...((options === null || options === void 0 ? void 0 : options.fromUrl) && { from_url: options.fromUrl }),
109
111
  });
110
112
  const { access_token, user } = response;
111
113
  if (access_token) {
@@ -23,7 +23,7 @@ export declare class Base44Error extends Error {
23
23
  * @param {string} options.serverUrl - Server URL (needed for login redirect)
24
24
  * @returns {import('axios').AxiosInstance} Configured axios instance
25
25
  */
26
- export declare function createAxiosClient({ baseURL, headers, token, requiresAuth, appId, serverUrl, interceptResponses, onError, }: {
26
+ export declare function createAxiosClient({ baseURL, headers, token, requiresAuth, appId, serverUrl, interceptResponses, }: {
27
27
  baseURL: string;
28
28
  headers?: Record<string, string>;
29
29
  token?: string;
@@ -31,5 +31,4 @@ export declare function createAxiosClient({ baseURL, headers, token, requiresAut
31
31
  appId: string;
32
32
  serverUrl: string;
33
33
  interceptResponses?: boolean;
34
- onError?: (error: Error) => void;
35
34
  }): import("axios").AxiosInstance;
@@ -64,7 +64,7 @@ function redirectToLogin(serverUrl, appId) {
64
64
  * @param {string} options.serverUrl - Server URL (needed for login redirect)
65
65
  * @returns {import('axios').AxiosInstance} Configured axios instance
66
66
  */
67
- export function createAxiosClient({ baseURL, headers = {}, token, requiresAuth = false, appId, serverUrl, interceptResponses = true, onError, }) {
67
+ export function createAxiosClient({ baseURL, headers = {}, token, requiresAuth = false, appId, serverUrl, interceptResponses = true, }) {
68
68
  const client = axios.create({
69
69
  baseURL,
70
70
  headers: {
@@ -107,7 +107,6 @@ export function createAxiosClient({ baseURL, headers = {}, token, requiresAuth =
107
107
  redirectToLogin(serverUrl, appId);
108
108
  }, 100);
109
109
  }
110
- onError === null || onError === void 0 ? void 0 : onError(base44Error);
111
110
  return Promise.reject(base44Error);
112
111
  });
113
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.7.0-pr.27.eb342e0",
3
+ "version": "0.7.0-pr.28.d052301",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,8 +19,7 @@
19
19
  "prepublishOnly": "npm run build"
20
20
  },
21
21
  "dependencies": {
22
- "axios": "^1.6.2",
23
- "socket.io-client": "^4.7.5"
22
+ "axios": "^1.6.2"
24
23
  },
25
24
  "devDependencies": {
26
25
  "@vitest/coverage-istanbul": "^1.0.0",
@@ -1,16 +0,0 @@
1
- import { RoomsSocket } from "../utils/socket-utils.js";
2
- import { AgentConversation, AgentMessage } from "./agents.types.js";
3
- import { AxiosInstance } from "axios";
4
- export type AgentsModuleConfig = {
5
- axios: AxiosInstance;
6
- socket: ReturnType<typeof RoomsSocket>;
7
- appId: string;
8
- };
9
- export declare function createAgentsModule({ axios, socket, appId, }: AgentsModuleConfig): {
10
- getConversations: () => Promise<AgentConversation[]>;
11
- getConversation: (conversationId: string) => Promise<AgentConversation | undefined>;
12
- listConversations: (filterParams: any) => Promise<AgentConversation[]>;
13
- createConversation: (conversation: any) => Promise<AgentConversation>;
14
- addMessage: (conversation: any, message: any) => Promise<AgentMessage>;
15
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
16
- };
@@ -1,55 +0,0 @@
1
- export function createAgentsModule({ axios, socket, appId, }) {
2
- let currentConversation = null;
3
- const baseURL = `/apps/${appId}/agents`;
4
- const getConversations = () => {
5
- return axios.get(`${baseURL}/conversations`);
6
- };
7
- const getConversation = (conversationId) => {
8
- return axios.get(`${baseURL}/conversations/${conversationId}`);
9
- };
10
- const listConversations = (filterParams) => {
11
- return axios.get(`${baseURL}/conversations`, {
12
- params: filterParams,
13
- });
14
- };
15
- const createConversation = (conversation) => {
16
- return axios.post(`${baseURL}/conversations`, conversation);
17
- };
18
- const addMessage = (conversation, message) => {
19
- // this whole trick with current conversation so that we can call the onUpdateModel with the latest messages
20
- let convLatestMessages = null;
21
- if (currentConversation && currentConversation.id === conversation.id) {
22
- convLatestMessages = currentConversation.messages;
23
- }
24
- else {
25
- currentConversation = conversation;
26
- convLatestMessages = conversation.messages;
27
- }
28
- conversation.messages = [...convLatestMessages, message];
29
- socket.handlers.update_model({
30
- room: `/agent-conversations/${conversation.id}`,
31
- data: JSON.stringify(conversation),
32
- });
33
- return axios.post(`${baseURL}/conversations/${conversation.id}/messages`, message);
34
- };
35
- const subscribeToConversation = (conversationId, onUpdate) => {
36
- return socket.subscribeToRoom(`/agent-conversations/${conversationId}`, {
37
- connect: () => { },
38
- update_model: ({ data: jsonStr }) => {
39
- const data = JSON.parse(jsonStr);
40
- if (currentConversation && currentConversation.id === data.id) {
41
- currentConversation = data;
42
- }
43
- onUpdate(data);
44
- },
45
- });
46
- };
47
- return {
48
- getConversations,
49
- getConversation,
50
- listConversations,
51
- createConversation,
52
- addMessage,
53
- subscribeToConversation,
54
- };
55
- }
@@ -1,44 +0,0 @@
1
- export type AgentConversation = {
2
- id: string;
3
- app_id: string;
4
- agent_name: string;
5
- created_by_id: string;
6
- messages: AgentMessage[];
7
- metadata?: Record<string, any>;
8
- };
9
- export type AgentMessage = {
10
- id: string;
11
- role: "user" | "assistant" | "system";
12
- reasoning: {
13
- start_date: string;
14
- end_date?: string;
15
- content: string;
16
- };
17
- content?: string | Record<string, any> | null;
18
- file_urls?: string[] | null;
19
- tool_calls?: {
20
- id: string;
21
- name: string;
22
- arguments_string: string;
23
- status: "running" | "success" | "error" | "stopped";
24
- results?: string | null;
25
- }[] | null;
26
- usage: {
27
- prompt_tokens?: number;
28
- completion_tokens?: number;
29
- } | null;
30
- hidden?: boolean;
31
- custom_context?: {
32
- message: string;
33
- data: Record<string, any>;
34
- type: string;
35
- }[] | null;
36
- model: string | null;
37
- checkpoint_id: string | null;
38
- metadata?: {
39
- created_date: string;
40
- created_by_email: string;
41
- created_by_full_name: string | null;
42
- };
43
- additional_message_params?: Record<string, any>;
44
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,50 +0,0 @@
1
- import { Socket } from "socket.io-client";
2
- export type RoomsSocketConfig = {
3
- serverUrl: string;
4
- mountPath: string;
5
- transports: string[];
6
- appId: string;
7
- token?: string;
8
- };
9
- export type TSocketRoom = string;
10
- export type TJsonStr = string;
11
- type RoomsSocketEventsMap = {
12
- listen: {
13
- connect: () => void;
14
- update_model: (msg: {
15
- room: string;
16
- data: TJsonStr;
17
- }) => void;
18
- error: (error: Error) => void;
19
- };
20
- emit: {
21
- join: (room: string) => void;
22
- leave: (room: string) => void;
23
- };
24
- };
25
- type TEvent = keyof RoomsSocketEventsMap["listen"];
26
- type THandler<E extends TEvent> = (...args: Parameters<RoomsSocketEventsMap["listen"][E]>) => void;
27
- export declare function RoomsSocket({ config }: {
28
- config: RoomsSocketConfig;
29
- }): {
30
- socket: Socket<{
31
- connect: () => void;
32
- update_model: (msg: {
33
- room: string;
34
- data: TJsonStr;
35
- }) => void;
36
- error: (error: Error) => void;
37
- }, {
38
- join: (room: string) => void;
39
- leave: (room: string) => void;
40
- }>;
41
- subscribeToRoom: (room: TSocketRoom, handlers: Partial<{ [k in TEvent]: THandler<k>; }>) => () => void;
42
- updateConfig: (config: Partial<RoomsSocketConfig>) => void;
43
- handlers: {
44
- connect: THandler<"connect">;
45
- update_model: THandler<"update_model">;
46
- error: THandler<"error">;
47
- };
48
- disconnect: () => void;
49
- };
50
- export {};
@@ -1,103 +0,0 @@
1
- import { io } from "socket.io-client";
2
- import { getAccessToken } from "./auth-utils.js";
3
- function initializeSocket(config, handlers) {
4
- var _a;
5
- const socket = io(config.serverUrl, {
6
- path: config.mountPath,
7
- transports: config.transports,
8
- query: {
9
- app_id: config.appId,
10
- token: (_a = config.token) !== null && _a !== void 0 ? _a : getAccessToken(),
11
- },
12
- });
13
- socket.on("connect", () => {
14
- var _a;
15
- console.log("connect", socket.id);
16
- (_a = handlers.connect) === null || _a === void 0 ? void 0 : _a.call(handlers);
17
- });
18
- socket.on("update_model", (msg) => {
19
- var _a;
20
- (_a = handlers.update_model) === null || _a === void 0 ? void 0 : _a.call(handlers, msg);
21
- });
22
- socket.on("error", (error) => {
23
- var _a;
24
- (_a = handlers.error) === null || _a === void 0 ? void 0 : _a.call(handlers, error);
25
- });
26
- socket.on("connect_error", (error) => {
27
- var _a;
28
- console.error("connect_error", error);
29
- (_a = handlers.error) === null || _a === void 0 ? void 0 : _a.call(handlers, error);
30
- });
31
- return socket;
32
- }
33
- export function RoomsSocket({ config }) {
34
- let currentConfig = { ...config };
35
- const roomsToListeners = {};
36
- const handlers = {
37
- connect: () => {
38
- Object.keys(roomsToListeners).forEach((room) => {
39
- var _a;
40
- joinRoom(room);
41
- (_a = getListeners(room)) === null || _a === void 0 ? void 0 : _a.forEach(({ connect: connectHandler }) => {
42
- connectHandler === null || connectHandler === void 0 ? void 0 : connectHandler();
43
- });
44
- });
45
- },
46
- update_model: (msg) => {
47
- var _a;
48
- if (roomsToListeners[msg.room]) {
49
- (_a = getListeners(msg.room)) === null || _a === void 0 ? void 0 : _a.forEach(({ update_model }) => {
50
- update_model === null || update_model === void 0 ? void 0 : update_model(msg);
51
- });
52
- }
53
- },
54
- error: (error) => {
55
- var _a;
56
- console.error("error", error);
57
- (_a = handlers.error) === null || _a === void 0 ? void 0 : _a.call(handlers, error);
58
- },
59
- };
60
- let socket = initializeSocket(config, handlers);
61
- function cleanup() {
62
- disconnect();
63
- }
64
- function disconnect() {
65
- if (socket) {
66
- socket.disconnect();
67
- }
68
- }
69
- function updateConfig(config) {
70
- cleanup();
71
- currentConfig = {
72
- ...currentConfig,
73
- ...config,
74
- };
75
- socket = initializeSocket(currentConfig, handlers);
76
- }
77
- function joinRoom(room) {
78
- socket.emit("join", room);
79
- }
80
- function leaveRoom(room) {
81
- socket.emit("leave", room);
82
- }
83
- function getListeners(room) {
84
- return roomsToListeners[room];
85
- }
86
- const subscribeToRoom = (room, handlers) => {
87
- if (!roomsToListeners[room]) {
88
- joinRoom(room);
89
- roomsToListeners[room] = [];
90
- }
91
- roomsToListeners[room].push(handlers);
92
- return () => {
93
- roomsToListeners[room] = roomsToListeners[room].filter((listener) => listener !== handlers);
94
- };
95
- };
96
- return {
97
- socket,
98
- subscribeToRoom,
99
- updateConfig,
100
- handlers,
101
- disconnect,
102
- };
103
- }