mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-30 15:41:56 +08:00
- Introduced `OffsetPaginationParams`, `CursorPaginationParams`, and their corresponding response types to standardize pagination handling across the API. - Updated existing API types and hooks to support both offset and cursor-based pagination, improving data fetching capabilities. - Enhanced documentation with detailed usage examples for pagination, including request parameters and response structures, to aid developers in implementing pagination effectively. - Refactored related components to utilize the new pagination types, ensuring consistency and clarity in data management.
114 lines
2.8 KiB
TypeScript
114 lines
2.8 KiB
TypeScript
import type { CursorPaginationParams, OffsetPaginationParams, ServiceOptions } from '@shared/data/api/apiTypes'
|
|
|
|
/**
|
|
* Base pagination params for service layer (supports both modes)
|
|
*/
|
|
type BasePaginationParams = (OffsetPaginationParams | CursorPaginationParams) & Record<string, any>
|
|
|
|
/**
|
|
* Standard service interface for data operations
|
|
* Defines the contract that all services should implement
|
|
*/
|
|
export interface IBaseService<T = any, TCreate = any, TUpdate = any> {
|
|
/**
|
|
* Find entity by ID
|
|
*/
|
|
findById(id: string, options?: ServiceOptions): Promise<T | null>
|
|
|
|
/**
|
|
* Find multiple entities with pagination
|
|
*/
|
|
findMany(
|
|
params: BasePaginationParams,
|
|
options?: ServiceOptions
|
|
): Promise<{
|
|
items: T[]
|
|
total?: number
|
|
page?: number
|
|
nextCursor?: string
|
|
}>
|
|
|
|
/**
|
|
* Create new entity
|
|
*/
|
|
create(data: TCreate, options?: ServiceOptions): Promise<T>
|
|
|
|
/**
|
|
* Update existing entity
|
|
*/
|
|
update(id: string, data: TUpdate, options?: ServiceOptions): Promise<T>
|
|
|
|
/**
|
|
* Delete entity (hard or soft delete depending on implementation)
|
|
*/
|
|
delete(id: string, options?: ServiceOptions): Promise<void>
|
|
|
|
/**
|
|
* Check if entity exists
|
|
*/
|
|
exists(id: string, options?: ServiceOptions): Promise<boolean>
|
|
}
|
|
|
|
/**
|
|
* Extended service interface for soft delete operations
|
|
*/
|
|
export interface ISoftDeleteService<T = any, TCreate = any, TUpdate = any> extends IBaseService<T, TCreate, TUpdate> {
|
|
/**
|
|
* Archive entity (soft delete)
|
|
*/
|
|
archive(id: string, options?: ServiceOptions): Promise<T | void>
|
|
|
|
/**
|
|
* Restore archived entity
|
|
*/
|
|
restore(id: string, options?: ServiceOptions): Promise<T | void>
|
|
}
|
|
|
|
/**
|
|
* Extended service interface for search operations
|
|
*/
|
|
export interface ISearchableService<T = any, TCreate = any, TUpdate = any> extends IBaseService<T, TCreate, TUpdate> {
|
|
/**
|
|
* Search entities by query
|
|
*/
|
|
search(
|
|
query: string,
|
|
params?: BasePaginationParams,
|
|
options?: ServiceOptions
|
|
): Promise<{
|
|
items: T[]
|
|
total?: number
|
|
page?: number
|
|
nextCursor?: string
|
|
}>
|
|
}
|
|
|
|
/**
|
|
* Service interface for hierarchical data (parent-child relationships)
|
|
*/
|
|
export interface IHierarchicalService<TParent = any, TChild = any, TChildCreate = any> extends IBaseService<TParent> {
|
|
/**
|
|
* Get child entities for a parent
|
|
*/
|
|
getChildren(
|
|
parentId: string,
|
|
params?: BasePaginationParams,
|
|
options?: ServiceOptions
|
|
): Promise<{
|
|
items: TChild[]
|
|
total?: number
|
|
page?: number
|
|
nextCursor?: string
|
|
}>
|
|
|
|
/**
|
|
* Add child entity to parent
|
|
*/
|
|
addChild(parentId: string, childData: TChildCreate, options?: ServiceOptions): Promise<TChild>
|
|
|
|
/**
|
|
* Remove child entity from parent
|
|
*/
|
|
removeChild(parentId: string, childId: string, options?: ServiceOptions): Promise<void>
|
|
}
|