specify custom api url for client

This commit is contained in:
Guillaume Dorce 2022-11-04 15:35:48 +01:00
parent dd07580943
commit 0397efba8a
4 changed files with 21 additions and 11 deletions

View File

@ -1,8 +1,9 @@
import { Cookies } from "react-cookie";
import { api } from "../main";
const likePost = async (id: string) => {
const token = new Cookies().get('token');
const response = await fetch(`/api/posts/like/${id}`, {
const response = await fetch(`${api}/posts/like/${id}`, {
method: 'PUT',
mode: 'cors',
headers: {
@ -19,7 +20,7 @@ const likePost = async (id: string) => {
const unlikePost = async (id: string) => {
const token = new Cookies().get('token');
const response = await fetch(`/api/posts/unlike/${id}`, {
const response = await fetch(`${api}/posts/unlike/${id}`, {
method: 'PUT',
mode: 'cors',
headers: {

View File

@ -1,8 +1,9 @@
import { Cookies } from 'react-cookie';
import { api } from '../main';
const getMessages = async () => {
const token = new Cookies().get('token');
const response = await fetch('/api/posts', {
const response = await fetch(api + '/posts', {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
@ -17,7 +18,7 @@ const getMessages = async () => {
const newMessage = async (data: FormData) => {
const token = new Cookies().get('token');
const response = await fetch('/api/posts/new', {
const response = await fetch(api + '/posts/new', {
method: 'POST',
body: data,
mode: 'cors',
@ -30,7 +31,7 @@ const newMessage = async (data: FormData) => {
const deleteMessage = async (id: string) => {
const token = new Cookies().get('token');
const response = await fetch(`/api/posts/delete/${id}`, {
const response = await fetch(`${api}/posts/delete/${id}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${token}`,
@ -41,7 +42,7 @@ const deleteMessage = async (id: string) => {
const editMessage = async (id: string, data: FormData) => {
const token = new Cookies().get('token');
const response = await fetch(`/api/posts/edit/${id}`, {
const response = await fetch(`${api}/posts/edit/${id}`, {
method: 'PUT',
body: data,
mode: 'cors',

View File

@ -1,9 +1,10 @@
import { Cookies } from 'react-cookie';
import { api } from '../main';
const getMeInfo = async () => {
const token = new Cookies().get('token');
const response = await fetch('/api/me', {
const response = await fetch(api + '/me', {
method: 'GET',
mode: 'cors',
headers: {
@ -21,7 +22,7 @@ const getMeInfo = async () => {
const login = async ({ email, password }: { email: string; password: string }) => {
const token = new Cookies().get('token');
const response = await fetch('/api/auth/login', {
const response = await fetch(api + '/auth/login', {
method: 'POST',
body: JSON.stringify({ email, password }),
mode: 'cors',
@ -51,7 +52,7 @@ const signup = async (formData: FormData) => {
throw 'Passwords do not match';
}
const response = await fetch('/api/auth/signup', {
const response = await fetch(api + '/auth/signup', {
method: 'POST',
body: JSON.stringify(form),
mode: 'cors',
@ -69,7 +70,7 @@ const signup = async (formData: FormData) => {
export const giveUserRights = async (userId: string, role: string) => {
const token = new Cookies().get('token');
const response = await fetch(`/api/users/${userId}/roles`, {
const response = await fetch(`${api}/users/${userId}/roles`, {
method: 'POST',
mode: 'cors',
headers: {
@ -117,7 +118,7 @@ export const changeUserInfo = async (userId: string, formData: FormData) => {
}
}
const response = await fetch(`/api/users/${userId}`, {
const response = await fetch(`${api}/users/${userId}`, {
method: 'PUT',
mode: 'cors',
headers: {

View File

@ -3,3 +3,10 @@ import App from './App';
import './index.css';
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(<App />);
let api = '/api';
if (import.meta.env.VITE_API_URL) {
api = import.meta.env.VITE_API_URL;
}
export { api };