import {z} from 'zod' export type GetApiChemicalOpenapiCompoundDetailArgs = {inchi_key: string}; export type GetApiChemicalOpenapiCompoundDetailOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const b014CompoundDetailOpenPatentItem = z.object({pn: z.string().optional(), patent_id: z.string().optional()}); export const b014CompoundDetailOpenPatentsItem = z.object({total: z.number().int().optional(), patents: z.array(b014CompoundDetailOpenPatentItem).optional()}); export const b014CompoundDetailValueUrlDTO = z.object({url: z.string().optional(), value: z.string().optional()}); export const b014CompoundDetailOpenCompoundCrossDTO = z.object({zinc: b014CompoundDetailValueUrlDTO.optional(), chebi: b014CompoundDetailValueUrlDTO.optional(), mcule: b014CompoundDetailValueUrlDTO.optional(), comptox: b014CompoundDetailValueUrlDTO.optional(), fda_srs: b014CompoundDetailValueUrlDTO.optional(), nih_cli: b014CompoundDetailValueUrlDTO.optional(), drugbank: b014CompoundDetailValueUrlDTO.optional(), pharmgkb: b014CompoundDetailValueUrlDTO.optional(), pub_chem: z.array(b014CompoundDetailValueUrlDTO).optional(), bindingdb: b014CompoundDetailValueUrlDTO.optional(), emolecules: b014CompoundDetailValueUrlDTO.optional(), nmrshiftdb: b014CompoundDetailValueUrlDTO.optional(), phamacology: b014CompoundDetailValueUrlDTO.optional(), human_metabolome: b014CompoundDetailValueUrlDTO.optional()}); export const b014CompoundDetailOpenCompoundDetailItem = z.object({inchi: z.string().optional(), names: z.array(z.unknown()).optional(), formula: z.string().optional(), patents: b014CompoundDetailOpenPatentsItem.optional(), synonyms: z.array(z.unknown()).optional(), inchi_key: z.string().optional(), iupac_name: z.string().optional(), mol_weight: z.number().optional(), canonical_smiles: z.string().optional(), cross_references: b014CompoundDetailOpenCompoundCrossDTO.optional(), isometric_smiles: z.string().optional()}); export const b014CompoundDetailResponse = z.object({data: b014CompoundDetailOpenCompoundDetailItem.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 }; /** * B014 Chemical Structure Details * * GET /chemical-openapi/compound/detail */ export const getApiChemicalOpenapiCompoundDetail = async (args: GetApiChemicalOpenapiCompoundDetailArgs, options?: GetApiChemicalOpenapiCompoundDetailOptions) => { 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.inchi_key != null) query.append("inchi_key", String(args.inchi_key)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/chemical-openapi/compound/detail${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(b014CompoundDetailResponse, await res.json()) };