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:
@@ -35,6 +35,7 @@ import {
|
||||
User as UserIcon,
|
||||
} from 'lucide-react';
|
||||
import { TaskEditDialog } from '@/components/task-edit-dialog';
|
||||
import { fetchApi } from '@/lib/api-client';
|
||||
|
||||
interface ProjectWithRelations extends Project {
|
||||
owner: User | null;
|
||||
@@ -100,10 +101,10 @@ export default function ProjectDetailPage({ projectId }: { projectId: string })
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
const [projectRes, objectivesRes, krsRes, tasksRes] = await Promise.all([
|
||||
fetch(`/api/projects/${projectId}`),
|
||||
fetch(`/api/objectives?project_id=${projectId}`),
|
||||
fetch(`/api/krs?project_id=${projectId}`),
|
||||
fetch(`/api/tasks?project_id=${projectId}`),
|
||||
fetchApi(`/api/projects/${projectId}`),
|
||||
fetchApi(`/api/objectives?project_id=${projectId}`),
|
||||
fetchApi(`/api/krs?project_id=${projectId}`),
|
||||
fetchApi(`/api/tasks?project_id=${projectId}`),
|
||||
]);
|
||||
|
||||
if (projectRes.status === 403) {
|
||||
@@ -156,7 +157,7 @@ export default function ProjectDetailPage({ projectId }: { projectId: string })
|
||||
const handleDeleteProject = async () => {
|
||||
setIsDeleting(true);
|
||||
try {
|
||||
const res = await fetch(`/api/projects/${projectId}`, { method: 'DELETE' });
|
||||
const res = await fetchApi(`/api/projects/${projectId}`, { method: 'DELETE' });
|
||||
if (!res.ok) throw new Error('删除失败');
|
||||
toast.success('项目已删除');
|
||||
router.push('/projects');
|
||||
@@ -172,7 +173,7 @@ export default function ProjectDetailPage({ projectId }: { projectId: string })
|
||||
setIsEditing(true);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/projects/${projectId}`, {
|
||||
const res = await fetchApi(`/api/projects/${projectId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(editForm),
|
||||
@@ -199,7 +200,7 @@ export default function ProjectDetailPage({ projectId }: { projectId: string })
|
||||
if (!deleteObjectiveId) return;
|
||||
setIsDeletingObjective(true);
|
||||
try {
|
||||
const res = await fetch(`/api/objectives/${deleteObjectiveId}`, { method: 'DELETE' });
|
||||
const res = await fetchApi(`/api/objectives/${deleteObjectiveId}`, { method: 'DELETE' });
|
||||
if (!res.ok) throw new Error('删除失败');
|
||||
toast.success('目标已删除');
|
||||
fetchData();
|
||||
@@ -220,7 +221,7 @@ export default function ProjectDetailPage({ projectId }: { projectId: string })
|
||||
const formData = new FormData(e.currentTarget);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/objectives/${editingObjective.id}`, {
|
||||
const res = await fetchApi(`/api/objectives/${editingObjective.id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -246,7 +247,7 @@ export default function ProjectDetailPage({ projectId }: { projectId: string })
|
||||
|
||||
const handleTaskStatusChange = async (taskId: string, newStatus: string) => {
|
||||
try {
|
||||
const res = await fetch(`/api/tasks/${taskId}`, {
|
||||
const res = await fetchApi(`/api/tasks/${taskId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ status: newStatus }),
|
||||
|
||||
@@ -18,6 +18,7 @@ import { PERIOD_TYPES, DIRECTION_LABELS } from '@/lib/constants';
|
||||
import type { Objective, Metric, Template } from '@/types/database';
|
||||
import { ArrowLeft, Loader2 } from 'lucide-react';
|
||||
import { getErrorMessage } from '@/lib/error-utils';
|
||||
import { fetchApi } from '@/lib/api-client';
|
||||
|
||||
const formSchema = z.object({
|
||||
title: z.string().min(1, 'KR标题不能为空'),
|
||||
@@ -72,9 +73,9 @@ export default function NewKRPage() {
|
||||
useEffect(() => {
|
||||
async function fetchData() {
|
||||
const [objRes, metricsRes, templatesRes] = await Promise.all([
|
||||
fetch(`/api/objectives?project_id=${projectId}`),
|
||||
fetch('/api/metrics?active_only=true'),
|
||||
fetch('/api/templates?active_only=true'),
|
||||
fetchApi(`/api/objectives?project_id=${projectId}`),
|
||||
fetchApi('/api/metrics?active_only=true'),
|
||||
fetchApi('/api/templates?active_only=true'),
|
||||
]);
|
||||
|
||||
if (objRes.ok) {
|
||||
@@ -156,7 +157,7 @@ export default function NewKRPage() {
|
||||
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
const res = await fetch('/api/krs', {
|
||||
const res = await fetchApi('/api/krs', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
|
||||
@@ -15,6 +15,7 @@ import { useUser } from '@/components/user-context';
|
||||
import { toast } from 'sonner';
|
||||
import { ArrowLeft, Loader2 } from 'lucide-react';
|
||||
import { getErrorMessage } from '@/lib/error-utils';
|
||||
import { fetchApi } from '@/lib/api-client';
|
||||
|
||||
const formSchema = z.object({
|
||||
title: z.string().min(1, '目标标题不能为空'),
|
||||
@@ -54,7 +55,7 @@ export default function NewObjectivePage() {
|
||||
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
const res = await fetch('/api/objectives', {
|
||||
const res = await fetchApi('/api/objectives', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
|
||||
@@ -18,6 +18,7 @@ import { toast } from 'sonner';
|
||||
import type { User } from '@/types/database';
|
||||
import { ArrowLeft, Loader2 } from 'lucide-react';
|
||||
import { getErrorMessage } from '@/lib/error-utils';
|
||||
import { fetchApi } from '@/lib/api-client';
|
||||
|
||||
const formSchema = z.object({
|
||||
name: z.string().min(1, '项目名称不能为空'),
|
||||
@@ -48,12 +49,12 @@ export default function NewProjectPage() {
|
||||
const onSubmit = async (values: FormValues) => {
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
const res = await fetch('/api/projects', {
|
||||
const res = await fetchApi('/api/projects', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
...values,
|
||||
owner_id: currentUser?.id,
|
||||
created_by: currentUser?.id,
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import { StatusBadge, StatusDot } from '@/components/status-badge';
|
||||
import { useUser } from '@/components/user-context';
|
||||
import type { Project, User } from '@/types/database';
|
||||
import { Plus, Users, Target, ArrowRight } from 'lucide-react';
|
||||
import { fetchApi } from '@/lib/api-client';
|
||||
|
||||
interface ProjectWithRelations extends Project {
|
||||
owner: User | null;
|
||||
@@ -22,7 +23,7 @@ export default function ProjectsPage() {
|
||||
useEffect(() => {
|
||||
async function fetchProjects() {
|
||||
try {
|
||||
const res = await fetch('/api/projects');
|
||||
const res = await fetchApi('/api/projects');
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
setProjects(data);
|
||||
|
||||
Reference in New Issue
Block a user