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:
@@ -30,6 +30,7 @@ import {
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@/components/ui/alert-dialog';
|
||||
import { fetchApi } from '@/lib/api-client';
|
||||
|
||||
const taskSchema = z.object({
|
||||
title: z.string().min(1, '计划标题不能为空'),
|
||||
@@ -145,7 +146,7 @@ export function TaskEditDialog({
|
||||
const url = task ? `/api/tasks/${task.id}` : '/api/tasks';
|
||||
const method = task ? 'PATCH' : 'POST';
|
||||
|
||||
const taskRes = await fetch(url, {
|
||||
const taskRes = await fetchApi(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -193,7 +194,7 @@ export function TaskEditDialog({
|
||||
const localSteps = steps.filter(s => s.id.startsWith('temp-'));
|
||||
if (localSteps.length > 0) {
|
||||
await Promise.all(localSteps.map(step =>
|
||||
fetch('/api/task-steps', {
|
||||
fetchApi('/api/task-steps', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -223,7 +224,7 @@ export function TaskEditDialog({
|
||||
|
||||
try {
|
||||
setIsDeletingTask(true);
|
||||
await fetch(`/api/tasks/${task.id}`, { method: 'DELETE' });
|
||||
await fetchApi(`/api/tasks/${task.id}`, { method: 'DELETE' });
|
||||
toast.success('计划已删除');
|
||||
onSuccess();
|
||||
onOpenChange(false);
|
||||
@@ -241,7 +242,7 @@ export function TaskEditDialog({
|
||||
// If task exists, save step to DB immediately
|
||||
if (task) {
|
||||
try {
|
||||
const res = await fetch('/api/task-steps', {
|
||||
const res = await fetchApi('/api/task-steps', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -295,7 +296,7 @@ export function TaskEditDialog({
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/task-steps', {
|
||||
const res = await fetchApi('/api/task-steps', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -320,7 +321,7 @@ export function TaskEditDialog({
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/task-steps?id=${stepId}`, { method: 'DELETE' });
|
||||
const res = await fetchApi(`/api/task-steps?id=${stepId}`, { method: 'DELETE' });
|
||||
if (res.ok) {
|
||||
setSteps(steps.filter(s => s.id !== stepId));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user