@in.pulse-crm/sdk 2.11.2 → 2.11.4
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/.prettierrc +4 -4
- package/dist/files.client.d.ts +1 -0
- package/dist/files.client.js +4 -0
- package/dist/whatsapp.client.d.ts +1 -1
- package/dist/whatsapp.client.js +2 -2
- 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 +85 -79
- 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 +79 -79
- 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 -308
- 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/tsconfig.json +16 -16
package/.prettierrc
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
{
|
|
2
|
-
"tabWidth": 4,
|
|
3
|
-
"useTabs": true
|
|
4
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"tabWidth": 4,
|
|
3
|
+
"useTabs": true
|
|
4
|
+
}
|
package/dist/files.client.d.ts
CHANGED
package/dist/files.client.js
CHANGED
|
@@ -65,5 +65,9 @@ class FilesClient extends api_client_1.default {
|
|
|
65
65
|
async deleteFile(id) {
|
|
66
66
|
await this.ax.delete(`/api/files/${id}`);
|
|
67
67
|
}
|
|
68
|
+
async uploadWabaMedia(instance, wabaMediaId) {
|
|
69
|
+
const response = await this.ax.post(`/api/waba`, { instance, wabaMediaId });
|
|
70
|
+
return response.data.data;
|
|
71
|
+
}
|
|
68
72
|
}
|
|
69
73
|
exports.default = FilesClient;
|
|
@@ -15,7 +15,7 @@ export default class WhatsappClient extends ApiClient {
|
|
|
15
15
|
markContactMessagesAsRead(contactId: number): Promise<WppMessage[]>;
|
|
16
16
|
sendMessage(to: string, data: SendMessageData): Promise<WppMessage>;
|
|
17
17
|
editMessage(messageId: string, newText: string, isInternal?: boolean): Promise<void>;
|
|
18
|
-
finishChatById(id: number, resultId: number): Promise<void>;
|
|
18
|
+
finishChatById(id: number, resultId: number, triggerSatisfactionBot?: boolean): Promise<void>;
|
|
19
19
|
startChatByContactId(contactId: number, template?: any): Promise<void>;
|
|
20
20
|
getResults(): Promise<{
|
|
21
21
|
id: number;
|
package/dist/whatsapp.client.js
CHANGED
|
@@ -60,9 +60,9 @@ class WhatsappClient extends api_client_1.default {
|
|
|
60
60
|
const body = { newText };
|
|
61
61
|
await this.ax.put(url, body);
|
|
62
62
|
}
|
|
63
|
-
async finishChatById(id, resultId) {
|
|
63
|
+
async finishChatById(id, resultId, triggerSatisfactionBot = false) {
|
|
64
64
|
const url = `/api/whatsapp/chats/${id}/finish`;
|
|
65
|
-
const body = { resultId };
|
|
65
|
+
const body = { resultId, triggerSatisfactionBot };
|
|
66
66
|
await this.ax.post(url, body);
|
|
67
67
|
}
|
|
68
68
|
async startChatByContactId(contactId, template) {
|
package/package.json
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@in.pulse-crm/sdk",
|
|
3
|
-
"version": "2.11.
|
|
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.4",
|
|
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;
|