♻️ refactor: standardize code formatting and improve readability across components

- Updated component files to use consistent single quotes for strings.
- Removed unnecessary newlines and adjusted indentation for better readability.
- Simplified conditional rendering and improved code structure in various components.
- Added ESLint configuration for better code quality and adherence to standards.
- Enhanced error handling in i18n request logic.
This commit is contained in:
2025-07-26 10:01:27 +08:00
parent e659de23fb
commit 99e291654d
29 changed files with 2214 additions and 355 deletions

View File

@ -1,35 +1,35 @@
import axios from "axios";
import { camelToSnakeObj, snakeToCamelObj } from "field-conv";
import axios from 'axios'
import { camelToSnakeObj, snakeToCamelObj } from 'field-conv'
export const BACKEND_URL = process.env.NEXT_PUBLIC_API_BASE_URL || "http://neo-blog-backend:8888";
export const BACKEND_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://neo-blog-backend:8888'
const isServer = typeof window === "undefined";
const isServer = typeof window === 'undefined'
const API_SUFFIX = "/api/v1";
const API_SUFFIX = '/api/v1'
const axiosClient = axios.create({
baseURL: isServer ? BACKEND_URL + API_SUFFIX : API_SUFFIX,
timeout: 10000,
});
})
axiosClient.interceptors.request.use((config) => {
if (config.data && typeof config.data === "object") {
config.data = camelToSnakeObj(config.data);
if (config.data && typeof config.data === 'object') {
config.data = camelToSnakeObj(config.data)
}
if (config.params && typeof config.params === "object") {
config.params = camelToSnakeObj(config.params);
if (config.params && typeof config.params === 'object') {
config.params = camelToSnakeObj(config.params)
}
return config;
});
return config
})
axiosClient.interceptors.response.use(
(response) => {
if (response.data && typeof response.data === "object") {
response.data = snakeToCamelObj(response.data);
if (response.data && typeof response.data === 'object') {
response.data = snakeToCamelObj(response.data)
}
return response;
return response
},
(error) => Promise.reject(error),
);
error => Promise.reject(error),
)
export default axiosClient;
export default axiosClient

View File

@ -1,10 +1,9 @@
import type { Label } from "@/models/label";
import type { BaseResponse } from "@/models/resp";
import axiosClient from "./client";
import type { Label } from '@/models/label'
import type { BaseResponse } from '@/models/resp'
import axiosClient from './client'
export async function listLabels(): Promise<BaseResponse<Label[] | null>> {
const res = await axiosClient.get<BaseResponse<Label[] | null>>("/label/list", {
});
return res.data;
}
const res = await axiosClient.get<BaseResponse<Label[] | null>>('/label/list', {
})
return res.data
}

View File

@ -1,41 +1,42 @@
import type { BaseResponse } from "@/models/resp";
import type { Post } from "@/models/post";
import axiosClient from "./client";
import type { Post } from '@/models/post'
import type { BaseResponse } from '@/models/resp'
import axiosClient from './client'
interface ListPostsParams {
page?: number;
size?: number;
orderBy?: string;
desc?: boolean;
keywords?: string;
page?: number
size?: number
orderBy?: string
desc?: boolean
keywords?: string
}
export async function getPostById(id: string): Promise<Post | null> {
console.log("Fetching post by ID:", id);
try {
const res = await axiosClient.get<BaseResponse<Post>>(`/post/p/19`);
return res.data.data;
} catch (error) {
console.error("Error fetching post by ID:", error);
return null;
}
console.log('Fetching post by ID:', id)
try {
const res = await axiosClient.get<BaseResponse<Post>>(`/post/p/19`)
return res.data.data
}
catch (error) {
console.error('Error fetching post by ID:', error)
return null
}
}
export async function listPosts({
page = 1,
size = 10,
orderBy = 'updated_at',
desc = false,
keywords = ''
page = 1,
size = 10,
orderBy = 'updated_at',
desc = false,
keywords = '',
}: ListPostsParams = {}): Promise<BaseResponse<Post[]>> {
const res = await axiosClient.get<BaseResponse<Post[]>>("/post/list", {
params: {
page,
size,
orderBy,
desc,
keywords
}
});
return res.data;
}
const res = await axiosClient.get<BaseResponse<Post[]>>('/post/list', {
params: {
page,
size,
orderBy,
desc,
keywords,
},
})
return res.data
}

View File

@ -1,60 +1,60 @@
import axiosClient from "./client";
import type { OidcConfig } from "@/models/oidc-config";
import type { User } from "@/models/user";
import type { BaseResponse } from "@/models/resp";
import type { OidcConfig } from '@/models/oidc-config'
import type { BaseResponse } from '@/models/resp'
import type { User } from '@/models/user'
import axiosClient from './client'
export interface LoginRequest {
username: string;
password: string;
rememberMe?: boolean; // 可以轻松添加新字段
captcha?: string;
username: string
password: string
rememberMe?: boolean // 可以轻松添加新字段
captcha?: string
}
export interface RegisterRequest {
username: string;
password: string;
nickname: string;
email: string;
verificationCode?: string;
username: string
password: string
nickname: string
email: string
verificationCode?: string
}
export async function userLogin(
data: LoginRequest
): Promise<BaseResponse<{ token: string; user: User }>> {
const res = await axiosClient.post<BaseResponse<{ token: string; user: User }>>(
"/user/login",
data
);
return res.data;
data: LoginRequest,
): Promise<BaseResponse<{ token: string, user: User }>> {
const res = await axiosClient.post<BaseResponse<{ token: string, user: User }>>(
'/user/login',
data,
)
return res.data
}
export async function userRegister(
data: RegisterRequest
): Promise<BaseResponse<{ token: string; user: User }>> {
const res = await axiosClient.post<BaseResponse<{ token: string; user: User }>>(
"/user/register",
data
);
return res.data;
data: RegisterRequest,
): Promise<BaseResponse<{ token: string, user: User }>> {
const res = await axiosClient.post<BaseResponse<{ token: string, user: User }>>(
'/user/register',
data,
)
return res.data
}
export async function ListOidcConfigs(): Promise<BaseResponse<OidcConfig[]>> {
const res = await axiosClient.get<BaseResponse<OidcConfig[]>>(
"/user/oidc/list"
);
return res.data;
const res = await axiosClient.get<BaseResponse<OidcConfig[]>>(
'/user/oidc/list',
)
return res.data
}
export async function getLoginUser(token: string = ""): Promise<BaseResponse<User>> {
const res = await axiosClient.get<BaseResponse<User>>("/user/me", {
headers: {
Authorization: `Bearer ${token}`
}
});
return res.data;
export async function getLoginUser(token: string = ''): Promise<BaseResponse<User>> {
const res = await axiosClient.get<BaseResponse<User>>('/user/me', {
headers: {
Authorization: `Bearer ${token}`,
},
})
return res.data
}
export async function getUserById(id: number): Promise<BaseResponse<User>> {
const res = await axiosClient.get<BaseResponse<User>>(`/user/u/${id}`);
return res.data;
}
const res = await axiosClient.get<BaseResponse<User>>(`/user/u/${id}`)
return res.data
}