Skip to content

Commit 52230ed

Browse files
committed
session worker compatibility
1 parent dd123c6 commit 52230ed

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "evolution-api",
3-
"version": "1.8.0",
3+
"version": "1.8.1",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/src/main.js",
66
"scripts": {
@@ -49,7 +49,7 @@
4949
"amqplib": "^0.10.3",
5050
"@aws-sdk/client-sqs": "^3.569.0",
5151
"axios": "^1.6.5",
52-
"@whiskeysockets/baileys": "6.7.2",
52+
"@whiskeysockets/baileys": "6.7.4",
5353
"class-validator": "^0.14.1",
5454
"compression": "^1.7.4",
5555
"cors": "^2.8.5",

src/api/provider/sessions.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import axios from 'axios';
22
import { execSync } from 'child_process';
33

4-
import { ConfigService, ProviderSession } from '../../config/env.config';
4+
import { Auth, ConfigService, ProviderSession } from '../../config/env.config';
55
import { Logger } from '../../config/logger.config';
66

77
type ResponseSuccess = { status: number; data?: any };
@@ -10,11 +10,13 @@ type ResponseProvider = Promise<[ResponseSuccess?, Error?]>;
1010
export class ProviderFiles {
1111
constructor(private readonly configService: ConfigService) {
1212
this.baseUrl = `http://${this.config.HOST}:${this.config.PORT}/session/${this.config.PREFIX}`;
13+
this.globalApiToken = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
1314
}
1415

1516
private readonly logger = new Logger(ProviderFiles.name);
1617

1718
private baseUrl: string;
19+
private globalApiToken: string;
1820

1921
private readonly config = Object.freeze(this.configService.get<ProviderSession>('PROVIDER'));
2022

@@ -24,14 +26,14 @@ export class ProviderFiles {
2426

2527
public async onModuleInit() {
2628
if (this.config.ENABLED) {
27-
const client = axios.create({
28-
baseURL: this.baseUrl,
29-
});
29+
const url = `http://${this.config.HOST}:${this.config.PORT}`;
3030
try {
31-
const response = await client.options('/ping');
32-
if (!response?.data?.pong) {
31+
const response = await axios.options(url + '/ping');
32+
if (response?.data != 'pong') {
3333
throw new Error('Offline file provider.');
3434
}
35+
36+
await axios.post(`${url}/session`, { group: this.config.PREFIX }, { headers: { apikey: this.globalApiToken } });
3537
} catch (error) {
3638
this.logger.error(['Failed to connect to the file server', error?.message, error?.stack]);
3739
const pid = process.pid;
@@ -46,9 +48,13 @@ export class ProviderFiles {
4648

4749
public async create(instance: string): ResponseProvider {
4850
try {
49-
const response = await axios.post(`${this.baseUrl}`, {
50-
instance,
51-
});
51+
const response = await axios.post(
52+
`${this.baseUrl}`,
53+
{
54+
instance,
55+
},
56+
{ headers: { apikey: this.globalApiToken } },
57+
);
5258
return [{ status: response.status, data: response?.data }];
5359
} catch (error) {
5460
return [
@@ -63,7 +69,9 @@ export class ProviderFiles {
6369

6470
public async write(instance: string, key: string, data: any): ResponseProvider {
6571
try {
66-
const response = await axios.post(`${this.baseUrl}/${instance}/${key}`, data);
72+
const response = await axios.post(`${this.baseUrl}/${instance}/${key}`, data, {
73+
headers: { apikey: this.globalApiToken },
74+
});
6775
return [{ status: response.status, data: response?.data }];
6876
} catch (error) {
6977
return [
@@ -78,7 +86,9 @@ export class ProviderFiles {
7886

7987
public async read(instance: string, key: string): ResponseProvider {
8088
try {
81-
const response = await axios.get(`${this.baseUrl}/${instance}/${key}`);
89+
const response = await axios.get(`${this.baseUrl}/${instance}/${key}`, {
90+
headers: { apikey: this.globalApiToken },
91+
});
8292
return [{ status: response.status, data: response?.data }];
8393
} catch (error) {
8494
return [
@@ -93,7 +103,9 @@ export class ProviderFiles {
93103

94104
public async delete(instance: string, key: string): ResponseProvider {
95105
try {
96-
const response = await axios.delete(`${this.baseUrl}/${instance}/${key}`);
106+
const response = await axios.delete(`${this.baseUrl}/${instance}/${key}`, {
107+
headers: { apikey: this.globalApiToken },
108+
});
97109
return [{ status: response.status, data: response?.data }];
98110
} catch (error) {
99111
return [
@@ -108,7 +120,7 @@ export class ProviderFiles {
108120

109121
public async allInstances(): ResponseProvider {
110122
try {
111-
const response = await axios.get(`${this.baseUrl}/list-instances`);
123+
const response = await axios.get(`${this.baseUrl}/list-instances`, { headers: { apikey: this.globalApiToken } });
112124
return [{ status: response.status, data: response?.data as string[] }];
113125
} catch (error) {
114126
return [
@@ -123,7 +135,7 @@ export class ProviderFiles {
123135

124136
public async removeSession(instance: string): ResponseProvider {
125137
try {
126-
const response = await axios.delete(`${this.baseUrl}/${instance}`);
138+
const response = await axios.delete(`${this.baseUrl}/${instance}`, { headers: { apikey: this.globalApiToken } });
127139
return [{ status: response.status, data: response?.data }];
128140
} catch (error) {
129141
return [

src/utils/use-multi-file-auth-state-provider-files.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class AuthStateProvider {
6767
data: json,
6868
});
6969
if (error) {
70-
this.logger.error(['writeData', error?.message, error?.stack]);
70+
// this.logger.error(['writeData', error?.message, error?.stack]);
7171
return;
7272
}
7373
return response;
@@ -76,7 +76,7 @@ export class AuthStateProvider {
7676
const readData = async (key: string): Promise<any> => {
7777
const [response, error] = await this.providerFiles.read(instance, key);
7878
if (error) {
79-
this.logger.error(['readData', error?.message, error?.stack]);
79+
// this.logger.error(['readData', error?.message, error?.stack]);
8080
return;
8181
}
8282
if (isNotEmpty(response?.data)) {
@@ -87,7 +87,7 @@ export class AuthStateProvider {
8787
const removeData = async (key: string) => {
8888
const [response, error] = await this.providerFiles.delete(instance, key);
8989
if (error) {
90-
this.logger.error(['removeData', error?.message, error?.stack]);
90+
// this.logger.error(['removeData', error?.message, error?.stack]);
9191
return;
9292
}
9393

0 commit comments

Comments
 (0)