Skip to content

Commit

Permalink
session worker compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Jun 3, 2024
1 parent dd123c6 commit 52230ed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "evolution-api",
"version": "1.8.0",
"version": "1.8.1",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js",
"scripts": {
Expand Down Expand Up @@ -49,7 +49,7 @@
"amqplib": "^0.10.3",
"@aws-sdk/client-sqs": "^3.569.0",
"axios": "^1.6.5",
"@whiskeysockets/baileys": "6.7.2",
"@whiskeysockets/baileys": "6.7.4",
"class-validator": "^0.14.1",
"compression": "^1.7.4",
"cors": "^2.8.5",
Expand Down
40 changes: 26 additions & 14 deletions src/api/provider/sessions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { execSync } from 'child_process';

import { ConfigService, ProviderSession } from '../../config/env.config';
import { Auth, ConfigService, ProviderSession } from '../../config/env.config';
import { Logger } from '../../config/logger.config';

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

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

private baseUrl: string;
private globalApiToken: string;

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

Expand All @@ -24,14 +26,14 @@ export class ProviderFiles {

public async onModuleInit() {
if (this.config.ENABLED) {
const client = axios.create({
baseURL: this.baseUrl,
});
const url = `http://${this.config.HOST}:${this.config.PORT}`;
try {
const response = await client.options('/ping');
if (!response?.data?.pong) {
const response = await axios.options(url + '/ping');
if (response?.data != 'pong') {
throw new Error('Offline file provider.');
}

await axios.post(`${url}/session`, { group: this.config.PREFIX }, { headers: { apikey: this.globalApiToken } });
} catch (error) {
this.logger.error(['Failed to connect to the file server', error?.message, error?.stack]);
const pid = process.pid;
Expand All @@ -46,9 +48,13 @@ export class ProviderFiles {

public async create(instance: string): ResponseProvider {
try {
const response = await axios.post(`${this.baseUrl}`, {
instance,
});
const response = await axios.post(
`${this.baseUrl}`,
{
instance,
},
{ headers: { apikey: this.globalApiToken } },
);
return [{ status: response.status, data: response?.data }];
} catch (error) {
return [
Expand All @@ -63,7 +69,9 @@ export class ProviderFiles {

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

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

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

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

public async removeSession(instance: string): ResponseProvider {
try {
const response = await axios.delete(`${this.baseUrl}/${instance}`);
const response = await axios.delete(`${this.baseUrl}/${instance}`, { headers: { apikey: this.globalApiToken } });
return [{ status: response.status, data: response?.data }];
} catch (error) {
return [
Expand Down
6 changes: 3 additions & 3 deletions src/utils/use-multi-file-auth-state-provider-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class AuthStateProvider {
data: json,
});
if (error) {
this.logger.error(['writeData', error?.message, error?.stack]);
// this.logger.error(['writeData', error?.message, error?.stack]);
return;
}
return response;
Expand All @@ -76,7 +76,7 @@ export class AuthStateProvider {
const readData = async (key: string): Promise<any> => {
const [response, error] = await this.providerFiles.read(instance, key);
if (error) {
this.logger.error(['readData', error?.message, error?.stack]);
// this.logger.error(['readData', error?.message, error?.stack]);
return;
}
if (isNotEmpty(response?.data)) {
Expand All @@ -87,7 +87,7 @@ export class AuthStateProvider {
const removeData = async (key: string) => {
const [response, error] = await this.providerFiles.delete(instance, key);
if (error) {
this.logger.error(['removeData', error?.message, error?.stack]);
// this.logger.error(['removeData', error?.message, error?.stack]);
return;
}

Expand Down

0 comments on commit 52230ed

Please sign in to comment.