feat(ocr): add type for OcrProviderId and getProvider method

Add OcrProviderId type definition and implement getProvider method in OcrService to fetch a single OCR provider by ID
This commit is contained in:
icarus 2025-10-20 07:23:41 +08:00
parent 4e7a67df59
commit 4ab6961fcc
4 changed files with 23 additions and 2 deletions

View File

@ -3,6 +3,7 @@
import type {
GetOcrProviderResponse,
ListOcrProvidersResponse,
OcrProviderId,
PatchOcrProviderRequest,
PatchOcrProviderResponse
} from '@types'
@ -366,6 +367,7 @@ export interface ApiSchemas {
'/ocr/providers/:id': {
GET: {
params: { id: OcrProviderId }
response: GetOcrProviderResponse
}
PATCH: {

View File

@ -221,8 +221,8 @@ export const apiHandlers: ApiImplementation = {
},
'/ocr/providers/:id': {
GET: async () => {
throw new Error('Not implemented')
GET: async ({ params }) => {
return ocrService.getProvider(params.id)
},
PATCH: async ({ body }) => {
return ocrService.patchProvider(body)

View File

@ -58,6 +58,22 @@ export class OcrService {
return { data: providers.filter((p) => registeredKeys.includes(p.id)) }
}
public async getProvider(providerId: string) {
if (!this.registry.has(providerId)) {
throw new Error(`OCR provider ${providerId} is not registered`)
}
const providers = await dbService
.getDb()
.select()
.from(ocrProviderTable)
.where(eq(ocrProviderTable.id, providerId))
.limit(1)
if (providers.length === 0) {
throw new Error(`OCR provider ${providerId} not found`)
}
return { data: providers[0] }
}
public async patchProvider(update: PatchOcrProviderRequest): Promise<PatchOcrProviderResponse> {
const providers = await dbService
.getDb()

View File

@ -82,6 +82,9 @@ export const OcrProviderConfigSchema = OcrProviderBaseConfigSchema.loose()
export type OcrProviderConfig = z.infer<typeof OcrProviderConfigSchema>
const OcrProviderIdSchema = z.string()
export type OcrProviderId = z.infer<typeof OcrProviderIdSchema>
const OcrProviderNameSchema = z.string()
export const OcrProviderSchema = z.object({