@base44-preview/sdk 0.7.0-pr.27.ff44f1a → 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 +8 -38
- package/dist/client.js +6 -24
- package/dist/modules/auth.d.ts +7 -2
- package/dist/modules/auth.js +6 -3
- package/package.json +2 -3
- package/dist/modules/agents.d.ts +0 -15
- package/dist/modules/agents.js +0 -96
- package/dist/modules/agents.types.d.ts +0 -44
- package/dist/modules/agents.types.js +0 -1
- package/dist/utils/socket-utils.d.ts +0 -48
- package/dist/utils/socket-utils.js +0 -73
package/dist/client.d.ts
CHANGED
|
@@ -38,15 +38,6 @@ export declare function createClient(config: {
|
|
|
38
38
|
functions: {
|
|
39
39
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
40
40
|
};
|
|
41
|
-
agents: {
|
|
42
|
-
getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
43
|
-
getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
|
|
44
|
-
listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
45
|
-
createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
|
|
46
|
-
addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
|
|
47
|
-
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
48
|
-
updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
|
|
49
|
-
};
|
|
50
41
|
};
|
|
51
42
|
entities: {};
|
|
52
43
|
integrations: {};
|
|
@@ -56,7 +47,10 @@ export declare function createClient(config: {
|
|
|
56
47
|
redirectToLogin(nextUrl: string): void;
|
|
57
48
|
logout(redirectUrl?: string): void;
|
|
58
49
|
setToken(token: string, saveToStorage?: boolean): void;
|
|
59
|
-
loginViaEmailPassword(email: string, password: string,
|
|
50
|
+
loginViaEmailPassword(email: string, password: string, options?: {
|
|
51
|
+
turnstileToken?: string;
|
|
52
|
+
fromUrl?: string;
|
|
53
|
+
}): Promise<{
|
|
60
54
|
access_token: string;
|
|
61
55
|
user: any;
|
|
62
56
|
}>;
|
|
@@ -65,15 +59,6 @@ export declare function createClient(config: {
|
|
|
65
59
|
functions: {
|
|
66
60
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
67
61
|
};
|
|
68
|
-
agents: {
|
|
69
|
-
getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
70
|
-
getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
|
|
71
|
-
listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
72
|
-
createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
|
|
73
|
-
addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
|
|
74
|
-
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
75
|
-
updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
|
|
76
|
-
};
|
|
77
62
|
};
|
|
78
63
|
export declare function createClientFromRequest(request: Request): {
|
|
79
64
|
/**
|
|
@@ -99,15 +84,6 @@ export declare function createClientFromRequest(request: Request): {
|
|
|
99
84
|
functions: {
|
|
100
85
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
101
86
|
};
|
|
102
|
-
agents: {
|
|
103
|
-
getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
104
|
-
getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
|
|
105
|
-
listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
106
|
-
createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
|
|
107
|
-
addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
|
|
108
|
-
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
109
|
-
updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
|
|
110
|
-
};
|
|
111
87
|
};
|
|
112
88
|
entities: {};
|
|
113
89
|
integrations: {};
|
|
@@ -117,7 +93,10 @@ export declare function createClientFromRequest(request: Request): {
|
|
|
117
93
|
redirectToLogin(nextUrl: string): void;
|
|
118
94
|
logout(redirectUrl?: string): void;
|
|
119
95
|
setToken(token: string, saveToStorage?: boolean): void;
|
|
120
|
-
loginViaEmailPassword(email: string, password: string,
|
|
96
|
+
loginViaEmailPassword(email: string, password: string, options?: {
|
|
97
|
+
turnstileToken?: string;
|
|
98
|
+
fromUrl?: string;
|
|
99
|
+
}): Promise<{
|
|
121
100
|
access_token: string;
|
|
122
101
|
user: any;
|
|
123
102
|
}>;
|
|
@@ -126,13 +105,4 @@ export declare function createClientFromRequest(request: Request): {
|
|
|
126
105
|
functions: {
|
|
127
106
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
128
107
|
};
|
|
129
|
-
agents: {
|
|
130
|
-
getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
131
|
-
getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
|
|
132
|
-
listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
133
|
-
createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
|
|
134
|
-
addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
|
|
135
|
-
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
136
|
-
updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
|
|
137
|
-
};
|
|
138
108
|
};
|
package/dist/client.js
CHANGED
|
@@ -5,7 +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
8
|
/**
|
|
10
9
|
* Create a Base44 client instance
|
|
11
10
|
* @param {Object} config - Client configuration
|
|
@@ -63,22 +62,12 @@ export function createClient(config) {
|
|
|
63
62
|
integrations: createIntegrationsModule(axiosClient, appId),
|
|
64
63
|
auth: createAuthModule(axiosClient, functionsAxiosClient, appId),
|
|
65
64
|
functions: createFunctionsModule(functionsAxiosClient, appId),
|
|
66
|
-
agents: createAgentsModule({
|
|
67
|
-
serverUrl,
|
|
68
|
-
appId,
|
|
69
|
-
token,
|
|
70
|
-
}),
|
|
71
65
|
};
|
|
72
66
|
const serviceRoleModules = {
|
|
73
67
|
entities: createEntitiesModule(serviceRoleAxiosClient, appId),
|
|
74
68
|
integrations: createIntegrationsModule(serviceRoleAxiosClient, appId),
|
|
75
69
|
sso: createSsoModule(serviceRoleAxiosClient, appId, token),
|
|
76
70
|
functions: createFunctionsModule(serviceRoleFunctionsAxiosClient, appId),
|
|
77
|
-
agents: createAgentsModule({
|
|
78
|
-
serverUrl,
|
|
79
|
-
appId,
|
|
80
|
-
token: serviceToken,
|
|
81
|
-
}),
|
|
82
71
|
};
|
|
83
72
|
// Always try to get token from localStorage or URL parameters
|
|
84
73
|
if (typeof window !== "undefined") {
|
|
@@ -86,9 +75,6 @@ export function createClient(config) {
|
|
|
86
75
|
const accessToken = token || getAccessToken();
|
|
87
76
|
if (accessToken) {
|
|
88
77
|
userModules.auth.setToken(accessToken);
|
|
89
|
-
userModules.agents.updateConfig({
|
|
90
|
-
token: accessToken,
|
|
91
|
-
});
|
|
92
78
|
}
|
|
93
79
|
}
|
|
94
80
|
// If authentication is required, verify token and redirect to login if needed
|
|
@@ -134,10 +120,10 @@ export function createClient(config) {
|
|
|
134
120
|
*/
|
|
135
121
|
get asServiceRole() {
|
|
136
122
|
if (!serviceToken) {
|
|
137
|
-
throw new Error(
|
|
123
|
+
throw new Error('Service token is required to use asServiceRole. Please provide a serviceToken when creating the client.');
|
|
138
124
|
}
|
|
139
125
|
return serviceRoleModules;
|
|
140
|
-
}
|
|
126
|
+
}
|
|
141
127
|
};
|
|
142
128
|
return client;
|
|
143
129
|
}
|
|
@@ -153,20 +139,16 @@ export function createClientFromRequest(request) {
|
|
|
153
139
|
let serviceRoleToken;
|
|
154
140
|
let userToken;
|
|
155
141
|
if (serviceRoleAuthHeader !== null) {
|
|
156
|
-
if (serviceRoleAuthHeader ===
|
|
157
|
-
!serviceRoleAuthHeader.startsWith("Bearer ") ||
|
|
158
|
-
serviceRoleAuthHeader.split(" ").length !== 2) {
|
|
142
|
+
if (serviceRoleAuthHeader === '' || !serviceRoleAuthHeader.startsWith('Bearer ') || serviceRoleAuthHeader.split(' ').length !== 2) {
|
|
159
143
|
throw new Error('Invalid authorization header format. Expected "Bearer <token>"');
|
|
160
144
|
}
|
|
161
|
-
serviceRoleToken = serviceRoleAuthHeader.split(
|
|
145
|
+
serviceRoleToken = serviceRoleAuthHeader.split(' ')[1];
|
|
162
146
|
}
|
|
163
147
|
if (authHeader !== null) {
|
|
164
|
-
if (authHeader ===
|
|
165
|
-
!authHeader.startsWith("Bearer ") ||
|
|
166
|
-
authHeader.split(" ").length !== 2) {
|
|
148
|
+
if (authHeader === '' || !authHeader.startsWith('Bearer ') || authHeader.split(' ').length !== 2) {
|
|
167
149
|
throw new Error('Invalid authorization header format. Expected "Bearer <token>"');
|
|
168
150
|
}
|
|
169
|
-
userToken = authHeader.split(
|
|
151
|
+
userToken = authHeader.split(' ')[1];
|
|
170
152
|
}
|
|
171
153
|
return createClient({
|
|
172
154
|
serverUrl: serverUrlHeader || "https://base44.app",
|
package/dist/modules/auth.d.ts
CHANGED
|
@@ -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
|
|
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,
|
|
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
|
}>;
|
package/dist/modules/auth.js
CHANGED
|
@@ -95,16 +95,19 @@ export function createAuthModule(axios, functionsAxiosClient, appId) {
|
|
|
95
95
|
* Login via username and password
|
|
96
96
|
* @param email - User email
|
|
97
97
|
* @param password - User password
|
|
98
|
-
* @param
|
|
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
|
|
99
101
|
* @returns Login response with access_token and user
|
|
100
102
|
*/
|
|
101
|
-
async loginViaEmailPassword(email, password,
|
|
103
|
+
async loginViaEmailPassword(email, password, options) {
|
|
102
104
|
var _a;
|
|
103
105
|
try {
|
|
104
106
|
const response = await axios.post(`/apps/${appId}/auth/login`, {
|
|
105
107
|
email,
|
|
106
108
|
password,
|
|
107
|
-
...(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 }),
|
|
108
111
|
});
|
|
109
112
|
const { access_token, user } = response;
|
|
110
113
|
if (access_token) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@base44-preview/sdk",
|
|
3
|
-
"version": "0.7.0-pr.
|
|
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",
|
package/dist/modules/agents.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { AgentConversation, AgentMessage } from "./agents.types";
|
|
2
|
-
export type AgentsModuleConfig = {
|
|
3
|
-
serverUrl: string;
|
|
4
|
-
appId: string;
|
|
5
|
-
token?: string;
|
|
6
|
-
};
|
|
7
|
-
export declare function createAgentsModule({ appId, serverUrl, token, }: AgentsModuleConfig): {
|
|
8
|
-
getConversations: () => Promise<AgentConversation[]>;
|
|
9
|
-
getConversation: (conversationId: string) => Promise<AgentConversation | undefined>;
|
|
10
|
-
listConversations: (filterParams: any) => Promise<AgentConversation[]>;
|
|
11
|
-
createConversation: (conversation: any) => Promise<AgentConversation>;
|
|
12
|
-
addMessage: (conversation: any, message: any) => Promise<AgentMessage>;
|
|
13
|
-
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
14
|
-
updateConfig: (config: Partial<AgentsModuleConfig>) => void;
|
|
15
|
-
};
|
package/dist/modules/agents.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { RoomsSocket } from "../utils/socket-utils";
|
|
2
|
-
import { createAxiosClient } from "../utils/axios-client";
|
|
3
|
-
export function createAgentsModule({ appId, serverUrl, token, }) {
|
|
4
|
-
let currentConversation = null;
|
|
5
|
-
const socketConfig = {
|
|
6
|
-
serverUrl,
|
|
7
|
-
mountPath: "/ws-user-apps/socket.io/",
|
|
8
|
-
transports: ["websocket"],
|
|
9
|
-
query: {
|
|
10
|
-
appId,
|
|
11
|
-
token,
|
|
12
|
-
},
|
|
13
|
-
};
|
|
14
|
-
const axiosConfig = {
|
|
15
|
-
serverUrl,
|
|
16
|
-
appId,
|
|
17
|
-
token,
|
|
18
|
-
};
|
|
19
|
-
let axios = createAgentsAxiosClient({
|
|
20
|
-
serverUrl,
|
|
21
|
-
appId,
|
|
22
|
-
token,
|
|
23
|
-
});
|
|
24
|
-
const roomSocket = RoomsSocket({
|
|
25
|
-
config: socketConfig,
|
|
26
|
-
});
|
|
27
|
-
const updateConfig = (config) => {
|
|
28
|
-
axios = createAgentsAxiosClient({ ...axiosConfig, ...config });
|
|
29
|
-
roomSocket.updateConfig({ ...socketConfig, ...config });
|
|
30
|
-
};
|
|
31
|
-
const getConversations = () => {
|
|
32
|
-
return axios.get(`/conversations`);
|
|
33
|
-
};
|
|
34
|
-
const getConversation = (conversationId) => {
|
|
35
|
-
return axios.get(`/conversations/${conversationId}`);
|
|
36
|
-
};
|
|
37
|
-
const listConversations = (filterParams) => {
|
|
38
|
-
return axios.get(`/conversations`, {
|
|
39
|
-
params: filterParams,
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
const createConversation = (conversation) => {
|
|
43
|
-
return axios.post(`/conversations`, conversation);
|
|
44
|
-
};
|
|
45
|
-
const addMessage = (conversation, message) => {
|
|
46
|
-
// this whole trick with current conversation so that we can call the onUpdateModel with the latest messages
|
|
47
|
-
let convLatestMessages = null;
|
|
48
|
-
if (currentConversation && currentConversation.id === conversation.id) {
|
|
49
|
-
convLatestMessages = currentConversation.messages;
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
currentConversation = conversation;
|
|
53
|
-
convLatestMessages = conversation.messages;
|
|
54
|
-
}
|
|
55
|
-
conversation.messages = [...convLatestMessages, message];
|
|
56
|
-
roomSocket.handlers.update_model({
|
|
57
|
-
room: `/agent-conversations/${conversation.id}`,
|
|
58
|
-
data: JSON.stringify(conversation),
|
|
59
|
-
});
|
|
60
|
-
return axios.post(`/conversations/${conversation.id}/messages`, message);
|
|
61
|
-
};
|
|
62
|
-
const subscribeToConversation = (conversationId, onUpdate) => {
|
|
63
|
-
return roomSocket.subscribeToRoom(`/agent-conversations/${conversationId}`, {
|
|
64
|
-
connect: () => { },
|
|
65
|
-
update_model: ({ data: jsonStr }) => {
|
|
66
|
-
const data = JSON.parse(jsonStr);
|
|
67
|
-
if (currentConversation && currentConversation.id === data.id) {
|
|
68
|
-
currentConversation = data;
|
|
69
|
-
}
|
|
70
|
-
onUpdate(data);
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
return {
|
|
75
|
-
getConversations,
|
|
76
|
-
getConversation,
|
|
77
|
-
listConversations,
|
|
78
|
-
createConversation,
|
|
79
|
-
addMessage,
|
|
80
|
-
subscribeToConversation,
|
|
81
|
-
updateConfig,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
function createAgentsAxiosClient({ serverUrl, appId, token, }) {
|
|
85
|
-
const axios = createAxiosClient({
|
|
86
|
-
baseURL: `${serverUrl}/api/apps/${appId}/agents`,
|
|
87
|
-
appId,
|
|
88
|
-
serverUrl,
|
|
89
|
-
token,
|
|
90
|
-
interceptResponses: true,
|
|
91
|
-
headers: {
|
|
92
|
-
"X-App-Id": String(appId),
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
return axios;
|
|
96
|
-
}
|
|
@@ -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,48 +0,0 @@
|
|
|
1
|
-
import { Socket } from "socket.io-client";
|
|
2
|
-
export type RoomsSocketConfig = {
|
|
3
|
-
serverUrl: string;
|
|
4
|
-
mountPath: string;
|
|
5
|
-
transports: string[];
|
|
6
|
-
query: {
|
|
7
|
-
appId: string;
|
|
8
|
-
token?: string;
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
export type TSocketRoom = string;
|
|
12
|
-
export type TJsonStr = string;
|
|
13
|
-
type RoomsSocketEventsMap = {
|
|
14
|
-
listen: {
|
|
15
|
-
connect: () => void;
|
|
16
|
-
update_model: (msg: {
|
|
17
|
-
room: string;
|
|
18
|
-
data: TJsonStr;
|
|
19
|
-
}) => void;
|
|
20
|
-
};
|
|
21
|
-
emit: {
|
|
22
|
-
join: (room: string) => void;
|
|
23
|
-
leave: (room: string) => void;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
type TEvent = keyof RoomsSocketEventsMap["listen"];
|
|
27
|
-
type THandler<E extends TEvent> = (...args: Parameters<RoomsSocketEventsMap["listen"][E]>) => void;
|
|
28
|
-
export declare function RoomsSocket({ config }: {
|
|
29
|
-
config: RoomsSocketConfig;
|
|
30
|
-
}): {
|
|
31
|
-
socket: Readonly<Socket<{
|
|
32
|
-
connect: () => void;
|
|
33
|
-
update_model: (msg: {
|
|
34
|
-
room: string;
|
|
35
|
-
data: TJsonStr;
|
|
36
|
-
}) => void;
|
|
37
|
-
}, {
|
|
38
|
-
join: (room: string) => void;
|
|
39
|
-
leave: (room: string) => void;
|
|
40
|
-
}>>;
|
|
41
|
-
subscribeToRoom: (room: TSocketRoom, handlers: { [k in TEvent]: THandler<k>; }) => () => void;
|
|
42
|
-
updateConfig: (config: RoomsSocketConfig) => void;
|
|
43
|
-
handlers: {
|
|
44
|
-
connect: THandler<"connect">;
|
|
45
|
-
update_model: THandler<"update_model">;
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
export {};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { io } from "socket.io-client";
|
|
2
|
-
function initializeSocket(config, handlers) {
|
|
3
|
-
const socket = io(config.serverUrl, {
|
|
4
|
-
path: config.mountPath,
|
|
5
|
-
transports: config.transports,
|
|
6
|
-
query: config.query,
|
|
7
|
-
});
|
|
8
|
-
socket.on("connect", () => {
|
|
9
|
-
console.log("connect", socket.id);
|
|
10
|
-
handlers.connect();
|
|
11
|
-
});
|
|
12
|
-
socket.on("update_model", (msg) => {
|
|
13
|
-
handlers.update_model(msg);
|
|
14
|
-
});
|
|
15
|
-
return socket;
|
|
16
|
-
}
|
|
17
|
-
export function RoomsSocket({ config }) {
|
|
18
|
-
const roomsToListeners = {};
|
|
19
|
-
const handlers = {
|
|
20
|
-
connect: () => {
|
|
21
|
-
Object.keys(roomsToListeners).forEach((room) => {
|
|
22
|
-
var _a;
|
|
23
|
-
joinRoom(room);
|
|
24
|
-
(_a = getListeners(room)) === null || _a === void 0 ? void 0 : _a.forEach(({ connect: connectHandler }) => {
|
|
25
|
-
connectHandler();
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
},
|
|
29
|
-
update_model: (msg) => {
|
|
30
|
-
var _a;
|
|
31
|
-
if (roomsToListeners[msg.room]) {
|
|
32
|
-
(_a = getListeners(msg.room)) === null || _a === void 0 ? void 0 : _a.forEach(({ update_model }) => {
|
|
33
|
-
update_model(msg);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
let socket = initializeSocket(config, handlers);
|
|
39
|
-
function cleanup() {
|
|
40
|
-
if (socket) {
|
|
41
|
-
socket.disconnect();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function updateConfig(config) {
|
|
45
|
-
cleanup();
|
|
46
|
-
socket = initializeSocket(config, handlers);
|
|
47
|
-
}
|
|
48
|
-
function joinRoom(room) {
|
|
49
|
-
socket.emit("join", room);
|
|
50
|
-
}
|
|
51
|
-
function leaveRoom(room) {
|
|
52
|
-
socket.emit("leave", room);
|
|
53
|
-
}
|
|
54
|
-
function getListeners(room) {
|
|
55
|
-
return roomsToListeners[room];
|
|
56
|
-
}
|
|
57
|
-
const subscribeToRoom = (room, handlers) => {
|
|
58
|
-
if (!roomsToListeners[room]) {
|
|
59
|
-
joinRoom(room);
|
|
60
|
-
roomsToListeners[room] = [];
|
|
61
|
-
}
|
|
62
|
-
roomsToListeners[room].push(handlers);
|
|
63
|
-
return () => {
|
|
64
|
-
roomsToListeners[room] = roomsToListeners[room].filter((listener) => listener !== handlers);
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
return {
|
|
68
|
-
socket: Object.freeze(socket),
|
|
69
|
-
subscribeToRoom,
|
|
70
|
-
updateConfig,
|
|
71
|
-
handlers,
|
|
72
|
-
};
|
|
73
|
-
}
|