@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
package/dist/files.client.d.ts
CHANGED
package/dist/files.client.js
CHANGED
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const form_data_1 = __importDefault(require("form-data"));
|
|
6
7
|
const api_client_1 = __importDefault(require("./api-client"));
|
|
7
8
|
class FilesClient extends api_client_1.default {
|
|
8
9
|
/**
|
|
@@ -42,12 +43,17 @@ class FilesClient extends api_client_1.default {
|
|
|
42
43
|
* @returns {Promise<File>} Os dados do arquivo enviado.
|
|
43
44
|
*/
|
|
44
45
|
async uploadFile(props) {
|
|
45
|
-
|
|
46
|
+
// Node: use 'form-data' (evita conflito de tipos com Blob)
|
|
47
|
+
const form = new form_data_1.default();
|
|
46
48
|
form.append("instance", props.instance);
|
|
47
49
|
form.append("dirType", props.dirType);
|
|
48
|
-
form.append("file",
|
|
50
|
+
form.append("file", props.buffer, {
|
|
51
|
+
filename: props.fileName,
|
|
52
|
+
contentType: props.mimeType,
|
|
53
|
+
});
|
|
49
54
|
const response = await this.ax.post("/api/files", form, {
|
|
50
|
-
|
|
55
|
+
// deixe o boundary correto
|
|
56
|
+
headers: form.getHeaders(),
|
|
51
57
|
});
|
|
52
58
|
return response.data.data;
|
|
53
59
|
}
|
|
@@ -17,8 +17,10 @@ export declare enum SocketEventType {
|
|
|
17
17
|
ReportStatus = "report_status",
|
|
18
18
|
InternalChatStarted = "internal_chat_started",
|
|
19
19
|
InternalChatFinished = "internal_chat_finished",
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
InternalMessage = "internal_message",
|
|
21
|
+
InternalMessageEdit = "internal_message_edit",
|
|
22
|
+
InternalMessageDelete = "internal_message_delete",
|
|
23
|
+
InternalMessageStatus = "internal_message_status"
|
|
22
24
|
}
|
|
23
25
|
export interface EmitSocketEventFn {
|
|
24
26
|
(type: SocketEventType.WwebjsQr, room: SocketServerAdminRoom, data: WWEBJSQrEventData): Promise<MessageResponse>;
|
|
@@ -34,6 +36,8 @@ export interface EmitSocketEventFn {
|
|
|
34
36
|
(type: SocketEventType.WppMessageReaction, room: SocketServerChatRoom, data: WppMessageReactionEventData): Promise<MessageResponse>;
|
|
35
37
|
(type: SocketEventType.ReportStatus, room: SocketServerReportsRoom, data: ReportStatusEventData): Promise<MessageResponse>;
|
|
36
38
|
(type: SocketEventType.InternalMessage, room: SocketServerInternalChatRoom, data: InternalMessageEventData): Promise<MessageResponse>;
|
|
39
|
+
(type: SocketEventType.InternalMessageEdit, room: SocketServerInternalChatRoom, data: InternalMessageEditEventData): Promise<MessageResponse>;
|
|
40
|
+
(type: SocketEventType.InternalMessageDelete, room: SocketServerInternalChatRoom, data: InternalMessageDeleteEventData): Promise<MessageResponse>;
|
|
37
41
|
(type: SocketEventType.InternalMessageStatus, room: SocketServerInternalChatRoom, data: InternalMessageEventData): Promise<MessageResponse>;
|
|
38
42
|
(type: SocketEventType.InternalChatStarted, room: SocketServerRoom, data: InternalChatStartedEventData): Promise<MessageResponse>;
|
|
39
43
|
(type: SocketEventType.InternalChatFinished, room: SocketServerRoom, data: InternalChatFinishedEventData): Promise<MessageResponse>;
|
|
@@ -52,6 +56,8 @@ export interface ListenSocketEventFn {
|
|
|
52
56
|
(type: SocketEventType.InternalChatStarted, callback: (data: InternalChatStartedEventData) => void): void;
|
|
53
57
|
(type: SocketEventType.InternalChatFinished, callback: (data: InternalChatFinishedEventData) => void): void;
|
|
54
58
|
(type: SocketEventType.InternalMessage, callback: (data: InternalMessageEventData) => void): void;
|
|
59
|
+
(type: SocketEventType.InternalMessageEdit, callback: (data: InternalMessageEditEventData) => void): void;
|
|
60
|
+
(type: SocketEventType.InternalMessageDelete, callback: (data: InternalMessageDeleteEventData) => void): void;
|
|
55
61
|
(type: SocketEventType.InternalMessageStatus, callback: (data: InternalMessageStatusEventData) => void): void;
|
|
56
62
|
(type: SocketEventType.WppMessageEdit, callback: (data: WppMessageEditEventData) => void): void;
|
|
57
63
|
(type: SocketEventType.WppMessageDelete, callback: (data: WppMessageDeleteEventData) => void): void;
|
|
@@ -119,6 +125,15 @@ export interface InternalContactMessagesReadEventData {
|
|
|
119
125
|
export interface InternalMessageEventData {
|
|
120
126
|
message: InternalMessage;
|
|
121
127
|
}
|
|
128
|
+
export interface InternalMessageEditEventData {
|
|
129
|
+
chatId: number;
|
|
130
|
+
internalMessageId: number;
|
|
131
|
+
newText: string;
|
|
132
|
+
}
|
|
133
|
+
export interface InternalMessageDeleteEventData {
|
|
134
|
+
chatId: number;
|
|
135
|
+
internalMessageId: number;
|
|
136
|
+
}
|
|
122
137
|
export interface InternalMessageStatusEventData {
|
|
123
138
|
chatId: number;
|
|
124
139
|
internalMessageId: number;
|
|
@@ -17,6 +17,8 @@ var SocketEventType;
|
|
|
17
17
|
SocketEventType["ReportStatus"] = "report_status";
|
|
18
18
|
SocketEventType["InternalChatStarted"] = "internal_chat_started";
|
|
19
19
|
SocketEventType["InternalChatFinished"] = "internal_chat_finished";
|
|
20
|
-
SocketEventType["InternalMessageStatus"] = "internal_chat_status";
|
|
21
20
|
SocketEventType["InternalMessage"] = "internal_message";
|
|
21
|
+
SocketEventType["InternalMessageEdit"] = "internal_message_edit";
|
|
22
|
+
SocketEventType["InternalMessageDelete"] = "internal_message_delete";
|
|
23
|
+
SocketEventType["InternalMessageStatus"] = "internal_message_status";
|
|
22
24
|
})(SocketEventType || (exports.SocketEventType = SocketEventType = {}));
|
package/package.json
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@in.pulse-crm/sdk",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "SDKs for abstraction of api consumption of in.pulse-crm application",
|
|
5
|
-
"main": "./dist/index.js",
|
|
6
|
-
"types": "./dist/index.d.ts",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"import": "./dist/index.js",
|
|
11
|
-
"require": "./dist/index.js"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"scripts": {
|
|
15
|
-
"build": "tsc",
|
|
16
|
-
"prettier": "prettier --write .",
|
|
17
|
-
"prettier:check": "prettier --check ."
|
|
18
|
-
},
|
|
19
|
-
"repository": {
|
|
20
|
-
"type": "git",
|
|
21
|
-
"url": "in.pulse-crm"
|
|
22
|
-
},
|
|
23
|
-
"author": "Renan G. Dutra <r.granatodutra@gmail.com>",
|
|
24
|
-
"license": "ISC",
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"@in.pulse-crm/utils": "^1.3.0",
|
|
27
|
-
"axios": "^1.8.3",
|
|
28
|
-
"form-data": "^4.0.2",
|
|
29
|
-
"socket.io-client": "^4.8.1"
|
|
30
|
-
},
|
|
31
|
-
"devDependencies": {
|
|
32
|
-
"@types/node": "^22.13.10",
|
|
33
|
-
"prettier": "^3.5.3",
|
|
34
|
-
"typescript": "^5.8.3"
|
|
35
|
-
}
|
|
36
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@in.pulse-crm/sdk",
|
|
3
|
+
"version": "2.11.0",
|
|
4
|
+
"description": "SDKs for abstraction of api consumption of in.pulse-crm application",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"prettier": "prettier --write .",
|
|
17
|
+
"prettier:check": "prettier --check ."
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "in.pulse-crm"
|
|
22
|
+
},
|
|
23
|
+
"author": "Renan G. Dutra <r.granatodutra@gmail.com>",
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@in.pulse-crm/utils": "^1.3.0",
|
|
27
|
+
"axios": "^1.8.3",
|
|
28
|
+
"form-data": "^4.0.2",
|
|
29
|
+
"socket.io-client": "^4.8.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^22.13.10",
|
|
33
|
+
"prettier": "^3.5.3",
|
|
34
|
+
"typescript": "^5.8.3"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/api-client.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import axios, { AxiosInstance, AxiosError } from "axios";
|
|
2
|
-
import { ErrorResponse } from "./types/response.types";
|
|
3
|
-
|
|
4
|
-
export default class ApiClient {
|
|
5
|
-
public readonly ax: AxiosInstance;
|
|
6
|
-
private baseUrl: string;
|
|
7
|
-
|
|
8
|
-
constructor(baseUrl: string) {
|
|
9
|
-
this.baseUrl = baseUrl;
|
|
10
|
-
|
|
11
|
-
this.ax = axios.create({
|
|
12
|
-
baseURL: `${this.baseUrl}`,
|
|
13
|
-
timeout: 60000,
|
|
14
|
-
headers: {
|
|
15
|
-
"Content-Type": "application/json",
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
this.initializeResponseInterceptor();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private initializeResponseInterceptor() {
|
|
23
|
-
this.ax.interceptors.response.use(null, this.handleError);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
protected handleError = (
|
|
27
|
-
error: AxiosError<ErrorResponse>,
|
|
28
|
-
): Promise<never> => {
|
|
29
|
-
const errorMessage = error.response?.data?.message || error.message;
|
|
30
|
-
return Promise.reject(new Error(errorMessage, { cause: error }));
|
|
31
|
-
};
|
|
32
|
-
}
|
|
1
|
+
import axios, { AxiosInstance, AxiosError } from "axios";
|
|
2
|
+
import { ErrorResponse } from "./types/response.types";
|
|
3
|
+
|
|
4
|
+
export default class ApiClient {
|
|
5
|
+
public readonly ax: AxiosInstance;
|
|
6
|
+
private baseUrl: string;
|
|
7
|
+
|
|
8
|
+
constructor(baseUrl: string) {
|
|
9
|
+
this.baseUrl = baseUrl;
|
|
10
|
+
|
|
11
|
+
this.ax = axios.create({
|
|
12
|
+
baseURL: `${this.baseUrl}`,
|
|
13
|
+
timeout: 60000,
|
|
14
|
+
headers: {
|
|
15
|
+
"Content-Type": "application/json",
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
this.initializeResponseInterceptor();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private initializeResponseInterceptor() {
|
|
23
|
+
this.ax.interceptors.response.use(null, this.handleError);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected handleError = (
|
|
27
|
+
error: AxiosError<ErrorResponse>,
|
|
28
|
+
): Promise<never> => {
|
|
29
|
+
const errorMessage = error.response?.data?.message || error.message;
|
|
30
|
+
return Promise.reject(new Error(errorMessage, { cause: error }));
|
|
31
|
+
};
|
|
32
|
+
}
|
package/src/auth.client.ts
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import { sanitizeErrorMessage } from "@in.pulse-crm/utils";
|
|
2
|
-
import { DataResponse } from "./types/response.types";
|
|
3
|
-
import ApiClient from "./api-client";
|
|
4
|
-
import { LoginData, SessionData, UserOnlineSession } from "./types/auth.types";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Classe AuthSDK para interagir com a API de autenticação.
|
|
8
|
-
*/
|
|
9
|
-
export default class AuthClient extends ApiClient {
|
|
10
|
-
/**
|
|
11
|
-
* Realiza o login do usuário.
|
|
12
|
-
* @param instance Nome da instância do Inpulse.
|
|
13
|
-
* @param username Nome de usuário.
|
|
14
|
-
* @param password Senha do usuário.
|
|
15
|
-
* @returns Dados de login.
|
|
16
|
-
*/
|
|
17
|
-
public async login(instance: string, username: string, password: string) {
|
|
18
|
-
const { data: res } = await this.ax.post<
|
|
19
|
-
DataResponse<LoginData>
|
|
20
|
-
>(`/api/auth/login`, { LOGIN: username, SENHA: password, instance });
|
|
21
|
-
|
|
22
|
-
return res.data;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Busca os dados da sessão.
|
|
27
|
-
* @param authToken Token de autenticação.
|
|
28
|
-
* @returns Dados da sessão.
|
|
29
|
-
*/
|
|
30
|
-
public async fetchSessionData(authToken: string) {
|
|
31
|
-
const { data: res } = await this.ax
|
|
32
|
-
.get<DataResponse<SessionData>>(`/api/auth/session`, {
|
|
33
|
-
headers: {
|
|
34
|
-
authorization: authToken,
|
|
35
|
-
},
|
|
36
|
-
})
|
|
37
|
-
.catch((error) => {
|
|
38
|
-
const message = sanitizeErrorMessage(error);
|
|
39
|
-
throw new Error("Failed to fetch session data! " + message);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return res.data;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Verifica se o usuário está autenticado.
|
|
47
|
-
* @param instanceName Nome da instância do Inpulse.
|
|
48
|
-
* @param authToken Token de autenticação.
|
|
49
|
-
* @returns Verdadeiro se o usuário estiver autenticado, falso caso contrário.
|
|
50
|
-
*/
|
|
51
|
-
public async isAuthenticated(
|
|
52
|
-
instanceName: string,
|
|
53
|
-
authToken: string,
|
|
54
|
-
): Promise<boolean> {
|
|
55
|
-
try {
|
|
56
|
-
const session = await this.fetchSessionData(authToken);
|
|
57
|
-
|
|
58
|
-
return !!session.userId && session.instance === instanceName;
|
|
59
|
-
} catch {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Verifica se o usuário está autorizado.
|
|
66
|
-
* @param instanceName Nome da instância do Inpulse.
|
|
67
|
-
* @param authToken Token de autenticação.
|
|
68
|
-
* @param authorizedRoles Lista de papéis autorizados.
|
|
69
|
-
* @returns Verdadeiro se o usuário estiver autorizado, falso caso contrário.
|
|
70
|
-
*/
|
|
71
|
-
public async isAuthorized(
|
|
72
|
-
instanceName: string,
|
|
73
|
-
authToken: string,
|
|
74
|
-
authorizedRoles: string[],
|
|
75
|
-
) {
|
|
76
|
-
try {
|
|
77
|
-
const session = await this.fetchSessionData(authToken);
|
|
78
|
-
|
|
79
|
-
return (
|
|
80
|
-
authorizedRoles.includes(session.role) &&
|
|
81
|
-
session.instance === instanceName
|
|
82
|
-
);
|
|
83
|
-
} catch {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public async getOnlineSessions(instance: string) {
|
|
89
|
-
const { data: res } = await this.ax.get<
|
|
90
|
-
DataResponse<UserOnlineSession[]>
|
|
91
|
-
>("/api/online-sessions", {
|
|
92
|
-
params: {
|
|
93
|
-
instance,
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
return res.data;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
public async initOnlineSession(authToken: string) {
|
|
101
|
-
await this.ax.post(
|
|
102
|
-
"/api/online-sessions",
|
|
103
|
-
{},
|
|
104
|
-
{
|
|
105
|
-
headers: {
|
|
106
|
-
Authorization: authToken,
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
public async finishOnlineSession(authToken: string) {
|
|
113
|
-
await this.ax.delete("/api/online-sessions", {
|
|
114
|
-
headers: {
|
|
115
|
-
Authorization: authToken,
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
}
|
|
1
|
+
import { sanitizeErrorMessage } from "@in.pulse-crm/utils";
|
|
2
|
+
import { DataResponse } from "./types/response.types";
|
|
3
|
+
import ApiClient from "./api-client";
|
|
4
|
+
import { LoginData, SessionData, UserOnlineSession } from "./types/auth.types";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Classe AuthSDK para interagir com a API de autenticação.
|
|
8
|
+
*/
|
|
9
|
+
export default class AuthClient extends ApiClient {
|
|
10
|
+
/**
|
|
11
|
+
* Realiza o login do usuário.
|
|
12
|
+
* @param instance Nome da instância do Inpulse.
|
|
13
|
+
* @param username Nome de usuário.
|
|
14
|
+
* @param password Senha do usuário.
|
|
15
|
+
* @returns Dados de login.
|
|
16
|
+
*/
|
|
17
|
+
public async login(instance: string, username: string, password: string) {
|
|
18
|
+
const { data: res } = await this.ax.post<
|
|
19
|
+
DataResponse<LoginData>
|
|
20
|
+
>(`/api/auth/login`, { LOGIN: username, SENHA: password, instance });
|
|
21
|
+
|
|
22
|
+
return res.data;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Busca os dados da sessão.
|
|
27
|
+
* @param authToken Token de autenticação.
|
|
28
|
+
* @returns Dados da sessão.
|
|
29
|
+
*/
|
|
30
|
+
public async fetchSessionData(authToken: string) {
|
|
31
|
+
const { data: res } = await this.ax
|
|
32
|
+
.get<DataResponse<SessionData>>(`/api/auth/session`, {
|
|
33
|
+
headers: {
|
|
34
|
+
authorization: authToken,
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
.catch((error) => {
|
|
38
|
+
const message = sanitizeErrorMessage(error);
|
|
39
|
+
throw new Error("Failed to fetch session data! " + message);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return res.data;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Verifica se o usuário está autenticado.
|
|
47
|
+
* @param instanceName Nome da instância do Inpulse.
|
|
48
|
+
* @param authToken Token de autenticação.
|
|
49
|
+
* @returns Verdadeiro se o usuário estiver autenticado, falso caso contrário.
|
|
50
|
+
*/
|
|
51
|
+
public async isAuthenticated(
|
|
52
|
+
instanceName: string,
|
|
53
|
+
authToken: string,
|
|
54
|
+
): Promise<boolean> {
|
|
55
|
+
try {
|
|
56
|
+
const session = await this.fetchSessionData(authToken);
|
|
57
|
+
|
|
58
|
+
return !!session.userId && session.instance === instanceName;
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Verifica se o usuário está autorizado.
|
|
66
|
+
* @param instanceName Nome da instância do Inpulse.
|
|
67
|
+
* @param authToken Token de autenticação.
|
|
68
|
+
* @param authorizedRoles Lista de papéis autorizados.
|
|
69
|
+
* @returns Verdadeiro se o usuário estiver autorizado, falso caso contrário.
|
|
70
|
+
*/
|
|
71
|
+
public async isAuthorized(
|
|
72
|
+
instanceName: string,
|
|
73
|
+
authToken: string,
|
|
74
|
+
authorizedRoles: string[],
|
|
75
|
+
) {
|
|
76
|
+
try {
|
|
77
|
+
const session = await this.fetchSessionData(authToken);
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
authorizedRoles.includes(session.role) &&
|
|
81
|
+
session.instance === instanceName
|
|
82
|
+
);
|
|
83
|
+
} catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public async getOnlineSessions(instance: string) {
|
|
89
|
+
const { data: res } = await this.ax.get<
|
|
90
|
+
DataResponse<UserOnlineSession[]>
|
|
91
|
+
>("/api/online-sessions", {
|
|
92
|
+
params: {
|
|
93
|
+
instance,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return res.data;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public async initOnlineSession(authToken: string) {
|
|
101
|
+
await this.ax.post(
|
|
102
|
+
"/api/online-sessions",
|
|
103
|
+
{},
|
|
104
|
+
{
|
|
105
|
+
headers: {
|
|
106
|
+
Authorization: authToken,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public async finishOnlineSession(authToken: string) {
|
|
113
|
+
await this.ax.delete("/api/online-sessions", {
|
|
114
|
+
headers: {
|
|
115
|
+
Authorization: authToken,
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
package/src/customers.client.ts
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import ApiClient from "./api-client";
|
|
2
|
-
import { PaginatedResponse, RequestFilters } from "./types";
|
|
3
|
-
import {
|
|
4
|
-
CreateCustomerDTO,
|
|
5
|
-
Customer,
|
|
6
|
-
UpdateCustomerDTO,
|
|
7
|
-
} from "./types/customers.types";
|
|
8
|
-
|
|
9
|
-
class CustomersClient extends ApiClient {
|
|
10
|
-
/**
|
|
11
|
-
* Cria um novo cliente.
|
|
12
|
-
* @param data - Os dados do cliente a serem criados.
|
|
13
|
-
* @returns Uma Promise que resolve para o cliente criado.
|
|
14
|
-
*/
|
|
15
|
-
public async createCustomer(data: CreateCustomerDTO) {
|
|
16
|
-
const response = await this.ax.post(`/api/customers`, data);
|
|
17
|
-
|
|
18
|
-
return response.data;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Obtém um cliente pelo ID.
|
|
23
|
-
* @param customerId - O ID do cliente a ser obtido.
|
|
24
|
-
* @returns Uma Promise que resolve para o cliente obtido.
|
|
25
|
-
*/
|
|
26
|
-
public async getCustomerById(customerId: number) {
|
|
27
|
-
const response = await this.ax.get(
|
|
28
|
-
`/api/customers/${customerId}`,
|
|
29
|
-
);
|
|
30
|
-
return response.data;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Atualiza um cliente existente.
|
|
35
|
-
* @param customerId - O ID do cliente a ser atualizado.
|
|
36
|
-
* @param data - Os dados atualizados do cliente.
|
|
37
|
-
* @returns Uma Promise que resolve para o cliente atualizado.
|
|
38
|
-
*/
|
|
39
|
-
public async updateCustomer(customerId: number, data: UpdateCustomerDTO) {
|
|
40
|
-
const response = await this.ax.patch(
|
|
41
|
-
`/api/customers/${customerId}`,
|
|
42
|
-
data,
|
|
43
|
-
);
|
|
44
|
-
return response.data;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Obtém todos os clientes.
|
|
49
|
-
*
|
|
50
|
-
* @param filters - Filtros opcionais para a busca de clientes.
|
|
51
|
-
* @todo Implementar tipagem para os filtros.
|
|
52
|
-
* @returns Uma Promise que resolve para uma lista de clientes.
|
|
53
|
-
*/
|
|
54
|
-
public async getCustomers(filters?: RequestFilters<Customer>) {
|
|
55
|
-
let baseUrl = `/api/customers`;
|
|
56
|
-
|
|
57
|
-
if (filters) {
|
|
58
|
-
const params = new URLSearchParams(filters);
|
|
59
|
-
baseUrl += `?${params.toString()}`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const response =
|
|
63
|
-
await this.ax.get<PaginatedResponse<Customer>>(baseUrl);
|
|
64
|
-
|
|
65
|
-
return response.data;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Define o token de autenticação para as requisições.
|
|
70
|
-
* @param token - O token de autenticação a ser definido.
|
|
71
|
-
*/
|
|
72
|
-
public setAuth(token: string) {
|
|
73
|
-
this.ax.defaults.headers.common["Authorization"] =
|
|
74
|
-
`Bearer ${token}`;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export default CustomersClient;
|
|
1
|
+
import ApiClient from "./api-client";
|
|
2
|
+
import { PaginatedResponse, RequestFilters } from "./types";
|
|
3
|
+
import {
|
|
4
|
+
CreateCustomerDTO,
|
|
5
|
+
Customer,
|
|
6
|
+
UpdateCustomerDTO,
|
|
7
|
+
} from "./types/customers.types";
|
|
8
|
+
|
|
9
|
+
class CustomersClient extends ApiClient {
|
|
10
|
+
/**
|
|
11
|
+
* Cria um novo cliente.
|
|
12
|
+
* @param data - Os dados do cliente a serem criados.
|
|
13
|
+
* @returns Uma Promise que resolve para o cliente criado.
|
|
14
|
+
*/
|
|
15
|
+
public async createCustomer(data: CreateCustomerDTO) {
|
|
16
|
+
const response = await this.ax.post(`/api/customers`, data);
|
|
17
|
+
|
|
18
|
+
return response.data;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Obtém um cliente pelo ID.
|
|
23
|
+
* @param customerId - O ID do cliente a ser obtido.
|
|
24
|
+
* @returns Uma Promise que resolve para o cliente obtido.
|
|
25
|
+
*/
|
|
26
|
+
public async getCustomerById(customerId: number) {
|
|
27
|
+
const response = await this.ax.get(
|
|
28
|
+
`/api/customers/${customerId}`,
|
|
29
|
+
);
|
|
30
|
+
return response.data;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Atualiza um cliente existente.
|
|
35
|
+
* @param customerId - O ID do cliente a ser atualizado.
|
|
36
|
+
* @param data - Os dados atualizados do cliente.
|
|
37
|
+
* @returns Uma Promise que resolve para o cliente atualizado.
|
|
38
|
+
*/
|
|
39
|
+
public async updateCustomer(customerId: number, data: UpdateCustomerDTO) {
|
|
40
|
+
const response = await this.ax.patch(
|
|
41
|
+
`/api/customers/${customerId}`,
|
|
42
|
+
data,
|
|
43
|
+
);
|
|
44
|
+
return response.data;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Obtém todos os clientes.
|
|
49
|
+
*
|
|
50
|
+
* @param filters - Filtros opcionais para a busca de clientes.
|
|
51
|
+
* @todo Implementar tipagem para os filtros.
|
|
52
|
+
* @returns Uma Promise que resolve para uma lista de clientes.
|
|
53
|
+
*/
|
|
54
|
+
public async getCustomers(filters?: RequestFilters<Customer>) {
|
|
55
|
+
let baseUrl = `/api/customers`;
|
|
56
|
+
|
|
57
|
+
if (filters) {
|
|
58
|
+
const params = new URLSearchParams(filters);
|
|
59
|
+
baseUrl += `?${params.toString()}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const response =
|
|
63
|
+
await this.ax.get<PaginatedResponse<Customer>>(baseUrl);
|
|
64
|
+
|
|
65
|
+
return response.data;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Define o token de autenticação para as requisições.
|
|
70
|
+
* @param token - O token de autenticação a ser definido.
|
|
71
|
+
*/
|
|
72
|
+
public setAuth(token: string) {
|
|
73
|
+
this.ax.defaults.headers.common["Authorization"] =
|
|
74
|
+
`Bearer ${token}`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export default CustomersClient;
|