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:
@@ -28,6 +28,7 @@ import {
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog';
|
||||
import { fetchApi } from '@/lib/api-client';
|
||||
|
||||
const metricSchema = z.object({
|
||||
name: z.string().min(1, '名称不能为空'),
|
||||
@@ -61,7 +62,7 @@ export default function AdminMetricsPage() {
|
||||
|
||||
const fetchMetrics = async () => {
|
||||
try {
|
||||
const res = await fetch('/api/metrics');
|
||||
const res = await fetchApi('/api/metrics');
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
setMetrics(data);
|
||||
@@ -83,7 +84,7 @@ export default function AdminMetricsPage() {
|
||||
const url = editingMetric ? `/api/metrics/${editingMetric.id}` : '/api/metrics';
|
||||
const method = editingMetric ? 'PATCH' : 'POST';
|
||||
|
||||
const res = await fetch(url, {
|
||||
const res = await fetchApi(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(values),
|
||||
@@ -119,7 +120,7 @@ export default function AdminMetricsPage() {
|
||||
|
||||
const handleToggleActive = async (metric: Metric) => {
|
||||
try {
|
||||
const res = await fetch(`/api/metrics/${metric.id}`, {
|
||||
const res = await fetchApi(`/api/metrics/${metric.id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ is_active: !metric.is_active }),
|
||||
@@ -143,7 +144,7 @@ export default function AdminMetricsPage() {
|
||||
if (!deleteMetricId) return;
|
||||
setIsDeleting(true);
|
||||
try {
|
||||
const res = await fetch(`/api/metrics/${deleteMetricId}`, { method: 'DELETE' });
|
||||
const res = await fetchApi(`/api/metrics/${deleteMetricId}`, { method: 'DELETE' });
|
||||
if (!res.ok) throw new Error('删除失败');
|
||||
|
||||
toast.success('指标已删除');
|
||||
|
||||
Reference in New Issue
Block a user