refactor: 重构并修复博客相关API

This commit is contained in:
2025-12-19 21:06:19 +08:00
parent b69d64f726
commit b0502d4d46
6 changed files with 72 additions and 48 deletions

View File

@@ -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
}
}
// return {
// user,
// isLoading,
// error
// }
// }