import {z} from 'zod' export type GetApiEurekalsOpenOcsrV2TaskArgs = {task_id: string}; export type GetApiEurekalsOpenOcsrV2TaskOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const b0672V2TaskOcsrLocationItemV2 = z.object({bbox: z.array(z.unknown()).optional(), page: z.number().int().optional(), image_name: z.string().optional()}); export const b0672V2TaskOcsrResultItemV2 = z.object({mol: z.string().optional(), smiles: z.string().optional(), formula: z.string().optional(), inchi_key: z.string().optional(), locations: z.array(b0672V2TaskOcsrLocationItemV2).optional()}); export const b0672V2TaskOpenAPIOCSRV2 = z.object({count: z.number().int().optional(), result: z.array(b0672V2TaskOcsrResultItemV2).optional(), status: z.string().optional(), task_id: z.string().optional()}); export const b0672V2TaskResponse = z.object({data: b0672V2TaskOpenAPIOCSRV2.optional(), status: z.boolean(), error_msg: z.string().optional(), error_code: z.number().int()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * B067-2 Get OCSR Task Status And Results * * GET /eurekals/open/ocsr/v2/task */ export const getApiEurekalsOpenOcsrV2Task = async (args: GetApiEurekalsOpenOcsrV2TaskArgs, options?: GetApiEurekalsOpenOcsrV2TaskOptions) => { const {baseUrl = "https://connect.patsnap.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.task_id != null) query.append("task_id", String(args.task_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/eurekals/open/ocsr/v2/task${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(b0672V2TaskResponse, await res.json()) };