feat:简单的分类页面

This commit is contained in:
2025-08-14 23:58:53 +08:00
parent 870f95aef0
commit 0b562a810b
13 changed files with 1261 additions and 1188 deletions

View File

@@ -11,11 +11,12 @@
"generate-client": "openapi-ts"
},
"dependencies": {
"@chakra-ui/react": "^3.8.0",
"@chakra-ui/react": "^3.24.2",
"@emotion/react": "^11.14.0",
"@tanstack/react-query": "^5.28.14",
"@tanstack/react-query": "^5.85.3",
"@tanstack/react-query-devtools": "^5.74.9",
"@tanstack/react-router": "1.19.1",
"@tanstack/react-router": "^1.131.10",
"antd": "^5.27.0",
"axios": "1.9.0",
"form-data": "4.0.2",
"next-themes": "^0.4.6",
@@ -23,7 +24,8 @@
"react-dom": "^18.2.0",
"react-error-boundary": "^5.0.0",
"react-hook-form": "7.49.3",
"react-icons": "^5.5.0"
"react-icons": "^5.5.0",
"zod": "^4.0.17"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",

View File

@@ -1,25 +1,21 @@
import type { ApiRequestOptions } from "./ApiRequestOptions"
import type { ApiResult } from "./ApiResult"
import type { ApiRequestOptions } from './ApiRequestOptions';
import type { ApiResult } from './ApiResult';
export class ApiError extends Error {
public readonly url: string
public readonly status: number
public readonly statusText: string
public readonly body: unknown
public readonly request: ApiRequestOptions
public readonly url: string;
public readonly status: number;
public readonly statusText: string;
public readonly body: unknown;
public readonly request: ApiRequestOptions;
constructor(
request: ApiRequestOptions,
response: ApiResult,
message: string,
) {
super(message)
constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
super(message);
this.name = "ApiError"
this.url = response.url
this.status = response.status
this.statusText = response.statusText
this.body = response.body
this.request = request
}
}
this.name = 'ApiError';
this.url = response.url;
this.status = response.status;
this.statusText = response.statusText;
this.body = response.body;
this.request = request;
}
}

View File

@@ -1,21 +1,21 @@
export type ApiRequestOptions<T = unknown> = {
readonly body?: any
readonly cookies?: Record<string, unknown>
readonly errors?: Record<number | string, string>
readonly formData?: Record<string, unknown> | any[] | Blob | File
readonly headers?: Record<string, unknown>
readonly mediaType?: string
readonly method:
| "DELETE"
| "GET"
| "HEAD"
| "OPTIONS"
| "PATCH"
| "POST"
| "PUT"
readonly path?: Record<string, unknown>
readonly query?: Record<string, unknown>
readonly responseHeader?: string
readonly responseTransformer?: (data: unknown) => Promise<T>
readonly url: string
}
readonly body?: any;
readonly cookies?: Record<string, unknown>;
readonly errors?: Record<number | string, string>;
readonly formData?: Record<string, unknown> | any[] | Blob | File;
readonly headers?: Record<string, unknown>;
readonly mediaType?: string;
readonly method:
| 'DELETE'
| 'GET'
| 'HEAD'
| 'OPTIONS'
| 'PATCH'
| 'POST'
| 'PUT';
readonly path?: Record<string, unknown>;
readonly query?: Record<string, unknown>;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly url: string;
};

View File

@@ -1,7 +1,7 @@
export type ApiResult<TData = any> = {
readonly body: TData
readonly ok: boolean
readonly status: number
readonly statusText: string
readonly url: string
}
readonly body: TData;
readonly ok: boolean;
readonly status: number;
readonly statusText: string;
readonly url: string;
};

View File

