feat: 添加 API 客户端以处理 basePath 并更新所有 fetch 调用
移除 next.config.js 中的 standalone 输出配置,添加 lib/api-client.ts 封装 fetch 请求, 自动为所有 API 调用添加配置的 basePath。更新所有页面和组件中的 fetch 调用以使用新的 fetchApi 函数, 确保在部署到子路径时 API 请求能正确工作。同时调整 tsconfig.json 的格式并修复项目创建时的字段名。
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
|
||||
import type { User } from '@/types/database';
|
||||
import { fetchApi } from '@/lib/api-client';
|
||||
|
||||
interface UserContextType {
|
||||
currentUser: User | null;
|
||||
@@ -26,7 +27,7 @@ export function UserProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
const refreshUsers = useCallback(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/users');
|
||||
const res = await fetchApi('/api/users');
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
setUsers(data);
|
||||
@@ -40,7 +41,7 @@ export function UserProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
const refreshMe = useCallback(async () => {
|
||||
try {
|
||||
const meRes = await fetch('/api/auth/me', { cache: 'no-store' });
|
||||
const meRes = await fetchApi('/api/auth/me', { cache: 'no-store' });
|
||||
const meData = await meRes.json().catch(() => ({}));
|
||||
const me = meData?.user as User | null | undefined;
|
||||
|
||||
@@ -109,7 +110,7 @@ export function UserProvider({ children }: { children: ReactNode }) {
|
||||
setCurrentUser(null);
|
||||
setIsAuthenticated(false);
|
||||
setUsers([]);
|
||||
fetch('/api/auth/logout', { method: 'POST' }).catch(() => {});
|
||||
fetchApi('/api/auth/logout', { method: 'POST' }).catch(() => {});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user