refactor: 重构并修复博客相关API
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Textarea } from "@/components/ui/textarea";
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
import { BlogApi } from "@/lib/api";
|
import { BlogAPI } from "@/lib/api/client";
|
||||||
import { BlogComment } from "@/lib/types/blogComment";
|
import { BlogComment } from "@/lib/types/blogComment";
|
||||||
import { Send, Undo2 } from "lucide-react";
|
import { Send, Undo2 } from "lucide-react";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
@@ -34,7 +34,7 @@ export function BlogCommentTool({ blogId, onInsertComment, replayTarget, handleC
|
|||||||
if (comment.trim().length === 0) return;
|
if (comment.trim().length === 0) return;
|
||||||
|
|
||||||
try {
|
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) {
|
if (res) {
|
||||||
toast.success('发布成功');
|
toast.success('发布成功');
|
||||||
setComment('');
|
setComment('');
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
'use client';
|
'use client';
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import { BlogCommentTool } from "./BlogCommentTool";
|
import { BlogCommentTool } from "./BlogCommentTool";
|
||||||
import { BlogApi } from "@/lib/api";
|
|
||||||
import { BlogComment } from "@/lib/types/blogComment";
|
import { BlogComment } from "@/lib/types/blogComment";
|
||||||
import { useState } from "react";
|
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 }) {
|
export function BlogComments({ blogId }: { blogId: string }) {
|
||||||
const { data, mutate } = useSWR(
|
const { data, mutate } = useSWR(
|
||||||
`/api/blog/${blogId}/comments`,
|
`/api/blog/${blogId}/comments`,
|
||||||
() => BlogApi.getComments(blogId),
|
() => BlogAPI.getComments(blogId),
|
||||||
)
|
)
|
||||||
|
|
||||||
const { user } = useUserMe();
|
const { user } = useUserStore();
|
||||||
|
|
||||||
const insertComment = async (newOne: BlogComment) => {
|
const insertComment = async (newOne: BlogComment) => {
|
||||||
await mutate(
|
await mutate(
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { BlogApi } from "@/lib/api";
|
|
||||||
import { base62 } from "@/lib/utils";
|
import { base62 } from "@/lib/utils";
|
||||||
import { useParams, useSearchParams } from "next/navigation";
|
import { useParams, useSearchParams } from "next/navigation";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
@@ -14,6 +13,7 @@ import rehypeRaw from 'rehype-raw'
|
|||||||
import { Skeleton } from "@/components/ui/skeleton";
|
import { Skeleton } from "@/components/ui/skeleton";
|
||||||
import { BlogComments } from "./components/BlogComments";
|
import { BlogComments } from "./components/BlogComments";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
|
import { BlogAPI } from "@/lib/api/client";
|
||||||
|
|
||||||
export default function Blog() {
|
export default function Blog() {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
@@ -31,9 +31,7 @@ export default function Blog() {
|
|||||||
const password = searchParams.get('p');
|
const password = searchParams.get('p');
|
||||||
const { data, error, isLoading } = useSWR(
|
const { data, error, isLoading } = useSWR(
|
||||||
`/api/blog/${id}`,
|
`/api/blog/${id}`,
|
||||||
() => BlogApi.get(id, {
|
() => BlogAPI.getBlog(id, password || undefined),
|
||||||
password: password || undefined,
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,41 +1,41 @@
|
|||||||
import { UserApi } from "@/lib/api";
|
// import { UserAPI } from "@/lib/api/client";
|
||||||
import useSWR from "swr";
|
// import useSWR from "swr";
|
||||||
|
|
||||||
export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) {
|
// export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) {
|
||||||
const isClientSide = typeof window !== 'undefined';
|
// const isClientSide = typeof window !== 'undefined';
|
||||||
|
|
||||||
const { data: user, isLoading, error } = useSWR(
|
// const { data: user, isLoading, error } = useSWR(
|
||||||
'/api/user/me',
|
// '/api/user/me',
|
||||||
async () => {
|
// async () => {
|
||||||
if (isClientSide && !localStorage.getItem('token')) {
|
// if (isClientSide && !localStorage.getItem('token')) {
|
||||||
throw Object.assign(new Error('未登录'), { statusCode: -1 });
|
// throw Object.assign(new Error('未登录'), { statusCode: -1 });
|
||||||
}
|
// }
|
||||||
return await UserApi.me();
|
// return UserAPI.me();
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
onError: (error) => {
|
// onError: (error) => {
|
||||||
if (error.statusCode === 401) {
|
// if (error.statusCode === 401) {
|
||||||
if (isClientSide) {
|
// if (isClientSide) {
|
||||||
localStorage.removeItem('token');
|
// localStorage.removeItem('token');
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
onError?.(error);
|
// onError?.(error);
|
||||||
},
|
// },
|
||||||
revalidateIfStale: false,
|
// revalidateIfStale: false,
|
||||||
revalidateOnFocus: false,
|
// revalidateOnFocus: false,
|
||||||
shouldRetryOnError: (err) => {
|
// shouldRetryOnError: (err) => {
|
||||||
if ([-1, 401].includes(err.statusCode)) {
|
// if ([-1, 401].includes(err.statusCode)) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
return true;
|
// return true;
|
||||||
},
|
// },
|
||||||
}
|
// }
|
||||||
);
|
// );
|
||||||
|
|
||||||
return {
|
// return {
|
||||||
user,
|
// user,
|
||||||
isLoading,
|
// isLoading,
|
||||||
error
|
// error
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
@@ -49,4 +49,5 @@ export * as AuthAPI from './endpoints/auth.client'
|
|||||||
export * as UserAPI from './endpoints/user.client'
|
export * as UserAPI from './endpoints/user.client'
|
||||||
export * as SmsAPI from './endpoints/sms.client'
|
export * as SmsAPI from './endpoints/sms.client'
|
||||||
export * as AdminAPI from './endpoints/admin.client'
|
export * as AdminAPI from './endpoints/admin.client'
|
||||||
export * as OSSAPI from './endpoints/oss.client'
|
export * as OSSAPI from './endpoints/oss.client'
|
||||||
|
export * as BlogAPI from './endpoints/blog.client'
|
||||||
25
apps/frontend/lib/api/endpoints/blog.client.ts
Normal file
25
apps/frontend/lib/api/endpoints/blog.client.ts
Normal file
@@ -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<BlogComment[]>(`/api/blog/${id}/comments`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createComment(blogId: string, content: string, parentId?: string) {
|
||||||
|
return clientFetch<BlogComment>(`/api/blog/${blogId}/comment`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({
|
||||||
|
content,
|
||||||
|
parentId: parentId || null,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user