@@ -1,126 +1,126 @@
export class CancelError extends Error {
constructor(message: string) {
super(message)
this.name = "CancelError"
}
constructor(message: string) {
super(message);
this.name = 'CancelError';
}
public get isCancelled(): boolean {
return true
}
public get isCancelled(): boolean {
return true;
}
}
export interface OnCancel {
readonly isResolved: boolean
readonly isRejected: boolean
readonly isCancelled: boolean
readonly isResolved: boolean;
readonly isRejected: boolean;
readonly isCancelled: boolean;
(cancelHandler: () => void): void
(cancelHandler: () => void): void;
}
export class CancelablePromise<T> implements Promise<T> {
private _isResolved: boolean
private _isRejected: boolean
private _isCancelled: boolean
readonly cancelHandlers: (() => void)[]
readonly promise: Promise<T>
private _resolve?: (value: T | PromiseLike<T>) => void
private _reject?: (reason?: unknown) => void
private _isResolved: boolean;
private _isRejected: boolean;
private _isCancelled: boolean;
readonly cancelHandlers: (() => void)[];
readonly promise: Promise<T>;
private _resolve?: (value: T | PromiseLike<T>) => void;
private _reject?: (reason?: unknown) => void;
constructor(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: unknown) => void,
onCancel: OnCancel,
) => void,
) {
this._isResolved = false
this._isRejected = false
this._isCancelled = false
this.cancelHandlers = []
this.promise = new Promise<T>((resolve, reject) => {
this._resolve = resolve
this._reject = reject
constructor(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: unknown) => void,
onCancel: OnCancel
) => void
) {
this._isResolved = false;
this._isRejected = false;
this._isCancelled = false;
this.cancelHandlers = [];
this.promise = new Promise<T>((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
const onResolve = (value: T | PromiseLike<T>): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return
}
this._isResolved = true
if (this._resolve) this._resolve(value)
}
const onResolve = (value: T | PromiseLike<T>): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return;
}
this._isResolved = true;
if (this._resolve) this._resolve(value);
};
const onReject = (reason?: unknown): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return
}
this._isRejected = true
if (this._reject) this._reject(reason)
}
const onReject = (reason?: unknown): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return;
}
this._isRejected = true;
if (this._reject) this._reject(reason);
};
const onCancel = (cancelHandler: () => void): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return
}
this.cancelHandlers.push(cancelHandler)
}
const onCancel = (cancelHandler: () => void): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return;
}
this.cancelHandlers.push(cancelHandler);
};
Object.defineProperty(onCancel, "isResolved", {
get: (): boolean => this._isResolved,
})
Object.defineProperty(onCancel, 'isResolved', {
get: (): boolean => this._isResolved,
});
Object.defineProperty(onCancel, "isRejected", {
get: (): boolean => this._isRejected,
})
Object.defineProperty(onCancel, 'isRejected', {
get: (): boolean => this._isRejected,
});
Object.defineProperty(onCancel, "isCancelled", {
get: (): boolean => this._isCancelled,
})
Object.defineProperty(onCancel, 'isCancelled', {
get: (): boolean => this._isCancelled,
});
return executor(onResolve, onReject, onCancel as OnCancel)
})
}
return executor(onResolve, onReject, onCancel as OnCancel);
});
}
get [Symbol.toStringTag]() {
return "Cancellable Promise"
}
get [Symbol.toStringTag]() {
return "Cancellable Promise";
}
public then<TResult1 = T, TResult2 = never>(
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,
): Promise<TResult1 | TResult2> {
return this.promise.then(onFulfilled, onRejected)
}
public then<TResult1 = T, TResult2 = never>(
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this.promise.then(onFulfilled, onRejected);
}
public catch<TResult = never>(
onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null,
): Promise<T | TResult> {
return this.promise.catch(onRejected)
}
public catch<TResult = never>(
onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null
): Promise<T | TResult> {
return this.promise.catch(onRejected);
}
public finally(onFinally?: (() => void) | null): Promise<T> {
return this.promise.finally(onFinally)
}
public finally(onFinally?: (() => void) | null): Promise<T> {
return this.promise.finally(onFinally);
}
public cancel(): void {
if (this._isResolved || this._isRejected || this._isCancelled) {
return
}
this._isCancelled = true
if (this.cancelHandlers.length) {
try {
for (const cancelHandler of this.cancelHandlers) {
cancelHandler()
}
} catch (error) {
console.warn("Cancellation threw an error", error)
return
}
}
this.cancelHandlers.length = 0
if (this._reject) this._reject(new CancelError("Request aborted"))
}
public cancel(): void {
if (this._isResolved || this._isRejected || this._isCancelled) {
return;
}
this._isCancelled = true;
if (this.cancelHandlers.length) {
try {
for (const cancelHandler of this.cancelHandlers) {
cancelHandler();
}
} catch (error) {
console.warn('Cancellation threw an error', error);
return;
}
}
this.cancelHandlers.length = 0;
if (this._reject) this._reject(new CancelError('Request aborted'));
}
public get isCancelled(): boolean {
return this._isCancelled
}
}
public get isCancelled(): boolean {
return this._isCancelled;
}
}

View File

@@ -1,57 +1,57 @@
import type { AxiosRequestConfig, AxiosResponse } from "axios"
import type { ApiRequestOptions } from "./ApiRequestOptions"
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
import type { ApiRequestOptions } from './ApiRequestOptions';
type Headers = Record<string, string>
type Middleware<T> = (value: T) => T | Promise<T>
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>
type Headers = Record<string, string>;
type Middleware<T> = (value: T) => T | Promise<T>;
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>;
export class Interceptors<T> {
_fns: Middleware<T>[]
_fns: Middleware<T>[];
constructor() {
this._fns = []
this._fns = [];
}
eject(fn: Middleware<T>): void {
const index = this._fns.indexOf(fn)
const index = this._fns.indexOf(fn);
if (index !== -1) {
this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)]
this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)];
}
}
use(fn: Middleware<T>): void {
this._fns = [...this._fns, fn]
this._fns = [...this._fns, fn];
}
}
export type OpenAPIConfig = {
BASE: string
CREDENTIALS: "include" | "omit" | "same-origin"
ENCODE_PATH?: ((path: string) => string) | undefined
HEADERS?: Headers | Resolver<Headers> | undefined
PASSWORD?: string | Resolver<string> | undefined
TOKEN?: string | Resolver<string> | undefined
USERNAME?: string | Resolver<string> | undefined
VERSION: string
WITH_CREDENTIALS: boolean
interceptors: {
request: Interceptors<AxiosRequestConfig>
response: Interceptors<AxiosResponse>
}
}
BASE: string;
CREDENTIALS: 'include' | 'omit' | 'same-origin';
ENCODE_PATH?: ((path: string) => string) | undefined;
HEADERS?: Headers | Resolver<Headers> | undefined;
PASSWORD?: string | Resolver<string> | undefined;
TOKEN?: string | Resolver<string> | undefined;
USERNAME?: string | Resolver<string> | undefined;
VERSION: string;
WITH_CREDENTIALS: boolean;
interceptors: {
request: Interceptors<AxiosRequestConfig>;
response: Interceptors<AxiosResponse>;
};
};
export const OpenAPI: OpenAPIConfig = {
BASE: "",
CREDENTIALS: "include",
ENCODE_PATH: undefined,
HEADERS: undefined,
PASSWORD: undefined,
TOKEN: undefined,
USERNAME: undefined,
VERSION: "0.1.0",
WITH_CREDENTIALS: false,
interceptors: {
request: new Interceptors(),
response: new Interceptors(),
},
}
BASE: '',
CREDENTIALS: 'include',
ENCODE_PATH: undefined,
HEADERS: undefined,
PASSWORD: undefined,
TOKEN: undefined,
USERNAME: undefined,
VERSION: '0.1.0',
WITH_CREDENTIALS: false,
interceptors: {
request: new Interceptors(),
response: new Interceptors(),
},
};

