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 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('模板已删除');
|
||||
|
||||
Reference in New Issue
Block a user