@in.pulse-crm/sdk 2.10.7 → 2.11.0
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/files.client.d.ts +0 -2
- package/dist/files.client.js +9 -3
- package/dist/types/files.types.d.ts +0 -2
- package/dist/types/internal.types.d.ts +1 -1
- package/dist/types/socket-events.types.d.ts +17 -2
- package/dist/types/socket-events.types.js +3 -1
- package/package.json +36 -36
- package/src/api-client.ts +32 -32
- package/src/auth.client.ts +119 -119
- package/src/customers.client.ts +78 -78
- package/src/files.client.ts +79 -80
- package/src/index.ts +12 -12
- package/src/instance.client.ts +42 -42
- package/src/internal.client.ts +142 -142
- package/src/ready-message.client.ts +72 -72
- package/src/reports.client.ts +111 -111
- package/src/socket-server.client.ts +33 -33
- package/src/socket.client.ts +91 -91
- package/src/types/auth.types.ts +42 -42
- package/src/types/customers.types.ts +78 -78
- package/src/types/files.types.ts +82 -82
- package/src/types/index.ts +12 -12
- package/src/types/internal.types.ts +78 -78
- package/src/types/ready-messages.types.ts +10 -10
- package/src/types/reports.types.ts +49 -49
- package/src/types/request.types.ts +5 -5
- package/src/types/response.types.ts +26 -26
- package/src/types/socket-events.types.ts +308 -279
- package/src/types/socket-rooms.types.ts +66 -66
- package/src/types/user.types.ts +162 -162
- package/src/types/wallet.types.ts +5 -5
- package/src/types/whatsapp.types.ts +232 -232
- package/src/users.client.ts +88 -88
- package/src/wallets.client.ts +124 -124
- package/src/whatsapp.client.ts +374 -374
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
import ApiClient from "./api-client";
|
|
2
|
-
import { DataResponse } from "./types/response.types";
|
|
3
|
-
import {
|
|
4
|
-
ReadyMessage,
|
|
5
|
-
} from "./types/ready-messages.types";
|
|
6
|
-
import FormData from "form-data";
|
|
7
|
-
|
|
8
|
-
export default class ReadyMessageClient extends ApiClient {
|
|
9
|
-
public async createReadyMessage(
|
|
10
|
-
File: File | null = null,
|
|
11
|
-
TITULO: string| null = null,
|
|
12
|
-
TEXTO_MENSAGEM: string| null = null,
|
|
13
|
-
SETOR: number | null = null,
|
|
14
|
-
) {
|
|
15
|
-
const form = new FormData();
|
|
16
|
-
|
|
17
|
-
if (File) {
|
|
18
|
-
form.append("file", File);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
form.append(
|
|
22
|
-
"data",
|
|
23
|
-
JSON.stringify({ TITULO, TEXTO_MENSAGEM,SETOR }),
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
const { data: res } = await this.ax.post<
|
|
27
|
-
DataResponse<ReadyMessage>
|
|
28
|
-
>(`/api/ready-messages`, form, {
|
|
29
|
-
headers: {
|
|
30
|
-
"Content-Type": "multipart/form-data",
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
return res.data;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public async deleteReadyMessage(chatId: number) {
|
|
38
|
-
const url = `/api/ready-messages/${chatId}`;
|
|
39
|
-
await this.ax.delete<DataResponse<ReadyMessage>>(url);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public async getReadyMessages() {
|
|
43
|
-
const url = `/api/ready-messages`;
|
|
44
|
-
const { data: res } =
|
|
45
|
-
await this.ax.get<DataResponse<ReadyMessage[]>>(url);
|
|
46
|
-
|
|
47
|
-
return res.data;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public async updateReadyMessage(id:number, ReadyMessage: ReadyMessage, file: File) {
|
|
51
|
-
const formData = new FormData();
|
|
52
|
-
formData.append("file", file);
|
|
53
|
-
formData.append(
|
|
54
|
-
"data",
|
|
55
|
-
JSON.stringify(ReadyMessage),
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
const { data: res } = await this.ax.put<
|
|
59
|
-
DataResponse<ReadyMessage>
|
|
60
|
-
>(`/api/ready-messages/${id}`, formData, {
|
|
61
|
-
headers: {
|
|
62
|
-
"Content-Type": "multipart/form-data",
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
return res.data;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public setAuth(token: string) {
|
|
69
|
-
this.ax.defaults.headers.common["Authorization"] =
|
|
70
|
-
`Bearer ${token}`;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
1
|
+
import ApiClient from "./api-client";
|
|
2
|
+
import { DataResponse } from "./types/response.types";
|
|
3
|
+
import {
|
|
4
|
+
ReadyMessage,
|
|
5
|
+
} from "./types/ready-messages.types";
|
|
6
|
+
import FormData from "form-data";
|
|
7
|
+
|
|
8
|
+
export default class ReadyMessageClient extends ApiClient {
|
|
9
|
+
public async createReadyMessage(
|
|
10
|
+
File: File | null = null,
|
|
11
|
+
TITULO: string| null = null,
|
|
12
|
+
TEXTO_MENSAGEM: string| null = null,
|
|
13
|
+
SETOR: number | null = null,
|
|
14
|
+
) {
|
|
15
|
+
const form = new FormData();
|
|
16
|
+
|
|
17
|
+
if (File) {
|
|
18
|
+
form.append("file", File);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
form.append(
|
|
22
|
+
"data",
|
|
23
|
+
JSON.stringify({ TITULO, TEXTO_MENSAGEM,SETOR }),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const { data: res } = await this.ax.post<
|
|
27
|
+
DataResponse<ReadyMessage>
|
|
28
|
+
>(`/api/ready-messages`, form, {
|
|
29
|
+
headers: {
|
|
30
|
+
"Content-Type": "multipart/form-data",
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return res.data;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async deleteReadyMessage(chatId: number) {
|
|
38
|
+
const url = `/api/ready-messages/${chatId}`;
|
|
39
|
+
await this.ax.delete<DataResponse<ReadyMessage>>(url);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public async getReadyMessages() {
|
|
43
|
+
const url = `/api/ready-messages`;
|
|
44
|
+
const { data: res } =
|
|
45
|
+
await this.ax.get<DataResponse<ReadyMessage[]>>(url);
|
|
46
|
+
|
|
47
|
+
return res.data;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async updateReadyMessage(id:number, ReadyMessage: ReadyMessage, file: File) {
|
|
51
|
+
const formData = new FormData();
|
|
52
|
+
formData.append("file", file);
|
|
53
|
+
formData.append(
|
|
54
|
+
"data",
|
|
55
|
+
JSON.stringify(ReadyMessage),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const { data: res } = await this.ax.put<
|
|
59
|
+
DataResponse<ReadyMessage>
|
|
60
|
+
>(`/api/ready-messages/${id}`, formData, {
|
|
61
|
+
headers: {
|
|
62
|
+
"Content-Type": "multipart/form-data",
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
return res.data;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public setAuth(token: string) {
|
|
69
|
+
this.ax.defaults.headers.common["Authorization"] =
|
|
70
|
+
`Bearer ${token}`;
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/reports.client.ts
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
import ApiClient from "./api-client";
|
|
2
|
-
import { ChatsReport, ExecuteSqlReportOptions, ExportSqlReportOptions, GenerateChatsReportOptions, SQLReportRow } from "./types/reports.types";
|
|
3
|
-
import { DataResponse, MessageResponse } from "./types/response.types";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* ReportsClient class to handle reports related API calls.
|
|
7
|
-
*
|
|
8
|
-
* This class extends the ApiClient class and provides methods to interact with the reports API.
|
|
9
|
-
* It includes methods to get, generate, and delete chat reports.
|
|
10
|
-
*/
|
|
11
|
-
export default class ReportsClient extends ApiClient {
|
|
12
|
-
/**
|
|
13
|
-
* Retrieves chat reports from the server.
|
|
14
|
-
*
|
|
15
|
-
* @returns A promise that resolves to an array of chat reports wrapped in a `DataResponse` object.
|
|
16
|
-
*/
|
|
17
|
-
public async getChatsReports() {
|
|
18
|
-
const url = `/api/reports/chats`;
|
|
19
|
-
const { data: res } =
|
|
20
|
-
await this.ax.get<DataResponse<Array<ChatsReport>>>(url);
|
|
21
|
-
|
|
22
|
-
return res.data;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Generates a report for chat interactions based on the provided options.
|
|
27
|
-
*
|
|
28
|
-
* @param body - The options for generating the chats report, including filters and parameters.
|
|
29
|
-
* @returns A promise that resolves to the data of the generated chats report.
|
|
30
|
-
*/
|
|
31
|
-
public async generateChatsReport(body: GenerateChatsReportOptions) {
|
|
32
|
-
const url = `/api/reports/chats`;
|
|
33
|
-
const { data: res } = await this.ax.post<
|
|
34
|
-
DataResponse<ChatsReport>
|
|
35
|
-
>(url, body);
|
|
36
|
-
|
|
37
|
-
return res.data;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Deletes a chat report by its unique identifier.
|
|
42
|
-
*
|
|
43
|
-
* @param chatsReportId - The unique identifier of the chat report to be deleted.
|
|
44
|
-
*/
|
|
45
|
-
public async deleteChatsReport(chatsReportId: number) {
|
|
46
|
-
const url = `/api/reports/chats/${chatsReportId}`;
|
|
47
|
-
await this.ax.delete<MessageResponse>(url);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Desative a report sql by its unique identifier.
|
|
51
|
-
*
|
|
52
|
-
* @param reportId - The unique identifier of the chat report to be deleted.
|
|
53
|
-
*/
|
|
54
|
-
|
|
55
|
-
public async deleteHistoryReport(reportId: number) {
|
|
56
|
-
const url = `/api/reports-history/${reportId}`;
|
|
57
|
-
await this.ax.delete<MessageResponse>(url);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Execute a report sql interactions based on the provided options.
|
|
61
|
-
*
|
|
62
|
-
* @param body - The options for execute the report sql.
|
|
63
|
-
* @returns A promise that resolves to the data of the generated chats report.
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
public async executeSqlReport(body: ExecuteSqlReportOptions) {
|
|
67
|
-
const url = `/api/execute-report-sql`;
|
|
68
|
-
const { data: res } = await this.ax.post<DataResponse<SQLReportRow[]>>(url, body);
|
|
69
|
-
return res.data;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Retrieves chat reports from the server.
|
|
74
|
-
*
|
|
75
|
-
* @returns A promise that resolves to an array of chat reports wrapped in a `DataResponse` object.
|
|
76
|
-
*/
|
|
77
|
-
public async getSqlReportsHistory() {
|
|
78
|
-
const url = `/api/reports-history`;
|
|
79
|
-
const { data: res } =
|
|
80
|
-
await this.ax.get<DataResponse<Array<any>>>(url);
|
|
81
|
-
|
|
82
|
-
return res.data;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Export a report sql interactions based on the provided options.
|
|
86
|
-
*
|
|
87
|
-
* @param body - The options for export the report sql.
|
|
88
|
-
* @returns A promise that resolves to the data of the generated chats report.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
public async exportReportSql(body: ExportSqlReportOptions) {
|
|
92
|
-
const url = `/api/export-report-sql`;
|
|
93
|
-
const response = await this.ax.post(url, body, {
|
|
94
|
-
responseType: 'blob',
|
|
95
|
-
});
|
|
96
|
-
return response.data as Blob;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Sets the authorization token for the HTTP client.
|
|
102
|
-
*
|
|
103
|
-
* @param token - The Bearer token to be used for authentication.
|
|
104
|
-
* This token will be included in the `Authorization` header
|
|
105
|
-
* of all HTTP requests made by the client.
|
|
106
|
-
*/
|
|
107
|
-
public setAuth(token: string) {
|
|
108
|
-
this.ax.defaults.headers.common["Authorization"] =
|
|
109
|
-
`Bearer ${token}`;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
1
|
+
import ApiClient from "./api-client";
|
|
2
|
+
import { ChatsReport, ExecuteSqlReportOptions, ExportSqlReportOptions, GenerateChatsReportOptions, SQLReportRow } from "./types/reports.types";
|
|
3
|
+
import { DataResponse, MessageResponse } from "./types/response.types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ReportsClient class to handle reports related API calls.
|
|
7
|
+
*
|
|
8
|
+
* This class extends the ApiClient class and provides methods to interact with the reports API.
|
|
9
|
+
* It includes methods to get, generate, and delete chat reports.
|
|
10
|
+
*/
|
|
11
|
+
export default class ReportsClient extends ApiClient {
|
|
12
|
+
/**
|
|
13
|
+
* Retrieves chat reports from the server.
|
|
14
|
+
*
|
|
15
|
+
* @returns A promise that resolves to an array of chat reports wrapped in a `DataResponse` object.
|
|
16
|
+
*/
|
|
17
|
+
public async getChatsReports() {
|
|
18
|
+
const url = `/api/reports/chats`;
|
|
19
|
+
const { data: res } =
|
|
20
|
+
await this.ax.get<DataResponse<Array<ChatsReport>>>(url);
|
|
21
|
+
|
|
22
|
+
return res.data;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Generates a report for chat interactions based on the provided options.
|
|
27
|
+
*
|
|
28
|
+
* @param body - The options for generating the chats report, including filters and parameters.
|
|
29
|
+
* @returns A promise that resolves to the data of the generated chats report.
|
|
30
|
+
*/
|
|
31
|
+
public async generateChatsReport(body: GenerateChatsReportOptions) {
|
|
32
|
+
const url = `/api/reports/chats`;
|
|
33
|
+
const { data: res } = await this.ax.post<
|
|
34
|
+
DataResponse<ChatsReport>
|
|
35
|
+
>(url, body);
|
|
36
|
+
|
|
37
|
+
return res.data;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Deletes a chat report by its unique identifier.
|
|
42
|
+
*
|
|
43
|
+
* @param chatsReportId - The unique identifier of the chat report to be deleted.
|
|
44
|
+
*/
|
|
45
|
+
public async deleteChatsReport(chatsReportId: number) {
|
|
46
|
+
const url = `/api/reports/chats/${chatsReportId}`;
|
|
47
|
+
await this.ax.delete<MessageResponse>(url);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Desative a report sql by its unique identifier.
|
|
51
|
+
*
|
|
52
|
+
* @param reportId - The unique identifier of the chat report to be deleted.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
public async deleteHistoryReport(reportId: number) {
|
|
56
|
+
const url = `/api/reports-history/${reportId}`;
|
|
57
|
+
await this.ax.delete<MessageResponse>(url);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Execute a report sql interactions based on the provided options.
|
|
61
|
+
*
|
|
62
|
+
* @param body - The options for execute the report sql.
|
|
63
|
+
* @returns A promise that resolves to the data of the generated chats report.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
public async executeSqlReport(body: ExecuteSqlReportOptions) {
|
|
67
|
+
const url = `/api/execute-report-sql`;
|
|
68
|
+
const { data: res } = await this.ax.post<DataResponse<SQLReportRow[]>>(url, body);
|
|
69
|
+
return res.data;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Retrieves chat reports from the server.
|
|
74
|
+
*
|
|
75
|
+
* @returns A promise that resolves to an array of chat reports wrapped in a `DataResponse` object.
|
|
76
|
+
*/
|
|
77
|
+
public async getSqlReportsHistory() {
|
|
78
|
+
const url = `/api/reports-history`;
|
|
79
|
+
const { data: res } =
|
|
80
|
+
await this.ax.get<DataResponse<Array<any>>>(url);
|
|
81
|
+
|
|
82
|
+
return res.data;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Export a report sql interactions based on the provided options.
|
|
86
|
+
*
|
|
87
|
+
* @param body - The options for export the report sql.
|
|
88
|
+
* @returns A promise that resolves to the data of the generated chats report.
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
public async exportReportSql(body: ExportSqlReportOptions) {
|
|
92
|
+
const url = `/api/export-report-sql`;
|
|
93
|
+
const response = await this.ax.post(url, body, {
|
|
94
|
+
responseType: 'blob',
|
|
95
|
+
});
|
|
96
|
+
return response.data as Blob;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Sets the authorization token for the HTTP client.
|
|
102
|
+
*
|
|
103
|
+
* @param token - The Bearer token to be used for authentication.
|
|
104
|
+
* This token will be included in the `Authorization` header
|
|
105
|
+
* of all HTTP requests made by the client.
|
|
106
|
+
*/
|
|
107
|
+
public setAuth(token: string) {
|
|
108
|
+
this.ax.defaults.headers.common["Authorization"] =
|
|
109
|
+
`Bearer ${token}`;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { EmitSocketEventFn } from "./types/socket-events.types";
|
|
2
|
-
import ApiClient from "./api-client";
|
|
3
|
-
import { MessageResponse } from "./types/response.types";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A client for interacting with a socket server API.
|
|
7
|
-
* Extends the `ApiClient` class to provide http functionality
|
|
8
|
-
* for emitting events to specific rooms on the server.
|
|
9
|
-
*/
|
|
10
|
-
export default class SocketServerApi extends ApiClient {
|
|
11
|
-
/**
|
|
12
|
-
* Creates an instance of the SocketServerApiClient.
|
|
13
|
-
*
|
|
14
|
-
* @param baseUrl - The base URL for the socket server API.
|
|
15
|
-
*/
|
|
16
|
-
constructor(baseUrl: string) {
|
|
17
|
-
super(baseUrl);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Emits a socket event to a specified room with a given event name and value.
|
|
22
|
-
*
|
|
23
|
-
* @param event - The name of the event to emit.
|
|
24
|
-
* @param room - The name of the room to which the event should be emitted.
|
|
25
|
-
* @param value - The payload or data to send with the event.
|
|
26
|
-
* @returns A promise that resolves with the response from the API call.
|
|
27
|
-
*/
|
|
28
|
-
public emit: EmitSocketEventFn = (event, room, value) => {
|
|
29
|
-
return this.ax
|
|
30
|
-
.post<MessageResponse>(`/api/ws/emit/${room}/${event}`, value)
|
|
31
|
-
.then((res) => res.data);
|
|
32
|
-
};
|
|
33
|
-
}
|
|
1
|
+
import { EmitSocketEventFn } from "./types/socket-events.types";
|
|
2
|
+
import ApiClient from "./api-client";
|
|
3
|
+
import { MessageResponse } from "./types/response.types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A client for interacting with a socket server API.
|
|
7
|
+
* Extends the `ApiClient` class to provide http functionality
|
|
8
|
+
* for emitting events to specific rooms on the server.
|
|
9
|
+
*/
|
|
10
|
+
export default class SocketServerApi extends ApiClient {
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of the SocketServerApiClient.
|
|
13
|
+
*
|
|
14
|
+
* @param baseUrl - The base URL for the socket server API.
|
|
15
|
+
*/
|
|
16
|
+
constructor(baseUrl: string) {
|
|
17
|
+
super(baseUrl);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Emits a socket event to a specified room with a given event name and value.
|
|
22
|
+
*
|
|
23
|
+
* @param event - The name of the event to emit.
|
|
24
|
+
* @param room - The name of the room to which the event should be emitted.
|
|
25
|
+
* @param value - The payload or data to send with the event.
|
|
26
|
+
* @returns A promise that resolves with the response from the API call.
|
|
27
|
+
*/
|
|
28
|
+
public emit: EmitSocketEventFn = (event, room, value) => {
|
|
29
|
+
return this.ax
|
|
30
|
+
.post<MessageResponse>(`/api/ws/emit/${room}/${event}`, value)
|
|
31
|
+
.then((res) => res.data);
|
|
32
|
+
};
|
|
33
|
+
}
|
package/src/socket.client.ts
CHANGED
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
import { io, Socket } from "socket.io-client";
|
|
2
|
-
import {
|
|
3
|
-
ListenSocketEventFn,
|
|
4
|
-
SocketEventType,
|
|
5
|
-
UnlistenSocketEventFn,
|
|
6
|
-
} from "./types/socket-events.types";
|
|
7
|
-
import { JoinRoomFn } from "./types";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* A client for interacting with a WebSocket server.
|
|
11
|
-
* This class provides methods to connect to the server, listen for events,
|
|
12
|
-
* and manage WebSocket connections.
|
|
13
|
-
*/
|
|
14
|
-
export default class SocketClient {
|
|
15
|
-
private readonly ws: Socket;
|
|
16
|
-
private readonly listeners: Map<SocketEventType, any> = new Map();
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Initializes a new instance of the socket client.
|
|
20
|
-
*
|
|
21
|
-
* @param baseUrl - The base URL of the WebSocket server to connect to.
|
|
22
|
-
* This URL is used to establish the WebSocket connection.
|
|
23
|
-
*
|
|
24
|
-
* The WebSocket client is configured with the following options:
|
|
25
|
-
* - `autoConnect`: Disabled by default to allow manual connection control.
|
|
26
|
-
* - `transports`: Uses the 'websocket' transport protocol exclusively.
|
|
27
|
-
*/
|
|
28
|
-
constructor(baseUrl: string) {
|
|
29
|
-
this.ws = io(baseUrl, {
|
|
30
|
-
autoConnect: false,
|
|
31
|
-
transports: ["websocket"],
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Establishes a WebSocket connection using the provided authentication token.
|
|
37
|
-
*
|
|
38
|
-
* @param token - The authentication token to be used for the WebSocket connection.
|
|
39
|
-
* This token is sent as part of the WebSocket authentication payload.
|
|
40
|
-
*/
|
|
41
|
-
public connect(token: string) {
|
|
42
|
-
this.ws.auth = { token };
|
|
43
|
-
this.ws.connect();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Disconnects the WebSocket client from the server.
|
|
48
|
-
* This method terminates the current WebSocket connection
|
|
49
|
-
* by invoking the `disconnect` method on the WebSocket instance.
|
|
50
|
-
*/
|
|
51
|
-
public disconnect() {
|
|
52
|
-
this.ws.disconnect();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Registers an event listener for a specified WebSocket event and provides a way to remove it.
|
|
57
|
-
*
|
|
58
|
-
* @param event - The name of the WebSocket event to listen for.
|
|
59
|
-
* @param callback - A function to be executed when the event is triggered. The function receives the event data as its argument.
|
|
60
|
-
* @returns A function that, when called, removes the event listener for the specified event.
|
|
61
|
-
*/
|
|
62
|
-
public on: ListenSocketEventFn = (event, callback) => {
|
|
63
|
-
const oldListener = this.listeners.get(event);
|
|
64
|
-
if (oldListener) {
|
|
65
|
-
this.ws.off(event, oldListener);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
this.ws.on(event, callback);
|
|
69
|
-
this.listeners.set(event, callback);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Removes a previously registered event listener from the WebSocket connection.
|
|
74
|
-
*
|
|
75
|
-
* @param event - The name of the event to stop listening for.
|
|
76
|
-
* @param callback - The callback function that was previously registered for the event.
|
|
77
|
-
*/
|
|
78
|
-
public off: UnlistenSocketEventFn = (event) => {
|
|
79
|
-
const listener = this.listeners.get(event);
|
|
80
|
-
if (!listener) return;
|
|
81
|
-
this.ws.off(event, listener);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
public joinRoom: JoinRoomFn = (room) => {
|
|
85
|
-
this.ws.emit("join-room", room);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
public leaveRoom: JoinRoomFn = (room) => {
|
|
89
|
-
this.ws.emit("leave-room", room);
|
|
90
|
-
};
|
|
91
|
-
}
|
|
1
|
+
import { io, Socket } from "socket.io-client";
|
|
2
|
+
import {
|
|
3
|
+
ListenSocketEventFn,
|
|
4
|
+
SocketEventType,
|
|
5
|
+
UnlistenSocketEventFn,
|
|
6
|
+
} from "./types/socket-events.types";
|
|
7
|
+
import { JoinRoomFn } from "./types";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A client for interacting with a WebSocket server.
|
|
11
|
+
* This class provides methods to connect to the server, listen for events,
|
|
12
|
+
* and manage WebSocket connections.
|
|
13
|
+
*/
|
|
14
|
+
export default class SocketClient {
|
|
15
|
+
private readonly ws: Socket;
|
|
16
|
+
private readonly listeners: Map<SocketEventType, any> = new Map();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Initializes a new instance of the socket client.
|
|
20
|
+
*
|
|
21
|
+
* @param baseUrl - The base URL of the WebSocket server to connect to.
|
|
22
|
+
* This URL is used to establish the WebSocket connection.
|
|
23
|
+
*
|
|
24
|
+
* The WebSocket client is configured with the following options:
|
|
25
|
+
* - `autoConnect`: Disabled by default to allow manual connection control.
|
|
26
|
+
* - `transports`: Uses the 'websocket' transport protocol exclusively.
|
|
27
|
+
*/
|
|
28
|
+
constructor(baseUrl: string) {
|
|
29
|
+
this.ws = io(baseUrl, {
|
|
30
|
+
autoConnect: false,
|
|
31
|
+
transports: ["websocket"],
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Establishes a WebSocket connection using the provided authentication token.
|
|
37
|
+
*
|
|
38
|
+
* @param token - The authentication token to be used for the WebSocket connection.
|
|
39
|
+
* This token is sent as part of the WebSocket authentication payload.
|
|
40
|
+
*/
|
|
41
|
+
public connect(token: string) {
|
|
42
|
+
this.ws.auth = { token };
|
|
43
|
+
this.ws.connect();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Disconnects the WebSocket client from the server.
|
|
48
|
+
* This method terminates the current WebSocket connection
|
|
49
|
+
* by invoking the `disconnect` method on the WebSocket instance.
|
|
50
|
+
*/
|
|
51
|
+
public disconnect() {
|
|
52
|
+
this.ws.disconnect();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Registers an event listener for a specified WebSocket event and provides a way to remove it.
|
|
57
|
+
*
|
|
58
|
+
* @param event - The name of the WebSocket event to listen for.
|
|
59
|
+
* @param callback - A function to be executed when the event is triggered. The function receives the event data as its argument.
|
|
60
|
+
* @returns A function that, when called, removes the event listener for the specified event.
|
|
61
|
+
*/
|
|
62
|
+
public on: ListenSocketEventFn = (event, callback) => {
|
|
63
|
+
const oldListener = this.listeners.get(event);
|
|
64
|
+
if (oldListener) {
|
|
65
|
+
this.ws.off(event, oldListener);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
this.ws.on(event, callback);
|
|
69
|
+
this.listeners.set(event, callback);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Removes a previously registered event listener from the WebSocket connection.
|
|
74
|
+
*
|
|
75
|
+
* @param event - The name of the event to stop listening for.
|
|
76
|
+
* @param callback - The callback function that was previously registered for the event.
|
|
77
|
+
*/
|
|
78
|
+
public off: UnlistenSocketEventFn = (event) => {
|
|
79
|
+
const listener = this.listeners.get(event);
|
|
80
|
+
if (!listener) return;
|
|
81
|
+
this.ws.off(event, listener);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
public joinRoom: JoinRoomFn = (room) => {
|
|
85
|
+
this.ws.emit("join-room", room);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
public leaveRoom: JoinRoomFn = (room) => {
|
|
89
|
+
this.ws.emit("leave-room", room);
|
|
90
|
+
};
|
|
91
|
+
}
|