View File

@@ -1,325 +1,301 @@
import axios from "axios"
import type {
AxiosError,
AxiosRequestConfig,
AxiosResponse,
AxiosInstance,
} from "axios"
import axios from 'axios';
import type { AxiosError, AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
import { ApiError } from "./ApiError"
import type { ApiRequestOptions } from "./ApiRequestOptions"
import type { ApiResult } from "./ApiResult"
import { CancelablePromise } from "./CancelablePromise"
import type { OnCancel } from "./CancelablePromise"
import type { OpenAPIConfig } from "./OpenAPI"
import { ApiError } from './ApiError';
import type { ApiRequestOptions } from './ApiRequestOptions';
import type { ApiResult } from './ApiResult';
import { CancelablePromise } from './CancelablePromise';
import type { OnCancel } from './CancelablePromise';
import type { OpenAPIConfig } from './OpenAPI';
export const isString = (value: unknown): value is string => {
return typeof value === "string"
}
return typeof value === 'string';
};
export const isStringWithValue = (value: unknown): value is string => {
return isString(value) && value !== ""
}
return isString(value) && value !== '';
};
export const isBlob = (value: any): value is Blob => {
return value instanceof Blob
}
return value instanceof Blob;
};
export const isFormData = (value: unknown): value is FormData => {
return value instanceof FormData
}
return value instanceof FormData;
};
export const isSuccess = (status: number): boolean => {
return status >= 200 && status < 300
}
return status >= 200 && status < 300;
};
export const base64 = (str: string): string => {
try {
return btoa(str)
} catch (err) {
// @ts-ignore
return Buffer.from(str).toString("base64")
}
}
try {
return btoa(str);
} catch (err) {
// @ts-ignore
return Buffer.from(str).toString('base64');
}
};
export const getQueryString = (params: Record<string, unknown>): string => {
const qs: string[] = []
const qs: string[] = [];
const append = (key: string, value: unknown) => {
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
}
const append = (key: string, value: unknown) => {
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
};
const encodePair = (key: string, value: unknown) => {
if (value === undefined || value === null) {
return
}
const encodePair = (key: string, value: unknown) => {
if (value === undefined || value === null) {
return;
}
if (value instanceof Date) {
append(key, value.toISOString())
} else if (Array.isArray(value)) {
value.forEach((v) => encodePair(key, v))
} else if (typeof value === "object") {
Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v))
} else {
append(key, value)
}
}
if (value instanceof Date) {
append(key, value.toISOString());
} else if (Array.isArray(value)) {
value.forEach(v => encodePair(key, v));
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v));
} else {
append(key, value);
}
};
Object.entries(params).forEach(([key, value]) => encodePair(key, value))
Object.entries(params).forEach(([key, value]) => encodePair(key, value));
return qs.length ? `?${qs.join("&")}` : ""
}
return qs.length ? `?${qs.join('&')}` : '';
};
const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
const encoder = config.ENCODE_PATH || encodeURI
const encoder = config.ENCODE_PATH || encodeURI;
const path = options.url
.replace("{api-version}", config.VERSION)
.replace(/{(.*?)}/g, (substring: string, group: string) => {
if (options.path?.hasOwnProperty(group)) {
return encoder(String(options.path[group]))
}
return substring
})
const path = options.url
.replace('{api-version}', config.VERSION)
.replace(/{(.*?)}/g, (substring: string, group: string) => {
if (options.path?.hasOwnProperty(group)) {
return encoder(String(options.path[group]));
}
return substring;
});
const url = config.BASE + path
return options.query ? url + getQueryString(options.query) : url
}
const url = config.BASE + path;
return options.query ? url + getQueryString(options.query) : url;
};
export const getFormData = (
options: ApiRequestOptions,
): FormData | undefined => {
if (options.formData) {
const formData = new FormData()
export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
if (options.formData) {
const formData = new FormData();
const process = (key: string, value: unknown) => {
if (isString(value) || isBlob(value)) {
formData.append(key, value)
} else {
formData.append(key, JSON.stringify(value))
}
}
const process = (key: string, value: unknown) => {
if (isString(value) || isBlob(value)) {
formData.append(key, value);
} else {
formData.append(key, JSON.stringify(value));
}
};
Object.entries(options.formData)
.filter(([, value]) => value !== undefined && value !== null)
.forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach((v) => process(key, v))
} else {
process(key, value)
}
})
Object.entries(options.formData)
.filter(([, value]) => value !== undefined && value !== null)
.forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach(v => process(key, v));
} else {
process(key, value);
}
});
return formData
}
return undefined
}
return formData;
}
return undefined;
};
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>;
export const resolve = async <T>(
options: ApiRequestOptions<T>,
resolver?: T | Resolver<T>,
): Promise<T | undefined> => {
if (typeof resolver === "function") {
return (resolver as Resolver<T>)(options)
}
return resolver
}
export const resolve = async <T>(options: ApiRequestOptions<T>, resolver?: T | Resolver<T>): Promise<T | undefined> => {
if (typeof resolver === 'function') {
return (resolver as Resolver<T>)(options);
}
return resolver;
};
export const getHeaders = async <T>(
config: OpenAPIConfig,
options: ApiRequestOptions<T>,
): Promise<Record<string, string>> => {
const [token, username, password, additionalHeaders] = await Promise.all([
// @ts-ignore
resolve(options, config.TOKEN),
// @ts-ignore
resolve(options, config.USERNAME),
// @ts-ignore
resolve(options, config.PASSWORD),
// @ts-ignore
resolve(options, config.HEADERS),
])
export const getHeaders = async <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>): Promise<Record<string, string>> => {
const [token, username, password, additionalHeaders] = await Promise.all([
// @ts-ignore
resolve(options, config.TOKEN),
// @ts-ignore
resolve(options, config.USERNAME),
// @ts-ignore
resolve(options, config.PASSWORD),
// @ts-ignore
resolve(options, config.HEADERS),
]);
const headers = Object.entries({
Accept: "application/json",
...additionalHeaders,
...options.headers,
})
.filter(([, value]) => value !== undefined && value !== null)
.reduce(
(headers, [key, value]) => ({
...headers,
[key]: String(value),
}),
{} as Record<string, string>,
)
const headers = Object.entries({
Accept: 'application/json',
...additionalHeaders,
...options.headers,
})
.filter(([, value]) => value !== undefined && value !== null)
.reduce((headers, [key, value]) => ({
...headers,
[key]: String(value),
}), {} as Record<string, string>);
if (isStringWithValue(token)) {
headers["Authorization"] = `Bearer ${token}`
}
if (isStringWithValue(token)) {
headers['Authorization'] = `Bearer ${token}`;
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(`${username}:${password}`)
headers["Authorization"] = `Basic ${credentials}`
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(`${username}:${password}`);
headers['Authorization'] = `Basic ${credentials}`;
}
if (options.body !== undefined) {
if (options.mediaType) {
headers["Content-Type"] = options.mediaType
} else if (isBlob(options.body)) {
headers["Content-Type"] = options.body.type || "application/octet-stream"
} else if (isString(options.body)) {
headers["Content-Type"] = "text/plain"
} else if (!isFormData(options.body)) {
headers["Content-Type"] = "application/json"
}
} else if (options.formData !== undefined) {
if (options.mediaType) {
headers["Content-Type"] = options.mediaType
}
}
if (options.body !== undefined) {
if (options.mediaType) {
headers['Content-Type'] = options.mediaType;
} else if (isBlob(options.body)) {
headers['Content-Type'] = options.body.type || 'application/octet-stream';
} else if (isString(options.body)) {
headers['Content-Type'] = 'text/plain';
} else if (!isFormData(options.body)) {
headers['Content-Type'] = 'application/json';
}
} else if (options.formData !== undefined) {
if (options.mediaType) {
headers['Content-Type'] = options.mediaType;
}
}
return headers
}
return headers;
};
export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body) {
return options.body
}
return undefined
}
if (options.body) {
return options.body;
}
return undefined;
};
export const sendRequest = async <T>(
config: OpenAPIConfig,
options: ApiRequestOptions<T>,
url: string,
body: unknown,
formData: FormData | undefined,
headers: Record<string, string>,
onCancel: OnCancel,
axiosClient: AxiosInstance,
config: OpenAPIConfig,
options: ApiRequestOptions<T>,
url: string,
body: unknown,
formData: FormData | undefined,
headers: Record<string, string>,
onCancel: OnCancel,
axiosClient: AxiosInstance
): Promise<AxiosResponse<T>> => {
const controller = new AbortController()
const controller = new AbortController();
let requestConfig: AxiosRequestConfig = {
data: body ?? formData,
headers,
method: options.method,
signal: controller.signal,
url,
withCredentials: config.WITH_CREDENTIALS,
}
let requestConfig: AxiosRequestConfig = {
data: body ?? formData,
headers,
method: options.method,
signal: controller.signal,
url,
withCredentials: config.WITH_CREDENTIALS,
};
onCancel(() => controller.abort())
onCancel(() => controller.abort());
for (const fn of config.interceptors.request._fns) {
requestConfig = await fn(requestConfig)
}
for (const fn of config.interceptors.request._fns) {
requestConfig = await fn(requestConfig);
}
try {
return await axiosClient.request(requestConfig)
} catch (error) {
const axiosError = error as AxiosError<T>
if (axiosError.response) {
return axiosError.response
}
throw error
}
}
try {
return await axiosClient.request(requestConfig);
} catch (error) {
const axiosError = error as AxiosError<T>;
if (axiosError.response) {
return axiosError.response;
}
throw error;
}
};
export const getResponseHeader = (
response: AxiosResponse<unknown>,
responseHeader?: string,
): string | undefined => {
if (responseHeader) {
const content = response.headers[responseHeader]
if (isString(content)) {
return content
}
}
return undefined
}
export const getResponseHeader = (response: AxiosResponse<unknown>, responseHeader?: string): string | undefined => {
if (responseHeader) {
const content = response.headers[responseHeader];
if (isString(content)) {
return content;
}
}
return undefined;
};
export const getResponseBody = (response: AxiosResponse<unknown>): unknown => {
if (response.status !== 204) {
return response.data
}
return undefined
}
if (response.status !== 204) {
return response.data;
}
return undefined;
};
export const catchErrorCodes = (
options: ApiRequestOptions,
result: ApiResult,
): void => {
const errors: Record<number, string> = {
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Payload Too Large",
414: "URI Too Long",
415: "Unsupported Media Type",
416: "Range Not Satisfiable",
417: "Expectation Failed",
418: "Im a teapot",
421: "Misdirected Request",
422: "Unprocessable Content",
423: "Locked",
424: "Failed Dependency",
425: "Too Early",
426: "Upgrade Required",
428: "Precondition Required",
429: "Too Many Requests",
431: "Request Header Fields Too Large",
451: "Unavailable For Legal Reasons",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
505: "HTTP Version Not Supported",
506: "Variant Also Negotiates",
507: "Insufficient Storage",
508: "Loop Detected",
510: "Not Extended",
511: "Network Authentication Required",
...options.errors,
}
export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
const errors: Record<number, string> = {
400: 'Bad Request',
401: 'Unauthorized',
402: 'Payment Required',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Payload Too Large',
414: 'URI Too Long',
415: 'Unsupported Media Type',
416: 'Range Not Satisfiable',
417: 'Expectation Failed',
418: 'Im a teapot',
421: 'Misdirected Request',
422: 'Unprocessable Content',
423: 'Locked',
424: 'Failed Dependency',
425: 'Too Early',
426: 'Upgrade Required',
428: 'Precondition Required',
429: 'Too Many Requests',
431: 'Request Header Fields Too Large',
451: 'Unavailable For Legal Reasons',
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTP Version Not Supported',
506: 'Variant Also Negotiates',
507: 'Insufficient Storage',
508: 'Loop Detected',
510: 'Not Extended',
511: 'Network Authentication Required',
...options.errors,
}
const error = errors[result.status]
if (error) {
throw new ApiError(options, result, error)
}
const error = errors[result.status];
if (error) {
throw new ApiError(options, result, error);
}
if (!result.ok) {
const errorStatus = result.status ?? "unknown"
const errorStatusText = result.statusText ?? "unknown"
const errorBody = (() => {
try {
return JSON.stringify(result.body, null, 2)
} catch (e) {
return undefined
}
})()
if (!result.ok) {
const errorStatus = result.status ?? 'unknown';
const errorStatusText = result.statusText ?? 'unknown';
const errorBody = (() => {
try {
return JSON.stringify(result.body, null, 2);
} catch (e) {
return undefined;
}
})();
throw new ApiError(
options,
result,
`Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`,
)
}
}
throw new ApiError(options, result,
`Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`
);
}
};
/**
* Request method
@@ -329,59 +305,43 @@ export const catchErrorCodes = (
* @returns CancelablePromise<T>
* @throws ApiError
*/
export const request = <T>(
config: OpenAPIConfig,
options: ApiRequestOptions<T>,
axiosClient: AxiosInstance = axios,
): CancelablePromise<T> => {
return new CancelablePromise(async (resolve, reject, onCancel) => {
try {
const url = getUrl(config, options)
const formData = getFormData(options)
const body = getRequestBody(options)
const headers = await getHeaders(config, options)
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>, axiosClient: AxiosInstance = axios): CancelablePromise<T> => {
return new CancelablePromise(async (resolve, reject, onCancel) => {
try {
const url = getUrl(config, options);
const formData = getFormData(options);
const body = getRequestBody(options);
const headers = await getHeaders(config, options);
if (!onCancel.isCancelled) {
let response = await sendRequest<T>(
config,
options,
url,
body,
formData,
headers,
onCancel,
axiosClient,
)
if (!onCancel.isCancelled) {
let response = await sendRequest<T>(config, options, url, body, formData, headers, onCancel, axiosClient);
for (const fn of config.interceptors.response._fns) {
response = await fn(response)
}
for (const fn of config.interceptors.response._fns) {
response = await fn(response);
}
const responseBody = getResponseBody(response)
const responseHeader = getResponseHeader(
response,
options.responseHeader,
)
const responseBody = getResponseBody(response);
const responseHeader = getResponseHeader(response, options.responseHeader);
let transformedBody = responseBody
if (options.responseTransformer && isSuccess(response.status)) {
transformedBody = await options.responseTransformer(responseBody)
}
let transformedBody = responseBody;
if (options.responseTransformer && isSuccess(response.status)) {
transformedBody = await options.responseTransformer(responseBody)
}
const result: ApiResult = {
url,
ok: isSuccess(response.status),
status: response.status,
statusText: response.statusText,
body: responseHeader ?? transformedBody,
}
const result: ApiResult = {
url,
ok: isSuccess(response.status),
status: response.status,
statusText: response.statusText,
body: responseHeader ?? transformedBody,
};
catchErrorCodes(options, result)
catchErrorCodes(options, result);
resolve(result.body)
}
} catch (error) {
reject(error)
}
})
}
resolve(result.body);
}
} catch (error) {
reject(error);
}
});
};

