From 0b350294049f8e5eed20f25752d2b6c01dd7e89a Mon Sep 17 00:00:00 2001 From: fullex <0xfullex@gmail.com> Date: Fri, 26 Dec 2025 14:17:08 +0800 Subject: [PATCH] refactor(dataApi): remove batch and transaction support from Data API - Deleted batch and transaction related schemas, handlers, and IPC channels to streamline the Data API. - Updated related type definitions and import paths to reflect the removal of batch and transaction functionalities. - Simplified the API server and adapter logic by eliminating unused methods and handlers. --- packages/shared/IpcChannel.ts | 2 - packages/shared/data/README.md | 3 +- packages/shared/data/api/README.md | 5 +- packages/shared/data/api/apiTypes.ts | 46 ------ packages/shared/data/api/index.ts | 5 +- packages/shared/data/api/schemas/batch.ts | 140 ------------------ packages/shared/data/api/schemas/index.ts | 5 +- src/main/data/README.md | 5 +- src/main/data/api/core/ApiServer.ts | 31 ---- src/main/data/api/core/adapters/IpcAdapter.ts | 51 ------- src/main/data/api/handlers/batch.ts | 55 ------- src/main/data/api/handlers/index.ts | 6 +- src/preload/index.ts | 2 - src/renderer/src/data/DataApiService.ts | 31 +--- src/renderer/src/data/README.md | 9 +- tests/__mocks__/README.md | 1 - 16 files changed, 11 insertions(+), 386 deletions(-) delete mode 100644 packages/shared/data/api/schemas/batch.ts delete mode 100644 src/main/data/api/handlers/batch.ts diff --git a/packages/shared/IpcChannel.ts b/packages/shared/IpcChannel.ts index ea7164a0d0..c6611f19a4 100644 --- a/packages/shared/IpcChannel.ts +++ b/packages/shared/IpcChannel.ts @@ -346,8 +346,6 @@ export enum IpcChannel { // Data: API Channels DataApi_Request = 'data-api:request', - DataApi_Batch = 'data-api:batch', - DataApi_Transaction = 'data-api:transaction', DataApi_Subscribe = 'data-api:subscribe', DataApi_Unsubscribe = 'data-api:unsubscribe', DataApi_Stream = 'data-api:stream', diff --git a/packages/shared/data/README.md b/packages/shared/data/README.md index 1be09a01c1..9428522575 100644 --- a/packages/shared/data/README.md +++ b/packages/shared/data/README.md @@ -13,8 +13,7 @@ packages/shared/data/ │ ├── errorCodes.ts # Error handling utilities │ ├── schemas/ # Domain-specific API schemas │ │ ├── index.ts # Schema composition -│ │ ├── test.ts # Test API schema and DTOs -│ │ └── batch.ts # Batch/transaction operations +│ │ └── test.ts # Test API schema and DTOs │ └── README.md # Detailed API documentation ├── cache/ # Cache system type definitions │ ├── cacheTypes.ts # Core cache infrastructure types diff --git a/packages/shared/data/api/README.md b/packages/shared/data/api/README.md index b2e9c7da1e..059cbcf92d 100644 --- a/packages/shared/data/api/README.md +++ b/packages/shared/data/api/README.md @@ -12,8 +12,7 @@ packages/shared/data/api/ ├── errorCodes.ts # Error handling utilities and factories └── schemas/ ├── index.ts # Schema composition (merges all domain schemas) - ├── test.ts # Test API schema and DTOs - └── batch.ts # Batch/transaction API schema + └── test.ts # Test API schema and DTOs ``` ## File Responsibilities @@ -109,7 +108,7 @@ export interface TopicSchemas { import type { TopicSchemas } from './topic' // AssertValidSchemas provides fallback validation even if ValidateSchema is forgotten -export type ApiSchemas = AssertValidSchemas +export type ApiSchemas = AssertValidSchemas ``` 3. Implement handlers in `src/main/data/api/handlers/` diff --git a/packages/shared/data/api/apiTypes.ts b/packages/shared/data/api/apiTypes.ts index e6e1217c5f..e89a769619 100644 --- a/packages/shared/data/api/apiTypes.ts +++ b/packages/shared/data/api/apiTypes.ts @@ -168,52 +168,6 @@ export enum ErrorCode { CONCURRENT_MODIFICATION = 'CONCURRENT_MODIFICATION' } -/** - * Transaction request wrapper for atomic operations - */ -export interface TransactionRequest { - /** List of operations to execute in transaction */ - operations: DataRequest[] - /** Transaction options */ - options?: { - /** Database isolation level */ - isolation?: 'read-uncommitted' | 'read-committed' | 'repeatable-read' | 'serializable' - /** Whether to rollback entire transaction on any error */ - rollbackOnError?: boolean - /** Transaction timeout in milliseconds */ - timeout?: number - } -} - -/** - * Batch request for multiple operations - */ -export interface BatchRequest { - /** List of requests to execute */ - requests: DataRequest[] - /** Whether to execute requests in parallel */ - parallel?: boolean - /** Stop on first error */ - stopOnError?: boolean -} - -/** - * Batch response containing results for all requests - */ -export interface BatchResponse { - /** Individual response for each request */ - results: DataResponse[] - /** Overall batch execution metadata */ - metadata: { - /** Total execution time */ - duration: number - /** Number of successful operations */ - successCount: number - /** Number of failed operations */ - errorCount: number - } -} - /** * Pagination parameters for list operations */ diff --git a/packages/shared/data/api/index.ts b/packages/shared/data/api/index.ts index d3c4c8afde..f4404af011 100644 --- a/packages/shared/data/api/index.ts +++ b/packages/shared/data/api/index.ts @@ -19,15 +19,12 @@ // ============================================================================ export type { - BatchRequest, - BatchResponse, DataApiError, DataRequest, DataResponse, HttpMethod, PaginatedResponse, - PaginationParams, - TransactionRequest + PaginationParams } from './apiTypes' // ============================================================================ diff --git a/packages/shared/data/api/schemas/batch.ts b/packages/shared/data/api/schemas/batch.ts deleted file mode 100644 index c2fc3e0162..0000000000 --- a/packages/shared/data/api/schemas/batch.ts +++ /dev/null @@ -1,140 +0,0 @@ -/** - * Batch and Transaction API Schema definitions - * - * Contains cross-domain operations for batch processing and atomic transactions. - * These endpoints are domain-agnostic and work with any API path. - */ - -import type { HttpMethod } from '../apiTypes' - -// ============================================================================ -// Domain Models & DTOs -// ============================================================================ - -/** - * Request for bulk operations on multiple items - */ -export interface BulkOperationRequest { - /** Type of bulk operation to perform */ - operation: 'create' | 'update' | 'delete' | 'archive' | 'restore' - /** Array of data items to process */ - data: TData[] -} - -/** - * Response from a bulk operation - */ -export interface BulkOperationResponse { - /** Number of successfully processed items */ - successful: number - /** Number of items that failed processing */ - failed: number - /** Array of errors that occurred during processing */ - errors: Array<{ - /** Index of the item that failed */ - index: number - /** Error message */ - error: string - /** Optional additional error data */ - data?: any - }> -} - -// ============================================================================ -// API Schema Definitions -// ============================================================================ - -/** - * Batch and Transaction API Schema definitions - * - * Validation is performed at composition level via AssertValidSchemas - * in schemas/index.ts, which ensures: - * - All methods are valid HTTP methods (GET, POST, PUT, DELETE, PATCH) - * - All endpoints have a `response` field - */ -export interface BatchSchemas { - /** - * Batch execution of multiple requests - * @example POST /batch { "requests": [...], "parallel": true } - */ - '/batch': { - /** Execute multiple API requests in a single call */ - POST: { - body: { - /** Array of requests to execute */ - requests: Array<{ - /** HTTP method for the request */ - method: HttpMethod - /** API path for the request */ - path: string - /** URL parameters */ - params?: any - /** Request body */ - body?: any - }> - /** Execute requests in parallel vs sequential */ - parallel?: boolean - } - response: { - /** Results array matching input order */ - results: Array<{ - /** HTTP status code */ - status: number - /** Response data if successful */ - data?: any - /** Error information if failed */ - error?: any - }> - /** Batch execution metadata */ - metadata: { - /** Total execution duration in ms */ - duration: number - /** Number of successful requests */ - successCount: number - /** Number of failed requests */ - errorCount: number - } - } - } - } - - /** - * Atomic transaction of multiple operations - * @example POST /transaction { "operations": [...], "options": { "rollbackOnError": true } } - */ - '/transaction': { - /** Execute multiple operations in a database transaction */ - POST: { - body: { - /** Array of operations to execute atomically */ - operations: Array<{ - /** HTTP method for the operation */ - method: HttpMethod - /** API path for the operation */ - path: string - /** URL parameters */ - params?: any - /** Request body */ - body?: any - }> - /** Transaction configuration options */ - options?: { - /** Database isolation level */ - isolation?: 'read-uncommitted' | 'read-committed' | 'repeatable-read' | 'serializable' - /** Rollback all operations on any error */ - rollbackOnError?: boolean - /** Transaction timeout in milliseconds */ - timeout?: number - } - } - response: Array<{ - /** HTTP status code */ - status: number - /** Response data if successful */ - data?: any - /** Error information if failed */ - error?: any - }> - } - } -} diff --git a/packages/shared/data/api/schemas/index.ts b/packages/shared/data/api/schemas/index.ts index 0af34a84e5..4697880783 100644 --- a/packages/shared/data/api/schemas/index.ts +++ b/packages/shared/data/api/schemas/index.ts @@ -19,7 +19,6 @@ */ import type { AssertValidSchemas } from '../apiTypes' -import type { BatchSchemas } from './batch' import type { TestSchemas } from './test' /** @@ -37,7 +36,7 @@ import type { TestSchemas } from './test' * @example * ```typescript * import type { TopicSchemas } from './topic' - * export type ApiSchemas = AssertValidSchemas + * export type ApiSchemas = AssertValidSchemas * ``` */ -export type ApiSchemas = AssertValidSchemas +export type ApiSchemas = AssertValidSchemas diff --git a/src/main/data/README.md b/src/main/data/README.md index e6d52e83f2..a8c49346a4 100644 --- a/src/main/data/README.md +++ b/src/main/data/README.md @@ -13,8 +13,7 @@ src/main/data/ │ │ └── adapters/ # Communication adapters (IPC) │ ├── handlers/ # API endpoint implementations │ │ ├── index.ts # Handler aggregation and exports -│ │ ├── test.ts # Test endpoint handlers -│ │ └── batch.ts # Batch/transaction handlers +│ │ └── test.ts # Test endpoint handlers │ └── index.ts # API framework exports │ ├── services/ # Business logic layer @@ -102,7 +101,7 @@ The API framework provides the interface layer for data access: - Delegating to business services - Transforming responses for IPC - **Anti-pattern**: Do NOT put business logic in handlers -- **Currently**: Contains test and batch handlers (business handlers pending) +- **Currently**: Contains test handlers (business handlers pending) - **Type Safety**: Must implement all endpoints defined in `@shared/data/api/schemas/` ### Business Logic Layer (`services/`) diff --git a/src/main/data/api/core/ApiServer.ts b/src/main/data/api/core/ApiServer.ts index b2317e1395..57fe86add5 100644 --- a/src/main/data/api/core/ApiServer.ts +++ b/src/main/data/api/core/ApiServer.ts @@ -105,37 +105,6 @@ export class ApiServer { } } - /** - * Handle batch requests - */ - async handleBatchRequest(batchRequest: DataRequest): Promise { - const requests = batchRequest.body?.requests || [] - - if (!Array.isArray(requests)) { - throw DataApiErrorFactory.create(ErrorCode.VALIDATION_ERROR, 'Batch request body must contain requests array') - } - - logger.debug(`Processing batch request with ${requests.length} requests`) - - // Use the batch handler from our handlers - const batchHandler = this.handlers['/batch']?.POST - if (!batchHandler) { - throw DataApiErrorFactory.create(ErrorCode.NOT_FOUND, 'Batch handler not found') - } - - const result = await batchHandler({ body: batchRequest.body }) - - return { - id: batchRequest.id, - status: 200, - data: result, - metadata: { - duration: 0, - timestamp: Date.now() - } - } - } - /** * Find handler for given path and method */ diff --git a/src/main/data/api/core/adapters/IpcAdapter.ts b/src/main/data/api/core/adapters/IpcAdapter.ts index 7d17264388..a37a33cbd3 100644 --- a/src/main/data/api/core/adapters/IpcAdapter.ts +++ b/src/main/data/api/core/adapters/IpcAdapter.ts @@ -57,55 +57,6 @@ export class IpcAdapter { } }) - // Batch request handler - ipcMain.handle(IpcChannel.DataApi_Batch, async (_event, batchRequest: DataRequest): Promise => { - try { - logger.debug('Handling batch request', { requestCount: batchRequest.body?.requests?.length }) - - const response = await this.apiServer.handleBatchRequest(batchRequest) - return response - } catch (error) { - logger.error('Batch request failed', error as Error) - - const apiError = toDataApiError(error, 'batch request') - return { - id: batchRequest.id, - status: apiError.status, - error: apiError, - metadata: { - duration: 0, - timestamp: Date.now() - } - } - } - }) - - // Transaction handler (placeholder) - ipcMain.handle( - IpcChannel.DataApi_Transaction, - async (_event, transactionRequest: DataRequest): Promise => { - try { - logger.debug('Handling transaction request') - - // TODO: Implement transaction support - throw new Error('Transaction support not yet implemented') - } catch (error) { - logger.error('Transaction request failed', error as Error) - - const apiError = toDataApiError(error, 'transaction request') - return { - id: transactionRequest.id, - status: apiError.status, - error: apiError, - metadata: { - duration: 0, - timestamp: Date.now() - } - } - } - } - ) - // Subscription handlers (placeholder for future real-time features) ipcMain.handle(IpcChannel.DataApi_Subscribe, async (_event, path: string) => { logger.debug(`Data subscription request: ${path}`) @@ -134,8 +85,6 @@ export class IpcAdapter { logger.debug('Removing IPC handlers...') ipcMain.removeHandler(IpcChannel.DataApi_Request) - ipcMain.removeHandler(IpcChannel.DataApi_Batch) - ipcMain.removeHandler(IpcChannel.DataApi_Transaction) ipcMain.removeHandler(IpcChannel.DataApi_Subscribe) ipcMain.removeHandler(IpcChannel.DataApi_Unsubscribe) diff --git a/src/main/data/api/handlers/batch.ts b/src/main/data/api/handlers/batch.ts deleted file mode 100644 index 8ae302462e..0000000000 --- a/src/main/data/api/handlers/batch.ts +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Batch and Transaction API Handlers - * - * Implements cross-domain batch processing and atomic transaction operations. - */ - -import type { ApiHandler, ApiMethods } from '@shared/data/api/apiTypes' -import type { BatchSchemas } from '@shared/data/api/schemas/batch' - -/** - * Handler type for a specific batch endpoint - */ -type BatchHandler> = ApiHandler - -/** - * Batch API handlers implementation - */ -export const batchHandlers: { - [Path in keyof BatchSchemas]: { - [Method in keyof BatchSchemas[Path]]: BatchHandler> - } -} = { - '/batch': { - POST: async ({ body }) => { - // Mock batch implementation - can be enhanced with actual batch processing - const { requests } = body - - const results = requests.map(() => ({ - status: 200, - data: { processed: true, timestamp: new Date().toISOString() } - })) - - return { - results, - metadata: { - duration: Math.floor(Math.random() * 500) + 100, - successCount: requests.length, - errorCount: 0 - } - } - } - }, - - '/transaction': { - POST: async ({ body }) => { - // Mock transaction implementation - can be enhanced with actual transaction support - const { operations } = body - - return operations.map(() => ({ - status: 200, - data: { executed: true, timestamp: new Date().toISOString() } - })) - } - } -} diff --git a/src/main/data/api/handlers/index.ts b/src/main/data/api/handlers/index.ts index 79824b209a..a79f4c6707 100644 --- a/src/main/data/api/handlers/index.ts +++ b/src/main/data/api/handlers/index.ts @@ -6,7 +6,6 @@ * * Handler files are organized by domain: * - test.ts - Test API handlers - * - batch.ts - Batch and transaction handlers * * @example Adding a new domain: * ```typescript @@ -14,7 +13,6 @@ * * export const apiHandlers: ApiImplementation = { * ...testHandlers, - * ...batchHandlers, * ...topicHandlers // Add new domain handlers here * } * ``` @@ -22,7 +20,6 @@ import type { ApiImplementation } from '@shared/data/api/apiTypes' -import { batchHandlers } from './batch' import { testHandlers } from './test' /** @@ -33,6 +30,5 @@ import { testHandlers } from './test' * TypeScript ensures exhaustive coverage - missing handlers cause compile errors. */ export const apiHandlers: ApiImplementation = { - ...testHandlers, - ...batchHandlers + ...testHandlers } diff --git a/src/preload/index.ts b/src/preload/index.ts index 47122439d0..2a0cbfe366 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -605,8 +605,6 @@ const api = { // Data API related APIs dataApi: { request: (req: any) => ipcRenderer.invoke(IpcChannel.DataApi_Request, req), - batch: (req: any) => ipcRenderer.invoke(IpcChannel.DataApi_Batch, req), - transaction: (req: any) => ipcRenderer.invoke(IpcChannel.DataApi_Transaction, req), subscribe: (path: string, callback: (data: any, event: string) => void) => { const channel = `${IpcChannel.DataApi_Stream}:${path}` const listener = (_: any, data: any, event: string) => callback(data, event) diff --git a/src/renderer/src/data/DataApiService.ts b/src/renderer/src/data/DataApiService.ts index 008a6e5baa..35a7fdac3b 100644 --- a/src/renderer/src/data/DataApiService.ts +++ b/src/renderer/src/data/DataApiService.ts @@ -15,7 +15,6 @@ * - Type-safe requests with full TypeScript inference * - Automatic retry with exponential backoff (network, timeout, 500/503 errors) * - Request timeout management (3s default) - * - Batch request support for performance * - Subscription management (real-time updates) * * Architecture: @@ -33,15 +32,11 @@ import { loggerService } from '@logger' import type { ApiClient, ConcreteApiPaths } from '@shared/data/api/apiTypes' import type { - BatchRequest, - BatchResponse, DataRequest, - DataResponse, HttpMethod, SubscriptionCallback, SubscriptionEvent, - SubscriptionOptions, - TransactionRequest + SubscriptionOptions } from '@shared/data/api/apiTypes' import { toDataApiError } from '@shared/data/api/errorCodes' @@ -311,30 +306,6 @@ export class DataApiService implements ApiClient { }) } - /** - * Execute multiple requests in batch - */ - async batch(requests: DataRequest[], options: { parallel?: boolean } = {}): Promise { - const batchRequest: BatchRequest = { - requests, - parallel: options.parallel ?? true - } - - return this.makeRequest('POST', '/batch', { body: batchRequest }) - } - - /** - * Execute requests in a transaction - */ - async transaction(operations: DataRequest[], options?: TransactionRequest['options']): Promise { - const transactionRequest: TransactionRequest = { - operations, - options - } - - return this.makeRequest('POST', '/transaction', { body: transactionRequest }) - } - /** * Subscribe to real-time updates */ diff --git a/src/renderer/src/data/README.md b/src/renderer/src/data/README.md index 850d887488..bf4068de06 100644 --- a/src/renderer/src/data/README.md +++ b/src/renderer/src/data/README.md @@ -82,9 +82,8 @@ const [recentFiles, setRecentFiles] = usePersistCache('app.recent_files') **Key Features**: - Type-safe request/response handling - Automatic retry with exponential backoff -- Batch operations and transactions - Real-time subscriptions -- Request cancellation and timeout handling +- Request timeout handling **Basic Usage**: ```typescript @@ -97,12 +96,6 @@ const topics = await dataApiService.get('/topics') const newTopic = await dataApiService.post('/topics', { body: { title: 'Hello', content: 'World' } }) - -// Batch operations -const responses = await dataApiService.batch([ - { method: 'GET', path: '/topics' }, - { method: 'GET', path: '/messages' } -]) ``` ### PreferenceService diff --git a/tests/__mocks__/README.md b/tests/__mocks__/README.md index 2ef64b7230..89ac4d348a 100644 --- a/tests/__mocks__/README.md +++ b/tests/__mocks__/README.md @@ -252,7 +252,6 @@ export const mockPreferenceDefaults: Record = { ### 功能特性 - **完整HTTP支持**: GET, POST, PUT, PATCH, DELETE -- **批量操作**: batch() 和 transaction() 支持 - **订阅系统**: subscribe/unsubscribe 模拟 - **连接管理**: connect/disconnect/ping 方法 - **智能模拟数据**: 基于路径自动生成合理的响应