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:
2026-01-28 15:54:19 +08:00
parent 7948b1afc7
commit fc9cfda29e
21 changed files with 118 additions and 65 deletions

View File

@@ -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('指标已删除');

View File

@@ -28,6 +28,7 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { fetchApi } from '@/lib/api-client';
const templateItemSchema = z.object({
title: z.string().min(1, '标题不能为空'),
@@ -75,7 +76,7 @@ export default function AdminTemplatesPage() {
const fetchTemplates = async () => {
try {
const res = await fetch('/api/templates');
const res = await fetchApi('/api/templates');
if (res.ok) {
const data = await res.json();
setTemplates(data);
@@ -97,7 +98,7 @@ export default function AdminTemplatesPage() {
const url = editingTemplate ? `/api/templates/${editingTemplate.id}` : '/api/templates';
const method = editingTemplate ? 'PATCH' : 'POST';
const res = await fetch(url, {
const res = await fetchApi(url, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(values),
@@ -133,7 +134,7 @@ export default function AdminTemplatesPage() {
const handleToggleActive = async (template: Template) => {
try {
const res = await fetch(`/api/templates/${template.id}`, {
const res = await fetchApi(`/api/templates/${template.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ is_active: !template.is_active }),
@@ -157,7 +158,7 @@ export default function AdminTemplatesPage() {
if (!deleteTemplateId) return;
setIsDeleting(true);
try {
const res = await fetch(`/api/templates/${deleteTemplateId}`, { method: 'DELETE' });
const res = await fetchApi(`/api/templates/${deleteTemplateId}`, { method: 'DELETE' });
if (!res.ok) throw new Error('删除失败');
toast.success('模板已删除');

View File

@@ -28,6 +28,7 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { fetchApi } from '@/lib/api-client';
const userSchema = z.object({
name: z.string().min(1, '名称不能为空'),
@@ -81,7 +82,7 @@ export default function AdminUsersPage() {
password: values.password ? values.password : undefined,
};
const res = await fetch(url, {
const res = await fetchApi(url, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
@@ -124,7 +125,7 @@ export default function AdminUsersPage() {
if (!deleteUserId) return;
setIsDeleting(true);
try {
const res = await fetch(`/api/users/${deleteUserId}`, { method: 'DELETE' });
const res = await fetchApi(`/api/users/${deleteUserId}`, { method: 'DELETE' });
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(data?.error || '删除失败');