mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 04:31:27 +08:00
- Removed outdated API model and schema files to simplify the structure. - Consolidated API types and schemas for better organization and clarity. - Updated import paths across the codebase to reflect the new structure. - Enhanced documentation in related README files to guide usage of the new API schema organization.
5.0 KiB
5.0 KiB
Cherry Studio Shared Data
This directory contains shared type definitions and schemas for the Cherry Studio data management systems. These files provide type safety and consistency across the entire application.
Directory Structure
packages/shared/data/
├── api/ # Data API type system (see api/README.md)
│ ├── index.ts # Barrel exports for infrastructure types
│ ├── apiTypes.ts # Core request/response types and utilities
│ ├── apiPaths.ts # Path template literal type utilities
│ ├── 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
│ └── README.md # Detailed API documentation
├── cache/ # Cache system type definitions
│ ├── cacheTypes.ts # Core cache infrastructure types
│ ├── cacheSchemas.ts # Cache key schemas and type mappings
│ └── cacheValueTypes.ts # Cache value type definitions
├── preference/ # Preference system type definitions
│ ├── preferenceTypes.ts # Core preference system types
│ └── preferenceSchemas.ts # Preference schemas and default values
├── types/ # Shared data types for Main/Renderer
└── README.md # This file
System Overview
This directory provides type definitions for four main data management systems:
Types System (types/)
- Purpose: Shared data types for cross-process (Main/Renderer) communication and database schemas
- Features: Database table field types, business entity definitions
- Usage: Used in Drizzle ORM schemas via
.$type<T>()and runtime type checking
API System (api/)
- Purpose: Type-safe IPC communication between Main and Renderer processes
- Features: RESTful patterns, modular schema design, error handling
- Documentation: See
api/README.mdfor detailed usage
Cache System (cache/)
- Purpose: Type definitions for three-layer caching architecture
- Features: Memory/shared/persist cache schemas, TTL support, hook integration
- Usage: Type-safe caching operations across the application
Preference System (preference/)
- Purpose: User configuration and settings management
- Features: 158 configuration items, default values, nested key support
- Usage: Type-safe preference access and synchronization
File Categories
Framework Infrastructure - These are TypeScript type definitions that:
- ✅ Exist only at compile time
- ✅ Provide type safety and IntelliSense support
- ✅ Define contracts between application layers
- ✅ Enable static analysis and error detection
Usage Examples
API Types
// Infrastructure types from barrel export
import type { DataRequest, DataResponse, ApiClient } from '@shared/data/api'
import { DataApiErrorFactory, ErrorCode } from '@shared/data/api'
// Domain DTOs directly from schema files
import type { TestItem, CreateTestItemDto } from '@shared/data/api/schemas/test'
Cache Types
// Import cache types
import type { UseCacheKey, UseSharedCacheKey } from '@shared/data/cache'
Preference Types
// Import preference types
import type { PreferenceKeyType, PreferenceDefaultScopeType } from '@shared/data/preference'
Development Guidelines
Adding Shared Types
- Create or update type file in
types/directory - Use camelCase for field names
- Reference types in Drizzle schemas using
.$type<T>()
Adding Cache Types
- Add cache key to
cache/cacheSchemas.ts - Define value type in
cache/cacheValueTypes.ts - Update type mappings for type safety
Adding Preference Types
- Add preference key to
preference/preferenceSchemas.ts - Define default value and type
- Preference system automatically picks up new keys
Adding API Types
- Create schema file in
api/schemas/(e.g.,topic.ts) - Define domain models, DTOs, and API schema in the file
- Register schema in
api/schemas/index.tsusing intersection type - See
api/README.mdfor detailed guide
Best Practices
- Use
import typefor type-only imports - Infrastructure types from barrel, domain DTOs from schema files
- Follow existing naming conventions
- Document complex types with JSDoc
Related Implementation
Main Process
src/main/data/api/- API server, handlers, and IPC adaptersrc/main/data/cache/- Cache service implementationsrc/main/data/preference/- Preference service implementation
Renderer Process
src/renderer/src/services/DataApiService.ts- API clientsrc/renderer/src/services/CacheService.ts- Cache servicesrc/renderer/src/services/PreferenceService.ts- Preference service