import {z} from 'zod' export type GetApiEnmDbQueryCompoundTermRepresentationArgs = {db: 'calibrate' | 'enanomapper' | 'enpra' | 'gracious' | 'marina' | 'nanogenotox' | 'nanoinformatix' | 'nanoreg1' | 'nanoreg2' | 'nanotest' | 'sanowork' | 'sbd4nano' | 'sabydoma', term: 'search' | 'url' | 'inchikey', representation: 'all' | 'smiles' | 'reach' | 'stdinchi' | 'stdinchikey' | 'names' | 'iupac_name' | 'synonym' | 'cas' | 'einecs', search?: string | undefined, b64search?: string | undefined, casesens?: boolean | undefined, bundleUri?: string | undefined, sameas?: string | undefined, page?: number | undefined, pagesize?: number | undefined}; export type GetApiEnmDbQueryCompoundTermRepresentationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dataset = z.object({query: z.object({}).optional(), dataEntry: z.object({}).optional(), model_uri: z.string().optional(), feature: z.object({}).optional()}); 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 }; /** * Exact chemical structure search * * GET /enm/{db}/query/compound/{term}/{representation} */ export const getApiEnmDbQueryCompoundTermRepresentation = async (args: GetApiEnmDbQueryCompoundTermRepresentationArgs, options?: GetApiEnmDbQueryCompoundTermRepresentationOptions) => { const {baseUrl = "https://api.ideaconsult.net/nanoreg1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.b64search != null) query.append("b64search", String(args.b64search)) if (args.casesens != null) query.append("casesens", String(args.casesens)) if (args.bundleUri != null) query.append("bundleUri", String(args.bundleUri)) if (args.sameas != null) query.append("sameas", String(args.sameas)) if (args.page != null) query.append("page", String(args.page)) if (args.pagesize != null) query.append("pagesize", String(args.pagesize)) const url = `${baseUrl.replace(/\/+$/, '')}/enm/${encodeURIComponent(String(args.db))}/query/compound/${encodeURIComponent(String(args.term))}/${encodeURIComponent(String(args.representation))}${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(dataset, await res.json()) };