mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 12:51:26 +08:00
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:
parent
4e7a67df59
commit
4ab6961fcc
@ -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: {
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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({
|
||||
|
||||
Loading…
Reference in New Issue
Block a user