From b0502d4d462ad5f2845e2b4c04f0c83fbd9c0b0c Mon Sep 17 00:00:00 2001 From: tone Date: Fri, 19 Dec 2025 21:06:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E5=B9=B6?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=9A=E5=AE=A2=E7=9B=B8=E5=85=B3API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blog/[id]/components/BlogCommentTool.tsx | 4 +- .../blog/[id]/components/BlogComments.tsx | 8 +- .../(with-header-footer)/blog/[id]/page.tsx | 6 +- apps/frontend/hooks/user/use-user-me.ts | 74 +++++++++---------- apps/frontend/lib/api/client.ts | 3 +- .../frontend/lib/api/endpoints/blog.client.ts | 25 +++++++ 6 files changed, 72 insertions(+), 48 deletions(-) create mode 100644 apps/frontend/lib/api/endpoints/blog.client.ts diff --git a/apps/frontend/app/(with-header-footer)/blog/[id]/components/BlogCommentTool.tsx b/apps/frontend/app/(with-header-footer)/blog/[id]/components/BlogCommentTool.tsx index db73071..cf5aafa 100644 --- a/apps/frontend/app/(with-header-footer)/blog/[id]/components/BlogCommentTool.tsx +++ b/apps/frontend/app/(with-header-footer)/blog/[id]/components/BlogCommentTool.tsx @@ -2,7 +2,7 @@ import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; -import { BlogApi } from "@/lib/api"; +import { BlogAPI } from "@/lib/api/client"; import { BlogComment } from "@/lib/types/blogComment"; import { Send, Undo2 } from "lucide-react"; import { useEffect, useRef, useState } from "react"; @@ -34,7 +34,7 @@ export function BlogCommentTool({ blogId, onInsertComment, replayTarget, handleC if (comment.trim().length === 0) return; try { - const res = await BlogApi.createComment(blogId, comment, replayTarget ? replayTarget.id : undefined); + const res = await BlogAPI.createComment(blogId, comment, replayTarget ? replayTarget.id : undefined); if (res) { toast.success('发布成功'); setComment(''); diff --git a/apps/frontend/app/(with-header-footer)/blog/[id]/components/BlogComments.tsx b/apps/frontend/app/(with-header-footer)/blog/[id]/components/BlogComments.tsx index 4562868..60857df 100644 --- a/apps/frontend/app/(with-header-footer)/blog/[id]/components/BlogComments.tsx +++ b/apps/frontend/app/(with-header-footer)/blog/[id]/components/BlogComments.tsx @@ -1,18 +1,18 @@ 'use client'; import useSWR from "swr"; import { BlogCommentTool } from "./BlogCommentTool"; -import { BlogApi } from "@/lib/api"; import { BlogComment } from "@/lib/types/blogComment"; import { useState } from "react"; -import { useUserMe } from "@/hooks/user/use-user-me"; +import { BlogAPI } from "@/lib/api/client"; +import { useUserStore } from "@/store/useUserStore"; export function BlogComments({ blogId }: { blogId: string }) { const { data, mutate } = useSWR( `/api/blog/${blogId}/comments`, - () => BlogApi.getComments(blogId), + () => BlogAPI.getComments(blogId), ) - const { user } = useUserMe(); + const { user } = useUserStore(); const insertComment = async (newOne: BlogComment) => { await mutate( diff --git a/apps/frontend/app/(with-header-footer)/blog/[id]/page.tsx b/apps/frontend/app/(with-header-footer)/blog/[id]/page.tsx index 10b6ba6..30caff5 100644 --- a/apps/frontend/app/(with-header-footer)/blog/[id]/page.tsx +++ b/apps/frontend/app/(with-header-footer)/blog/[id]/page.tsx @@ -1,6 +1,5 @@ 'use client'; -import { BlogApi } from "@/lib/api"; import { base62 } from "@/lib/utils"; import { useParams, useSearchParams } from "next/navigation"; import useSWR from "swr"; @@ -14,6 +13,7 @@ import rehypeRaw from 'rehype-raw' import { Skeleton } from "@/components/ui/skeleton"; import { BlogComments } from "./components/BlogComments"; import Image from "next/image"; +import { BlogAPI } from "@/lib/api/client"; export default function Blog() { const params = useParams(); @@ -31,9 +31,7 @@ export default function Blog() { const password = searchParams.get('p'); const { data, error, isLoading } = useSWR( `/api/blog/${id}`, - () => BlogApi.get(id, { - password: password || undefined, - }), + () => BlogAPI.getBlog(id, password || undefined), ) return ( diff --git a/apps/frontend/hooks/user/use-user-me.ts b/apps/frontend/hooks/user/use-user-me.ts index b622e0a..d5bfa07 100644 --- a/apps/frontend/hooks/user/use-user-me.ts +++ b/apps/frontend/hooks/user/use-user-me.ts @@ -1,41 +1,41 @@ -import { UserApi } from "@/lib/api"; -import useSWR from "swr"; +// import { UserAPI } from "@/lib/api/client"; +// import useSWR from "swr"; -export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) { - const isClientSide = typeof window !== 'undefined'; +// export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) { +// const isClientSide = typeof window !== 'undefined'; - const { data: user, isLoading, error } = useSWR( - '/api/user/me', - async () => { - if (isClientSide && !localStorage.getItem('token')) { - throw Object.assign(new Error('未登录'), { statusCode: -1 }); - } - return await UserApi.me(); - }, - { - onError: (error) => { - if (error.statusCode === 401) { - if (isClientSide) { - localStorage.removeItem('token'); - } - } +// const { data: user, isLoading, error } = useSWR( +// '/api/user/me', +// async () => { +// if (isClientSide && !localStorage.getItem('token')) { +// throw Object.assign(new Error('未登录'), { statusCode: -1 }); +// } +// return UserAPI.me(); +// }, +// { +// onError: (error) => { +// if (error.statusCode === 401) { +// if (isClientSide) { +// localStorage.removeItem('token'); +// } +// } - onError?.(error); - }, - revalidateIfStale: false, - revalidateOnFocus: false, - shouldRetryOnError: (err) => { - if ([-1, 401].includes(err.statusCode)) { - return false; - } - return true; - }, - } - ); +// onError?.(error); +// }, +// revalidateIfStale: false, +// revalidateOnFocus: false, +// shouldRetryOnError: (err) => { +// if ([-1, 401].includes(err.statusCode)) { +// return false; +// } +// return true; +// }, +// } +// ); - return { - user, - isLoading, - error - } -} \ No newline at end of file +// return { +// user, +// isLoading, +// error +// } +// } \ No newline at end of file diff --git a/apps/frontend/lib/api/client.ts b/apps/frontend/lib/api/client.ts index cc57496..722f614 100644 --- a/apps/frontend/lib/api/client.ts +++ b/apps/frontend/lib/api/client.ts @@ -49,4 +49,5 @@ export * as AuthAPI from './endpoints/auth.client' export * as UserAPI from './endpoints/user.client' export * as SmsAPI from './endpoints/sms.client' export * as AdminAPI from './endpoints/admin.client' -export * as OSSAPI from './endpoints/oss.client' \ No newline at end of file +export * as OSSAPI from './endpoints/oss.client' +export * as BlogAPI from './endpoints/blog.client' \ No newline at end of file diff --git a/apps/frontend/lib/api/endpoints/blog.client.ts b/apps/frontend/lib/api/endpoints/blog.client.ts new file mode 100644 index 0000000..31eec1a --- /dev/null +++ b/apps/frontend/lib/api/endpoints/blog.client.ts @@ -0,0 +1,25 @@ +import { BlogComment } from "@/lib/types/blogComment"; +import { clientFetch } from "../client"; + +export async function getBlog(id: string, password?: string) { + return clientFetch<{ + id: string; + title: string; + createdAt: string; + content: string; + }>(`/api/blog/${id}` + (password ? `?p=${password}` : '')); +} + +export async function getComments(id: string) { + return clientFetch(`/api/blog/${id}/comments`); +} + +export async function createComment(blogId: string, content: string, parentId?: string) { + return clientFetch(`/api/blog/${blogId}/comment`, { + method: 'POST', + body: JSON.stringify({ + content, + parentId: parentId || null, + }), + }); +} \ No newline at end of file