import {z} from 'zod' export type GetApiEnmDbSubstanceArgs = {db: 'calibrate' | 'enanomapper' | 'enpra' | 'gracious' | 'marina' | 'nanogenotox' | 'nanoinformatix' | 'nanoreg1' | 'nanoreg2' | 'nanotest' | 'sanowork' | 'sbd4nano' | 'sabydoma', search?: string | undefined, type?: ('substancetype' | 'name' | 'like' | 'regexp' | 'uuif' | 'CompTox' | 'DOI' | 'reliability' | 'purposeFlag' | 'studyResultType' | 'isRobustStudy' | 'citation' | 'citationowner' | 'topcategory' | 'endpointcategory' | 'params' | 'owner_name' | 'owner_uuid' | 'related' | 'reference' | 'facet') | undefined, compound_uri?: string | undefined, bundleUri?: string | undefined, addDummySubstance?: boolean | undefined, studysummary?: boolean | undefined, page?: number | undefined, pagesize?: number | undefined}; export type GetApiEnmDbSubstanceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const substance = z.object({substance: 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 }; /** * List substances * * GET /enm/{db}/substance */ export const getApiEnmDbSubstance = async (args: GetApiEnmDbSubstanceArgs, options?: GetApiEnmDbSubstanceOptions) => { const {baseUrl = "https://api.ideaconsult.net/enanomapper", 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.type != null) query.append("type", String(args.type)) if (args.compound_uri != null) query.append("compound_uri", String(args.compound_uri)) if (args.bundleUri != null) query.append("bundleUri", String(args.bundleUri)) if (args.addDummySubstance != null) query.append("addDummySubstance", String(args.addDummySubstance)) if (args.studysummary != null) query.append("studysummary", String(args.studysummary)) 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))}/substance${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(substance, await res.json()) };