From c7805521976bd0d83b5a943862eb01afaed4626b Mon Sep 17 00:00:00 2001 From: icarus Date: Sun, 19 Oct 2025 23:21:54 +0800 Subject: [PATCH] feat(ocr): add api schemas and handlers for ocr providers Implement API schemas and handlers for OCR providers endpoints Add TODO comments for future migration tasks Fix endpoint path in OcrImageSettings component --- packages/shared/data/api/apiSchemas.ts | 28 +++++++++++++++++++ src/main/data/api/handlers/index.ts | 27 ++++++++++++++++++ src/renderer/src/hooks/useOcrProvider.tsx | 1 + .../DocProcessSettings/OcrImageSettings.tsx | 2 +- 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/packages/shared/data/api/apiSchemas.ts b/packages/shared/data/api/apiSchemas.ts index e405af806e..3788637e23 100644 --- a/packages/shared/data/api/apiSchemas.ts +++ b/packages/shared/data/api/apiSchemas.ts @@ -1,5 +1,7 @@ // NOTE: Types are defined inline in the schema for simplicity // If needed, specific types can be imported from './apiModels' +import type { OcrProvider } from '@types' + import type { BodyForPath, ConcreteApiPaths, QueryParamsForPath, ResponseForPath } from './apiPaths' import type { HttpMethod, PaginatedResponse, PaginationParams } from './apiTypes' @@ -345,6 +347,32 @@ export interface ApiSchemas { }> } } + + '/ocr/providers': { + GET: { + response: OcrProvider[] + } + POST: { + body: { + // TODO + } + } + } + + '/ocr/providers/:id': { + GET: { + response: OcrProvider + } + PATCH: { + // TODO + } + PUT: { + // TODO + } + DELETE: { + // TODO + } + } } /** diff --git a/src/main/data/api/handlers/index.ts b/src/main/data/api/handlers/index.ts index 39122449ed..c0f954875a 100644 --- a/src/main/data/api/handlers/index.ts +++ b/src/main/data/api/handlers/index.ts @@ -12,6 +12,7 @@ import { TestService } from '../services/TestService' // Service instances const testService = TestService.getInstance() +// Defining all handlers here feels a bit bloated; perhaps we should modularize things? /** * Complete API handlers implementation * Must implement every path+method combination from ApiSchemas @@ -207,5 +208,31 @@ export const apiHandlers: ApiImplementation = { data: { executed: true, timestamp: new Date().toISOString() } })) } + }, + + '/ocr/providers': { + GET: async () => { + // We should implement it when user data migration completed. + // OcrProvider should be migrate to db, because the data objects contains user settings. + throw new Error('Not implemented') + }, + POST: async () => { + throw new Error('Not implemented') + } + }, + + '/ocr/providers/:id': { + GET: async () => { + throw new Error('Not implemented') + }, + PATCH: async () => { + throw new Error('Not implemented') + }, + PUT: async () => { + throw new Error('Not implemented') + }, + DELETE: async () => { + throw new Error('Not implemented') + } } } diff --git a/src/renderer/src/hooks/useOcrProvider.tsx b/src/renderer/src/hooks/useOcrProvider.tsx index 4a32dda2d3..93a49c301a 100644 --- a/src/renderer/src/hooks/useOcrProvider.tsx +++ b/src/renderer/src/hooks/useOcrProvider.tsx @@ -18,6 +18,7 @@ import { useDispatch } from 'react-redux' const logger = loggerService.withContext('useOcrProvider') export const useOcrProviders = () => { + // TODO: migrate to useQuery const providers = useAppSelector((state) => state.ocr.providers) const imageProviders = providers.filter(isImageOcrProvider) const [imageProviderId, setImageProviderId] = usePreference('ocr.settings.image_provider_id') diff --git a/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx b/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx index 5206104b87..650dae01d5 100644 --- a/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx +++ b/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx @@ -22,7 +22,7 @@ const OcrImageSettings = () => { return window.api.ocr.listProviders() }, []) - const { data: validProviders, isLoading, error } = useSWRImmutable('ocr/providers', fetcher) + const { data: validProviders, isLoading, error } = useSWRImmutable('/ocr/providers', fetcher) const imageProviders = providers.filter((p) => isImageOcrProvider(p))