diff --git a/src/api/client.ts b/src/api/client.ts new file mode 100644 index 0000000..7b689dd --- /dev/null +++ b/src/api/client.ts @@ -0,0 +1,5 @@ +import axios from "axios"; + +export const apiClient = axios.create({ + baseURL: "/api", +}); diff --git a/src/components/header/header.tsx b/src/components/header/header.tsx index fe6f869..23f1f2a 100644 --- a/src/components/header/header.tsx +++ b/src/components/header/header.tsx @@ -1,7 +1,7 @@ -import axios from "axios"; import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; +import { apiClient } from "@/api/client"; import { LoginDialog } from "@/components/login-dialog"; import { Button } from "@/components/ui/button"; import { Me } from "@/types"; @@ -13,8 +13,8 @@ export function Header() { const [hasError, setHasError] = useState(false); useEffect(() => { - axios - .get<{ data: Me }>("/api/me") + apiClient + .get<{ data: Me }>("/me") .then((response) => setMe(response.data.data)) .catch(() => setHasError(true)) .finally(() => setIsLoadingMe(false)); @@ -22,7 +22,7 @@ export function Header() { async function logout() { setIsLoadingLogout(true); - await axios.post(`/api/logout`); + await apiClient.post("/logout"); setIsLoadingLogout(false); window.location.reload(); } diff --git a/src/components/login-dialog/login-dialog.tsx b/src/components/login-dialog/login-dialog.tsx index eb7ad2d..09184a2 100644 --- a/src/components/login-dialog/login-dialog.tsx +++ b/src/components/login-dialog/login-dialog.tsx @@ -1,6 +1,6 @@ -import axios from "axios"; import { useState } from "react"; +import { apiClient } from "@/api/client"; import { Button } from "@/components/ui/button"; import { Dialog, @@ -37,7 +37,7 @@ export function LoginDialog({ children }: LoginDialogProps) { const username = event.currentTarget.elements.username.value; const password = event.currentTarget.elements.password.value; - await axios.post(`/api/login`, { username, password }); + await apiClient.post("/login", { username, password }); setIsLoading(false); setOpen(false); diff --git a/src/components/shout/reply-dialog.tsx b/src/components/shout/reply-dialog.tsx index d966da5..1fe8cb3 100644 --- a/src/components/shout/reply-dialog.tsx +++ b/src/components/shout/reply-dialog.tsx @@ -1,6 +1,6 @@ -import axios from "axios"; import { useEffect, useState } from "react"; +import { apiClient } from "@/api/client"; import { LoginDialog } from "@/components/login-dialog"; import { Button } from "@/components/ui/button"; import { @@ -38,8 +38,8 @@ export function ReplyDialog({ children, shoutId }: ReplyDialogProps) { const [hasError, setHasError] = useState(false); useEffect(() => { - axios - .get<{ data: Me }>("/api/me") + apiClient + .get<{ data: Me }>("/me") .then((response) => setIsAuthenticated(Boolean(response.data.data))) .catch(() => setHasError(true)) .finally(() => setIsLoading(false)); @@ -59,17 +59,17 @@ export function ReplyDialog({ children, shoutId }: ReplyDialogProps) { if (files?.length) { const formData = new FormData(); formData.append("image", files[0]); - const imageResponse = await axios.post<{ data: Image }>( - "/api/image", + const imageResponse = await apiClient.post<{ data: Image }>( + "/image", formData ); imageId = imageResponse.data.data.id; } - const newShoutResponse = await axios.post<{ data: Shout }>(`/api/shout`, { + const newShoutResponse = await apiClient.post<{ data: Shout }>(`/shout`, { message, imageId, }); - await axios.post(`/api/shout/${shoutId}/reply`, { + await apiClient.post(`/shout/${shoutId}/reply`, { replyId: newShoutResponse.data.data.id, }); setOpen(false); diff --git a/src/pages/feed/feed.tsx b/src/pages/feed/feed.tsx index 521898b..c3194f8 100644 --- a/src/pages/feed/feed.tsx +++ b/src/pages/feed/feed.tsx @@ -1,6 +1,6 @@ -import axios from "axios"; import { useEffect, useState } from "react"; +import { apiClient } from "@/api/client"; import { LoadingView } from "@/components/loading"; import { ShoutList } from "@/components/shout-list"; import { FeedResponse, Image, User } from "@/types"; @@ -10,8 +10,8 @@ export function Feed() { const [hasError, setHasError] = useState(false); useEffect(() => { - axios - .get("/api/feed") + apiClient + .get("/feed") .then((response) => setFeed(response.data)) .catch(() => setHasError(true)); }, []); diff --git a/src/pages/user-profile/user-profile.tsx b/src/pages/user-profile/user-profile.tsx index dcf6ad7..0cb5ad3 100644 --- a/src/pages/user-profile/user-profile.tsx +++ b/src/pages/user-profile/user-profile.tsx @@ -1,7 +1,7 @@ -import axios from "axios"; import { useEffect, useState } from "react"; import { Navigate, useParams } from "react-router"; +import { apiClient } from "@/api/client"; import { LoadingSpinner } from "@/components/loading"; import { ShoutList } from "@/components/shout-list"; import { UserResponse, UserShoutsResponse } from "@/types"; @@ -16,13 +16,13 @@ export function UserProfile() { const [hasError, setHasError] = useState(false); useEffect(() => { - axios - .get(`/api/user/${handle}`) + apiClient + .get(`/user/${handle}`) .then((response) => setUser(response.data)) .catch(() => setHasError(true)); - axios - .get(`/api/user/${handle}/shouts`) + apiClient + .get(`/user/${handle}/shouts`) .then((response) => setUserShouts(response.data)) .catch(() => setHasError(true)); }, [handle]);