Skip to content

Commit 9bde4fb

Browse files
committed
Refatoração da API: adição de interceptores para configuração de headers e baseURL, criação de apiGlobal, e atualização das chamadas de API para utilizar a nova instância. Inclusão de novos tokens no enum TOKEN_ID e armazenamento de informações da instância no localStorage.
1 parent 555282b commit 9bde4fb

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

src/lib/queries/api.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@ export const api = axios.create({
77
});
88

99
api.interceptors.request.use(
10+
async (config) => {
11+
const apiUrl = getToken(TOKEN_ID.API_URL);
12+
if (apiUrl) {
13+
config.baseURL = apiUrl.toString();
14+
}
15+
16+
if (!config.headers.apiKey || config.headers.apiKey === "") {
17+
const token = getToken(TOKEN_ID.INSTANCE_TOKEN);
18+
if (token) {
19+
config.headers.apikey = `${token}`;
20+
}
21+
}
22+
23+
return config;
24+
},
25+
(error) => {
26+
return Promise.reject(error);
27+
},
28+
);
29+
30+
export const apiGlobal = axios.create({
31+
timeout: 30000,
32+
});
33+
34+
apiGlobal.interceptors.request.use(
1035
async (config) => {
1136
const apiUrl = getToken(TOKEN_ID.API_URL);
1237
if (apiUrl) {

src/lib/queries/instance/fetchInstance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery } from "@tanstack/react-query";
22

3-
import { api } from "../api";
3+
import { apiGlobal } from "../api";
44
import { UseQueryParams } from "../types";
55
import { FetchInstanceResponse } from "./types";
66

@@ -15,7 +15,7 @@ const queryKey = (params: Partial<IParams>) => [
1515
];
1616

1717
export const fetchInstance = async ({ instanceId }: IParams) => {
18-
const response = await api.get(`/instance/fetchInstances`, {
18+
const response = await apiGlobal.get(`/instance/fetchInstances`, {
1919
params: { instanceId },
2020
});
2121
if (Array.isArray(response.data)) {

src/lib/queries/instance/fetchInstances.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useQuery } from "@tanstack/react-query";
22

3-
import { api } from "../api";
3+
import { apiGlobal } from "../api";
44
import { UseQueryParams } from "../types";
55
import { FetchInstancesResponse } from "./types";
66

77
const queryKey = ["instance", "fetchInstances"];
88

99
export const fetchInstances = async () => {
10-
const response = await api.get(`/instance/fetchInstances`);
10+
const response = await apiGlobal.get(`/instance/fetchInstances`);
1111
return response.data;
1212
};
1313

src/lib/queries/instance/manageInstance.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { NewInstance, Settings } from "@/types/evolution.types";
22

3-
import { api } from "../api";
3+
import { api, apiGlobal } from "../api";
44
import { useManageMutation } from "../mutateQuery";
55

66
const createInstance = async (instance: NewInstance) => {
7-
const response = await api.post("/instance/create", instance);
7+
const response = await apiGlobal.post("/instance/create", instance);
88
return response.data;
99
};
1010

@@ -19,7 +19,7 @@ const logout = async (instanceName: string) => {
1919
};
2020

2121
const deleteInstance = async (instanceName: string) => {
22-
const response = await api.delete(`/instance/delete/${instanceName}`);
22+
const response = await apiGlobal.delete(`/instance/delete/${instanceName}`);
2323
return response.data;
2424
};
2525

src/lib/queries/token.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
export enum TOKEN_ID {
33
API_URL = "apiUrl",
44
TOKEN = "token",
5+
INSTANCE_ID = "instanceId",
6+
INSTANCE_NAME = "instanceName",
7+
INSTANCE_TOKEN = "instanceToken",
58
VERSION = "version",
69
FACEBOOK_APP_ID = "facebookAppId",
710
FACEBOOK_CONFIG_ID = "facebookConfigId",

src/pages/instance/DashboardInstance/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
22
import { CircleUser, MessageCircle, RefreshCw, UsersRound } from "lucide-react";
3-
import { useMemo, useState } from "react";
3+
import { useEffect, useMemo, useState } from "react";
44
import { useTranslation } from "react-i18next";
55
import QRCode from "react-qr-code";
66

@@ -42,6 +42,14 @@ function DashboardInstance() {
4242
const { connect, logout, restart } = useManageInstance();
4343
const { instance, reloadInstance } = useInstance();
4444

45+
useEffect(() => {
46+
if (instance) {
47+
localStorage.setItem(TOKEN_ID.INSTANCE_ID, instance.id);
48+
localStorage.setItem(TOKEN_ID.INSTANCE_NAME, instance.name);
49+
localStorage.setItem(TOKEN_ID.INSTANCE_TOKEN, instance.token);
50+
}
51+
}, [instance]);
52+
4553
const handleReload = async () => {
4654
await reloadInstance();
4755
};

0 commit comments

Comments
 (0)