View File

@@ -1,6 +1,6 @@
// This file is auto-generated by @hey-api/openapi-ts
export { ApiError } from "./core/ApiError"
export { CancelablePromise, CancelError } from "./core/CancelablePromise"
export { OpenAPI, type OpenAPIConfig } from "./core/OpenAPI"
export * from "./sdk.gen"
export * from "./types.gen"
export { ApiError } from './core/ApiError';
export { CancelablePromise, CancelError } from './core/CancelablePromise';
export { OpenAPI, type OpenAPIConfig } from './core/OpenAPI';
export * from './sdk.gen';
export * from './types.gen';

File diff suppressed because it is too large Load Diff

View File

@@ -1,234 +1,255 @@
// This file is auto-generated by @hey-api/openapi-ts
export type Body_login_login_access_token = {
grant_type?: string | null
username: string
password: string
scope?: string
client_id?: string | null
client_secret?: string | null
}
grant_type?: (string | null);
username: string;
password: string;
scope?: string;
client_id?: (string | null);
client_secret?: (string | null);
};
export type ClassifyRequest = {
text: string;
};
export type HTTPValidationError = {
detail?: Array<ValidationError>
}
detail?: Array<ValidationError>;
};
export type ItemCreate = {
title: string
description?: string | null
}
title: string;
description?: (string | null);
};
export type ItemPublic = {
title: string
description?: string | null
id: string
owner_id: string
}
title: string;
description?: (string | null);
id: string;
owner_id: string;
};
export type ItemsPublic = {
data: Array<ItemPublic>
count: number
}
data: Array<ItemPublic>;
count: number;
};
export type ItemUpdate = {
title?: string | null
description?: string | null
}
title?: (string | null);
description?: (string | null);
};
export type Message = {
message: string
}
message: string;
};
export type NewPassword = {
token: string
new_password: string
}
token: string;
new_password: string;
};
export type PrivateUserCreate = {
email: string
password: string
full_name: string
is_verified?: boolean
email: string;
password: string;
full_name: string;
is_verified?: boolean;
};
export type ClassifyNewsReturn = {
data:{
predict:string,
text:string
}
}
export type Token = {
access_token: string
token_type?: string
}
access_token: string;
token_type?: string;
};
export type UpdatePassword = {
current_password: string
new_password: string
}
current_password: string;
new_password: string;
};
export type UserCreate = {
email: string
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
password: string
}
email: string;
is_active?: boolean;
is_superuser?: boolean;
full_name?: (string | null);
password: string;
};
export type UserPublic = {
email: string
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
id: string
}
email: string;
is_active?: boolean;
is_superuser?: boolean;
full_name?: (string | null);
id: string;
};
export type UserRegister = {
email: string
password: string
full_name?: string | null
}
email: string;
password: string;
full_name?: (string | null);
};
export type UsersPublic = {
data: Array<UserPublic>
count: number
}
data: Array<UserPublic>;
count: number;
};
export type UserUpdate = {
email?: string | null
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
password?: string | null
}
email?: (string | null);
is_active?: boolean;
is_superuser?: boolean;
full_name?: (string | null);
password?: (string | null);
};
export type UserUpdateMe = {
full_name?: string | null
email?: string | null
}
full_name?: (string | null);
email?: (string | null);
};
export type ValidationError = {
loc: Array<string | number>
msg: string
type: string
}
loc: Array<(string | number)>;
msg: string;
type: string;
};
export type ItemsReadItemsData = {
limit?: number
skip?: number
}
limit?: number;
skip?: number;
};
export type ItemsReadItemsResponse = ItemsPublic
export type ItemsReadItemsResponse = (ItemsPublic);
export type ItemsCreateItemData = {
requestBody: ItemCreate
}
requestBody: ItemCreate;
};
export type ItemsCreateItemResponse = ItemPublic
export type ItemsCreateItemResponse = (ItemPublic);
export type ItemsReadItemData = {
id: string
}
id: string;
};
export type ItemsReadItemResponse = ItemPublic
export type ItemsReadItemResponse = (ItemPublic);
export type ItemsUpdateItemData = {
id: string
requestBody: ItemUpdate
}
id: string;
requestBody: ItemUpdate;
};
export type ItemsUpdateItemResponse = ItemPublic
export type ItemsUpdateItemResponse = (ItemPublic);
export type ItemsDeleteItemData = {
id: string
}
id: string;
};
export type ItemsDeleteItemResponse = Message
export type ItemsDeleteItemResponse = (Message);
export type LoginLoginAccessTokenData = {
formData: Body_login_login_access_token
}
formData: Body_login_login_access_token;
};
export type LoginLoginAccessTokenResponse = Token
export type LoginLoginAccessTokenResponse = (Token);
export type LoginTestTokenResponse = UserPublic
export type LoginTestTokenResponse = (UserPublic);
export type LoginRecoverPasswordData = {
email: string
}
email: string;
};
export type LoginRecoverPasswordResponse = Message
export type LoginRecoverPasswordResponse = (Message);
export type LoginResetPasswordData = {
requestBody: NewPassword
}
requestBody: NewPassword;
};
export type LoginResetPasswordResponse = Message
export type LoginResetPasswordResponse = (Message);
export type LoginRecoverPasswordHtmlContentData = {
email: string
}
email: string;
};
export type LoginRecoverPasswordHtmlContentResponse = string
export type LoginRecoverPasswordHtmlContentResponse = (string);
export type PrivateCreateUserData = {
requestBody: PrivateUserCreate
}
requestBody: PrivateUserCreate;
};
export type PrivateCreateUserResponse = UserPublic
export type PrivateCreateUserResponse = (UserPublic);
export type UsersTestUserResponse = (unknown);
export type UsersTestUser1Response = (unknown);
export type UsersReadUsersData = {
limit?: number
skip?: number
}
limit?: number;
skip?: number;
};
export type UsersReadUsersResponse = UsersPublic
export type UsersReadUsersResponse = (UsersPublic);
export type UsersCreateUserData = {
requestBody: UserCreate
}
requestBody: UserCreate;
};
export type UsersCreateUserResponse = UserPublic
export type UsersCreateUserResponse = (UserPublic);
export type UsersReadUserMeResponse = UserPublic
export type UsersReadUserMeResponse = (UserPublic);
export type UsersDeleteUserMeResponse = Message
export type UsersDeleteUserMeResponse = (Message);
export type UsersUpdateUserMeData = {
requestBody: UserUpdateMe
}
requestBody: UserUpdateMe;
};
export type UsersUpdateUserMeResponse = UserPublic
export type UsersUpdateUserMeResponse = (UserPublic);
export type UsersUpdatePasswordMeData = {
requestBody: UpdatePassword
}
requestBody: UpdatePassword;
};
export type UsersUpdatePasswordMeResponse = Message
export type UsersUpdatePasswordMeResponse = (Message);
export type UsersRegisterUserData = {
requestBody: UserRegister
}
requestBody: UserRegister;
};
export type UsersRegisterUserResponse = UserPublic
export type UsersRegisterUserResponse = (UserPublic);
export type UsersReadUserByIdData = {
userId: string
}
userId: string;
};
export type UsersReadUserByIdResponse = UserPublic
export type UsersReadUserByIdResponse = (UserPublic);
export type UsersUpdateUserData = {
requestBody: UserUpdate
userId: string
}
requestBody: UserUpdate;
userId: string;
};
export type UsersUpdateUserResponse = UserPublic
export type UsersUpdateUserResponse = (UserPublic);
export type UsersDeleteUserData = {
userId: string
}
userId: string;
};
export type UsersDeleteUserResponse = Message
export type UsersDeleteUserResponse = (Message);
export type UtilsTestEmailData = {
emailTo: string
}
emailTo: string;
};
export type UtilsTestEmailResponse = Message
export type UtilsTestEmailResponse = (Message);
export type UtilsHealthCheckResponse = boolean
export type UtilsHealthCheckResponse = (boolean);
export type UtilsBentomlClassifiyData = {
requestBody: ClassifyRequest;
};
export type UtilsBentomlClassifiyResponse = (ClassifyNewsReturn);

