Skip to content

Commit

Permalink
Merge pull request #23 from fga-eps-mds/integrations
Browse files Browse the repository at this point in the history
feat: create integration beteween another back-ends
  • Loading branch information
mateusmaiamaia authored Feb 5, 2025
2 parents e626e80 + b256fb9 commit bac72f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VITE_API_BASE_URL=http://localhost:3000
VITE_API_BOOKS_URL=http://localhost:3001
VITE_API_RENTALS_URL=http://localhost:3003
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
VITE_API_BASE_URL=http://localhost:3000
VITE_API_BOOKS_URL=http://localhost:3001
VITE_API_RENTALS_URL=http://localhost:3003
2 changes: 2 additions & 0 deletions src/config/environment.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL;
export const API_BOOKS_URL = import.meta.env.VITE_API_BOOKS_URL;
export const API_RENTALS_URL = import.meta.env.VITE_API_RENTALS_URL;
23 changes: 13 additions & 10 deletions src/hooks/useApi/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import { useMemo } from "react"
import { API_BASE_URL } from "../../config/environment";
import { API_BASE_URL, API_BOOKS_URL, API_RENTALS_URL } from "../../config/environment";
import { User } from "../../interfaces/user";

const createApiInstance = (url: string) => {
Expand All @@ -22,15 +22,18 @@ const getDefaultErrorUseAPIMessage = (err: any) => {

const useApi = () => {
const api = useMemo(
() =>
createApiInstance(API_BASE_URL),
() => ({
base: createApiInstance(API_BASE_URL),
books: createApiInstance(API_BOOKS_URL),
rentals: createApiInstance(API_RENTALS_URL),
}),
[],
);

return {
getProfile: (token: string | null): Promise<{ data: User }> => {
return new Promise((resolve) => {
api
api.base
.get('/auth/profile', {
headers: {
Authorization: `Bearer ${token}`,
Expand All @@ -51,7 +54,7 @@ const useApi = () => {
refreshToken: string;
} }> => {
return new Promise((resolve) => {
api
api.base
.post('/auth/signup', data)
.then((res) => resolve(res))
.catch((err) => resolve(getDefaultErrorUseAPIMessage(err)));
Expand All @@ -65,7 +68,7 @@ const useApi = () => {
refreshToken: string;
} }> => {
return new Promise((resolve) => {
api
api.base
.post('/auth/signin', {
...data,
role: 'user',
Expand All @@ -76,15 +79,15 @@ const useApi = () => {
},
recoverPassword: (email: string): Promise<{ data: any }> => {
return new Promise((resolve) => {
api
api.base
.post(`/auth/recover-password`, { email })
.then((res) => resolve(res))
.catch((err) => resolve(getDefaultErrorUseAPIMessage(err)));
});
},
changePassword: (password: string, token: string): Promise<{ data: any }> => {
return new Promise((resolve) => {
api
api.base
.post(`/auth/change-password`, { password }, {
headers: {
Authorization: `Bearer ${token}`,
Expand All @@ -105,7 +108,7 @@ const useApi = () => {
id: string;
} }> => {
return new Promise((resolve) => {
api
api.base
.put('/users', data, {
headers: {
Authorization: `Bearer ${token}`,
Expand All @@ -119,7 +122,7 @@ const useApi = () => {
id: string;
} }> => {
return new Promise((resolve) => {
api
api.base
.delete('/users', {
headers: {
Authorization: `Bearer ${token}`,
Expand Down

0 comments on commit bac72f5

Please sign in to comment.