UNPKG

@devflow-cc/react

Version:

一个功能强大的React库,用于构建数据驱动的应用程序,支持实时通信、身份验证和数据库操作

1,018 lines (1,003 loc) 23.7 kB
import { AxiosRequestConfig } from 'axios'; import React from 'react'; interface User { id: string; username: string; email: string; full_name?: string; avatar_url?: string; bio?: string; is_active: boolean; is_verified: boolean; is_superuser: boolean; role_names: string[]; scopes?: string[]; last_login_at?: string; created_at: string; updated_at: string; } interface AuthResponse { access_token: string; refresh_token: string; token_type: string; user: User; } interface LoginRequest { username: string; password: string; } interface RegisterRequest { username: string; email: string; password: string; full_name: string; } interface UpdateUserRequest { full_name?: string; bio?: string; } interface ChangePasswordRequest { current_password: string; new_password: string; } interface UserListParams { page?: number; page_size?: number; search?: string; is_active?: boolean; role?: string; is_superuser?: boolean; } interface UserListResponse { items: User[]; total: number; page: number; page_size: number; total_pages: number; } interface CreateUserRequest { username: string; email: string; password: string; full_name: string; is_active?: boolean; is_superuser?: boolean; } interface UpdateUserAdminRequest { full_name?: string; is_active?: boolean; bio?: string; } interface SetUserRolesRequest { role_names: string[]; } interface ResetPasswordRequest { new_password: string; } interface AuditLog { id: string; user_id?: string; action: string; resource_type?: string; resource_id?: string; status: string; ip_address?: string; user_agent?: string; request_method?: string; request_path?: string; duration_ms?: number; details?: Record<string, any>; created_at: string; } interface AuditLogParams { user_id?: string; action?: string; resource_type?: string; resource_id?: string; status?: string; ip_address?: string; start_time?: string; end_time?: string; page?: number; page_size?: number; } interface MyAuditLogParams { action?: string; resource_type?: string; days?: number; page?: number; page_size?: number; } interface AuditLogResponse { items: AuditLog[]; total: number; page: number; page_size: number; total_pages: number; } interface ActivitySummary { total_actions: number; unique_days: number; actions_by_type: Record<string, number>; last_activity: string; most_active_day: string; } interface SystemStats { total_operations: number; operations_by_action: Record<string, number>; operations_by_user: Record<string, number>; operations_by_status: Record<string, number>; most_active_users: Array<{ user_id: string; operations: number; }>; failed_operations: number; } interface DataRecord { id: string; [key: string]: any; } interface DataQueryParams { fields?: string[]; where_conditions?: QueryCondition[]; order_by?: QuerySort[]; page?: number; page_size?: number; limit?: number; offset?: number; } interface QueryCondition { field: string; operator: string; value: any; } interface QuerySort { field: string; direction: 'asc' | 'desc'; } interface DataQueryResponse { items: DataRecord[]; total: number; page: number; page_size: number; total_pages: number; } interface DataUpdateRequest { data: Record<string, any>; where_conditions: QueryCondition[]; } interface DataDeleteRequest { where_conditions: QueryCondition[]; } interface ColumnDefinition { name: string; type: string; length?: number; precision?: number; scale?: number; nullable?: boolean; primary_key?: boolean; unique?: boolean; default?: any; foreign_key?: string; comment?: string; } interface CreateTableRequest { name: string; columns: ColumnDefinition[]; } interface TableInfo { name: string; columns: ColumnDefinition[]; indexes?: any[]; constraints?: any[]; created_at: string; updated_at: string; } interface TableListParams { page?: number; page_size?: number; search?: string; } interface TableListResponse { items: TableInfo[]; total: number; page: number; page_size: number; total_pages: number; } interface UpdateTableRequest { add_columns?: ColumnDefinition[]; modify_columns?: ColumnDefinition[]; } interface TableStats { table_name: string; record_count: number; size_bytes: number; last_updated: string; } interface TableNameValidation { table_name: string; is_valid: boolean; error_message?: string; suggested_name?: string; } interface TablePrefixInfo { prefix: string; separator: string; example: string; } interface WebSocketConfig { token: string; onMessage?: (data: any) => void; onError?: (error: any) => void; onClose?: () => void; } interface WebSocketMessage { type: string; table?: string; action?: string; data?: any; } interface DevFlowConfig { baseURL: string; apiVersion?: string; timeout?: number; language?: string; headers?: Record<string, string>; tokenKey?: string; token?: string; errorHandler?: { onError?: (error: any) => void; retry: number; }; } interface ApiResponse<T = any> { data?: T; error?: ApiError; message?: string; status?: number; } interface ApiError { detail: string; error_code?: string; timestamp?: string; status_code?: number; } interface UseQueryResult<T> { data: T | null; loading: boolean; error: string | null; refetch: () => void; } interface UseMutationResult<T, V> { mutate: (variables: V) => Promise<T>; loading: boolean; error: string | null; reset: () => void; } /** * 配置管理器 */ declare class ConfigManager { private config; private messages; constructor(config: DevFlowConfig); /** * 获取配置 */ getConfig(): DevFlowConfig; /** * 更新配置 */ updateConfig(newConfig: Partial<DevFlowConfig>): void; /** * 获取当前语言 */ getCurrentLanguage(): string; /** * 设置语言 */ setLanguage(language: string): void; /** * 获取国际化消息 */ getMessage(key: string, params?: Record<string, any>): string; /** * 扩展国际化消息 */ extendMessages(language: string, messages: Record<string, string>): void; /** * 获取支持的语言列表 */ getSupportedLanguages(): string[]; /** * 设置错误处理器 */ setErrorHandler(onError: (error: any) => void, retry?: number): void; /** * 获取错误处理器配置 */ getErrorHandler(): { onError?: (error: any) => void; retry: number; } | undefined; /** * 清除错误处理器 */ clearErrorHandler(): void; } /** * HTTP客户端类 */ declare class HttpClient { private client; private configManager; private storageManager; private token; constructor(configManager: ConfigManager); /** * 设置请求和响应拦截器 */ private setupInterceptors; /** * 设置认证token */ setToken(token: string): void; /** * 清除认证token */ clearToken(): void; /** * 获取当前token */ getToken(): string | null; /** * 从存储恢复token */ restoreToken(): void; /** * 更新配置 */ updateConfig(configManager: ConfigManager): void; /** * 执行带重试的请求 */ private executeWithRetry; /** * 转换错误信息 */ private transformError; /** * GET请求 */ get<T = any>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; /** * POST请求 */ post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; /** * PUT请求 */ put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; /** * DELETE请求 */ delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; /** * PATCH请求 */ patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; } /** * 认证模块 */ declare class AuthModule { private httpClient; private configManager; constructor(httpClient: HttpClient, configManager: ConfigManager); /** * 用户注册 */ signUp(request: RegisterRequest): Promise<ApiResponse<AuthResponse>>; /** * 用户登录 */ signInWithPassword(request: LoginRequest): Promise<ApiResponse<AuthResponse>>; /** * 获取当前用户信息 */ getUser(): Promise<ApiResponse<User>>; /** * 更新当前用户信息 */ updateUser(request: UpdateUserRequest): Promise<ApiResponse<User>>; /** * 修改密码 */ changePassword(request: ChangePasswordRequest): Promise<ApiResponse<void>>; /** * 用户登出 */ signOut(): Promise<ApiResponse<void>>; /** * 刷新访问令牌 */ refreshToken(refreshToken: string): Promise<ApiResponse<AuthResponse>>; /** * 检查是否已登录 */ isAuthenticated(): boolean; /** * 获取当前token */ getToken(): string | null; } /** * 用户管理模块 */ declare class UsersModule { private httpClient; private configManager; constructor(httpClient: HttpClient, configManager: ConfigManager); /** * 获取用户列表 */ getUsers(params?: UserListParams): Promise<ApiResponse<UserListResponse>>; /** * 获取用户详情 */ getUser(userId: string): Promise<ApiResponse<User>>; /** * 管理员创建用户 */ createUser(request: CreateUserRequest): Promise<ApiResponse<User>>; /** * 管理员更新用户 */ updateUser(userId: string, request: UpdateUserAdminRequest): Promise<ApiResponse<User>>; /** * 删除用户 */ deleteUser(userId: string): Promise<ApiResponse<void>>; /** * 激活用户 */ activateUser(userId: string): Promise<ApiResponse<User>>; /** * 停用用户 */ deactivateUser(userId: string): Promise<ApiResponse<User>>; /** * 设置用户角色 */ setUserRoles(userId: string, roleNames: string[]): Promise<ApiResponse<User>>; /** * 重置用户密码 */ resetUserPassword(userId: string, newPassword: string): Promise<ApiResponse<void>>; } /** * 审计日志模块 */ declare class AuditModule { private httpClient; private configManager; constructor(httpClient: HttpClient, configManager: ConfigManager); /** * 获取审计日志列表 */ getLogs(params?: AuditLogParams): Promise<ApiResponse<AuditLogResponse>>; /** * 获取我的审计日志 */ getMyLogs(params?: MyAuditLogParams): Promise<ApiResponse<AuditLogResponse>>; /** * 获取用户活动摘要 */ getActivitySummary(params?: { user_id?: string; days?: number; }): Promise<ApiResponse<ActivitySummary>>; /** * 获取系统审计统计 */ getSystemStats(params?: { days?: number; }): Promise<ApiResponse<SystemStats>>; } /** * 数据操作模块 */ declare class DataModule { private httpClient; private configManager; private currentTable?; private conditions; private sorts; private fields; private pageNum?; private pageSize?; private limitNum?; private offsetNum?; constructor(httpClient: HttpClient, configManager: ConfigManager); /** * 从指定表获取查询构建器 */ from(table: string): this; /** * 重置查询状态 */ private resetQueryState; /** * 智能过滤 - 自动忽略空值条件 */ private addCondition; /** * 等于 */ eq(field: string, value: any): this; /** * 不等于 */ neq(field: string, value: any): this; /** * 不等于(别名) */ ne(field: string, value: any): this; /** * 大于 */ gt(field: string, value: any): this; /** * 大于等于 */ gte(field: string, value: any): this; /** * 小于 */ lt(field: string, value: any): this; /** * 小于等于 */ lte(field: string, value: any): this; /** * 模糊匹配(区分大小写) */ like(field: string, value: any): this; /** * 模糊匹配(不区分大小写) */ ilike(field: string, value: any): this; /** * 以指定字符串开头 */ startsWith(field: string, value: any): this; /** * 以指定字符串开头(别名) */ starts_with(field: string, value: any): this; /** * 以指定字符串开头(别名) */ startswith(field: string, value: any): this; /** * 以指定字符串结尾 */ endsWith(field: string, value: any): this; /** * 以指定字符串结尾(别名) */ ends_with(field: string, value: any): this; /** * 以指定字符串结尾(别名) */ endswith(field: string, value: any): this; /** * 正则表达式匹配 */ regex(field: string, value: any): this; /** * 不区分大小写的正则表达式 */ iregex(field: string, value: any): this; /** * SIMILAR TO 操作符 */ similar(field: string, value: any): this; /** * NOT SIMILAR TO 操作符 */ notSimilar(field: string, value: any): this; /** * NOT SIMILAR TO 操作符(别名) */ not_similar(field: string, value: any): this; /** * 包含在列表中 */ in(field: string, values: any[]): this; /** * 不包含在列表中 */ notIn(field: string, values: any[]): this; /** * 不包含在列表中(别名) */ nin(field: string, values: any[]): this; /** * 在指定范围内 */ between(field: string, start: any, end: any): this; /** * 范围查询 */ range(field: string, start: any, end: any): this; /** * 字段为 NULL */ isNull(field: string): this; /** * 字段为 NULL(别名) */ isnull(field: string): this; /** * 字段不为 NULL */ isNotNull(field: string): this; /** * 字段不为 NULL(别名) */ isnotnull(field: string): this; /** * 字段为空 */ isEmpty(field: string): this; /** * 字段为空(别名) */ isempty(field: string): this; /** * 字段不为空 */ isNotEmpty(field: string): this; /** * 字段不为空(别名) */ isnotempty(field: string): this; /** * JSONB 包含 */ contains(field: string, value: any): this; /** * JSONB 被包含 */ containedBy(field: string, value: any): this; /** * JSONB 包含指定键 */ hasKey(field: string, key: string): this; /** * JSONB 数组长度 */ jsonLength(field: string, length: number): this; /** * 排序 */ order(field: string, direction?: 'asc' | 'desc'): this; /** * 分页 */ page(page: number, pageSize: number): this; /** * 限制数量 */ limit(limit: number): this; /** * 偏移量 */ offset(offset: number): this; /** * 选择字段 */ selectFields(fields?: string[]): this; /** * 构建查询参数 */ private buildQueryParams; /** * 查询数据 */ select(fields?: string[]): Promise<ApiResponse<DataQueryResponse>>; /** * 查询数据(别名) */ query(): Promise<ApiResponse<DataQueryResponse>>; /** * 获取单条记录 */ get(id: string): Promise<ApiResponse<DataRecord>>; /** * 插入数据 */ insert(data: Record<string, any> | Record<string, any>[]): Promise<ApiResponse<DataRecord | DataRecord[]>>; /** * 更新数据 */ update(data: Record<string, any>, confirmAll?: boolean): Promise<ApiResponse<{ updated_count: number; }>>; /** * 删除数据 */ delete(confirmAll?: boolean, hardDelete?: boolean): Promise<ApiResponse<{ deleted_count: number; }>>; /** * 验证标准字段 */ private validateStandardFields; /** * 获取所有表的数据操作接口 */ get tables(): { from: (table: string) => DataModule; }; } /** * 表管理模块 */ declare class TableModule { private httpClient; private configManager; constructor(httpClient: HttpClient, configManager: ConfigManager); /** * 创建表 */ createTable(request: CreateTableRequest): Promise<ApiResponse<TableInfo>>; /** * 获取表列表 */ getTables(params?: TableListParams): Promise<ApiResponse<TableListResponse>>; /** * 获取表信息 */ getTableInfo(tableName: string): Promise<ApiResponse<TableInfo>>; /** * 更新表结构 */ updateTable(tableName: string, request: UpdateTableRequest): Promise<ApiResponse<TableInfo>>; /** * 删除表 */ deleteTable(tableName: string): Promise<ApiResponse<void>>; /** * 获取表统计信息 */ getTableStats(tableName: string): Promise<ApiResponse<TableStats>>; /** * 验证表名是否合法 */ validateTableName(tableName: string): Promise<ApiResponse<TableNameValidation>>; /** * 获取表前缀配置信息 */ getTablePrefixInfo(): Promise<ApiResponse<TablePrefixInfo>>; } /** * WebSocket连接类 */ declare class WebSocketConnection { private ws; private config; private configManager; private subscriptions; private reconnectAttempts; private maxReconnectAttempts; private reconnectInterval; constructor(config: WebSocketConfig, configManager: any); /** * 建立连接 */ connect(): Promise<void>; /** * 处理接收到的消息 */ private handleMessage; /** * 尝试重连 */ private attemptReconnect; /** * 订阅表变更 */ subscribe(table: string, callback: (data: any) => void): void; /** * 取消订阅 */ unsubscribe(table: string): void; /** * 关闭连接 */ close(): void; /** * 检查连接状态 */ isConnected(): boolean; } /** * WebSocket模块 */ declare class WebSocketModule { private configManager; constructor(configManager: any); /** * 建立WebSocket连接 */ connect(config: WebSocketConfig): WebSocketConnection; } /** * DevFlow 主客户端类 */ declare class DevFlowClient { private configManager; private httpClient; private authModule; private usersModule; private auditModule; private dataModule; private tableModule; private websocketModule; constructor(config: DevFlowConfig); /** * 设置配置 */ setConfig(config: Partial<DevFlowConfig>): void; /** * 获取配置 */ getConfig(): DevFlowConfig; /** * 设置认证token */ setToken(token: string): void; /** * 清除认证token */ clearToken(): void; /** * 设置语言 */ setLanguage(language: string): void; /** * 获取当前语言 */ getCurrentLanguage(): string; /** * 国际化函数 */ t(key: string, params?: Record<string, any>): string; /** * 扩展国际化消息 */ extendMessages(language: string, messages: Record<string, string>): void; /** * 获取支持的语言列表 */ getSupportedLanguages(): string[]; /** * 认证模块 */ get auth(): AuthModule; /** * 用户管理模块 */ get users(): UsersModule; /** * 审计日志模块 */ get audit(): AuditModule; /** * 数据操作模块 */ get data(): DataModule; /** * 表管理模块 */ get table(): TableModule; /** * WebSocket模块 */ get websocket(): WebSocketModule; /** * 从指定表获取查询构建器(简化写法) */ from(table: string): DataModule; /** * 创建实时数据通道(兼容旧版本) */ channel(table: string): any; /** * 实时数据模块(兼容旧版本) */ get realtime(): any; /** * 表结构模块(兼容旧版本) */ get schema(): any; } /** * 创建DevFlow客户端实例 */ declare function createClient(config: DevFlowConfig): DevFlowClient; /** * 创建DevFlow客户端实例(别名) */ declare function createDevFlowClient(config: DevFlowConfig): DevFlowClient; /** * 获取或创建全局单例实例 */ declare function devflowSingleton(config?: DevFlowConfig): DevFlowClient; /** * 重置全局单例实例 */ declare function resetDevFlow(): void; /** * 简化的存储管理器 */ declare class StorageManager { private tokenKey; private config; constructor(config: DevFlowConfig); /** * 获取token * 优先使用用户配置中的token,如果没有则从localStorage获取 */ getToken(): string | null; /** * 设置token */ setToken(token: string): void; /** * 清除token */ clearToken(): void; /** * 获取token键名 */ getTokenKey(): string; /** * 更新配置 */ updateConfig(config: DevFlowConfig): void; } /** * DevFlow Context 值 */ interface DevFlowContextValue { client: DevFlowClient; config: DevFlowConfig; } /** * DevFlow Provider 组件 */ interface DevFlowProviderProps { config: DevFlowConfig; children: React.ReactNode; } declare const DevFlowProvider: React.FC<DevFlowProviderProps>; /** * 使用 DevFlow Context */ declare const useDevFlow: () => DevFlowContextValue; /** * 使用 DevFlow 客户端 */ declare const useDevFlowClient: () => DevFlowClient; /** * 使用 DevFlow 客户端(简化版本) */ declare const useDevflow: () => DevFlowClient; declare const devflow: DevFlowClient; export { AuditModule, AuthModule, ConfigManager, DataModule, DevFlowClient, DevFlowProvider, HttpClient, StorageManager, TableModule, UsersModule, WebSocketConnection, WebSocketModule, createClient, createDevFlowClient, devflow, devflowSingleton, resetDevFlow, useDevFlow, useDevFlowClient, useDevflow }; export type { ActivitySummary, ApiError, ApiResponse, AuditLog, AuditLogParams, AuditLogResponse, AuthResponse, ChangePasswordRequest, ColumnDefinition, CreateTableRequest, CreateUserRequest, DataDeleteRequest, DataQueryParams, DataQueryResponse, DataRecord, DataUpdateRequest, DevFlowConfig, LoginRequest, MyAuditLogParams, QueryCondition, QuerySort, RegisterRequest, ResetPasswordRequest, SetUserRolesRequest, SystemStats, TableInfo, TableListParams, TableListResponse, TableNameValidation, TablePrefixInfo, TableStats, UpdateTableRequest, UpdateUserAdminRequest, UpdateUserRequest, UseMutationResult, UseQueryResult, User, UserListParams, UserListResponse, WebSocketConfig, WebSocketMessage };