set BASE URL

This commit is contained in:
monoid 2024-04-07 22:16:37 +09:00
parent 02f3cd9bd1
commit e0cdf51507
2 changed files with 12 additions and 5 deletions

View File

@ -1,4 +1,7 @@
export const BASE_API_URL = 'http://localhost:8080/';
export async function fetcher(url: string) { export async function fetcher(url: string) {
const res = await fetch(url); const u = new URL(url, BASE_API_URL);
const res = await fetch(u);
return res.json(); return res.json();
} }

View File

@ -1,4 +1,5 @@
import { atom, useAtomValue, setAtomValue } from "../lib/atom.ts"; import { atom, useAtomValue, setAtomValue } from "../lib/atom.ts";
import { BASE_API_URL } from "../hook/fetcher.ts";
type LoginLocalStorage = { type LoginLocalStorage = {
username: string; username: string;
@ -22,8 +23,9 @@ function getUserSessions() {
return null; return null;
} }
async function refresh() { export async function refresh() {
const res = await fetch("/user/refresh", { const u = new URL("/user/refresh", BASE_API_URL);
const res = await fetch(u, {
method: "POST", method: "POST",
}); });
if (res.status !== 200) throw new Error("Maybe Network Error"); if (res.status !== 200) throw new Error("Maybe Network Error");
@ -47,7 +49,8 @@ async function refresh() {
} }
export const doLogout = async () => { export const doLogout = async () => {
const req = await fetch("/api/user/logout", { const u = new URL("/user/refresh", BASE_API_URL);
const req = await fetch(u, {
method: "POST", method: "POST",
}); });
const setVal = setAtomValue(userLoginStateAtom); const setVal = setAtomValue(userLoginStateAtom);
@ -76,7 +79,8 @@ export const doLogin = async (userLoginInfo: {
username: string; username: string;
password: string; password: string;
}): Promise<string | LoginLocalStorage> => { }): Promise<string | LoginLocalStorage> => {
const res = await fetch("/api/user/login", { const u = new URL("/user/refresh", BASE_API_URL);
const res = await fetch(u, {
method: "POST", method: "POST",
body: JSON.stringify(userLoginInfo), body: JSON.stringify(userLoginInfo),
headers: { "content-type": "application/json" }, headers: { "content-type": "application/json" },