View File

@@ -1,10 +1,11 @@
import { Box, Flex, Icon, Text } from "@chakra-ui/react"
import { useQueryClient } from "@tanstack/react-query"
import { Link as RouterLink } from "@tanstack/react-router"
import { FiBriefcase, FiHome, FiSettings, FiUsers } from "react-icons/fi"
import { FiBriefcase, FiHome, FiSettings, FiUsers, FiCamera } from "react-icons/fi"
import type { IconType } from "react-icons/lib"
import type { UserPublic } from "@/client"
import { useEffect } from "react"
const items = [
{ icon: FiHome, title: "Dashboard", path: "/" },
@@ -27,8 +28,14 @@ const SidebarItems = ({ onClose }: SidebarItemsProps) => {
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
const finalItems: Item[] = currentUser?.is_superuser
? [...items, { icon: FiUsers, title: "Admin", path: "/admin" }]
: items
? [...items, { icon: FiUsers, title: "Admin", path: "/admin" }, {icon: FiCamera, title: "新闻分类", path: "/classifyNews"}]
: [...items, {icon: FiCamera, title: "新闻分类", path: "/classifyNews"}]
useEffect(()=> {
console.log("这个finalItem是那里来的", finalItems, currentUser)
}, [finalItems])
const listItems = finalItems.map(({ icon, title, path }) => (
<RouterLink key={title} to={path} onClick={onClose}>
@@ -51,7 +58,7 @@ const SidebarItems = ({ onClose }: SidebarItemsProps) => {
return (
<>
<Text fontSize="xs" px={4} py={2} fontWeight="bold">
Menu
</Text>
<Box>{listItems}</Box>
</>

View File

@@ -19,6 +19,7 @@ import { Route as LayoutImport } from './routes/_layout'
import { Route as LayoutIndexImport } from './routes/_layout/index'
import { Route as LayoutSettingsImport } from './routes/_layout/settings'
import { Route as LayoutItemsImport } from './routes/_layout/items'
import { Route as LayoutClassifyNewsImport } from './routes/_layout/classifyNews'
import { Route as LayoutAdminImport } from './routes/_layout/admin'
// Create/Update Routes
@@ -63,6 +64,11 @@ const LayoutItemsRoute = LayoutItemsImport.update({
getParentRoute: () => LayoutRoute,
} as any)
const LayoutClassifyNewsRoute = LayoutClassifyNewsImport.update({
path: '/classifyNews',
getParentRoute: () => LayoutRoute,
} as any)
const LayoutAdminRoute = LayoutAdminImport.update({
path: '/admin',
getParentRoute: () => LayoutRoute,
@@ -96,6 +102,10 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof LayoutAdminImport
parentRoute: typeof LayoutImport
}
'/_layout/classifyNews': {
preLoaderRoute: typeof LayoutClassifyNewsImport
parentRoute: typeof LayoutImport
}
'/_layout/items': {
preLoaderRoute: typeof LayoutItemsImport
parentRoute: typeof LayoutImport
@@ -116,6 +126,7 @@ declare module '@tanstack/react-router' {
export const routeTree = rootRoute.addChildren([
LayoutRoute.addChildren([
LayoutAdminRoute,
LayoutClassifyNewsRoute,
LayoutItemsRoute,
LayoutSettingsRoute,
LayoutIndexRoute,

View File

@@ -0,0 +1,109 @@
import { createFileRoute } from "@tanstack/react-router"
import React, { useState } from "react"
import { Input, Button, Card, Spin, message, Typography, Space } from "antd"
import { UtilsService } from "@/client"
const { Title, Paragraph } = Typography
const ClassifyNewsPage = () => {
const [text, setText] = useState("")
const [loading, setLoading] = useState(false)
const [result, setResult] = useState<string | null>(null)
const handleClassify = async () => {
if (!text.trim()) {
message.warning("请输入新闻内容")
return
}
setLoading(true)
setResult(null)
try {
const res = await UtilsService.bentomlClassifiy({
requestBody: { text }
})
setResult(res.data.predict || "未识别分类")
} catch (err: any) {
message.error("分类请求失败")
setResult(null)
} finally {
setLoading(false)
}
}
return (
<div
style={{
minHeight: "100vh",
background: "linear-gradient(135deg, #f0f4ff 0%, #e0e7ff 100%)",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<Card
style={{
maxWidth: 520,
width: "100%",
boxShadow: "0 4px 24px rgba(0,0,0,0.08)",
borderRadius: 16,
}}
bodyStyle={{ padding: 32 }}
>
<Space direction="vertical" size="large" style={{ width: "100%" }}>
<Title level={3} style={{ marginBottom: 0, textAlign: "center" }}>
</Title>
<Paragraph type="secondary" style={{ textAlign: "center", marginTop: 0 }}>
</Paragraph>
<Input.TextArea
rows={6}
value={text}
onChange={e => setText(e.target.value)}
placeholder="请输入待分类的新闻文本"
disabled={loading}
style={{ resize: "none", fontSize: 16, borderRadius: 8 }}
autoSize={{ minRows: 6, maxRows: 10 }}
/>
<Button
type="primary"
onClick={handleClassify}
loading={loading}
block
size="large"
style={{ borderRadius: 8, fontWeight: 500, letterSpacing: 2 }}
>
</Button>
<div style={{ minHeight: 60 }}>
{loading && (
<div style={{ textAlign: "center", marginTop: 16 }}>
<Spin tip="正在识别分类..." />
</div>
)}
{result && !loading && (
<Card
type="inner"
title={<span style={{ fontWeight: 600, fontSize: 18 }}></span>}
style={{
marginTop: 16,
background: "#f6ffed",
borderColor: "#b7eb8f",
borderRadius: 8,
textAlign: "center",
}}
bodyStyle={{ fontSize: 20, color: "#389e0d", fontWeight: 600 }}
>
{result}
</Card>
)}
</div>
</Space>
</Card>
</div>
)
}
export const Route = createFileRoute("/_layout/classifyNews")({
component: ClassifyNewsPage,
})