import {z} from 'zod' export type GetApiMetaKnowledgeGraphArgs = {format?: string | undefined}; export type GetApiMetaKnowledgeGraphOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const metaAttribute = z.object({attribute_type_id: z.string(), attribute_source: z.string().nullable().optional(), original_attribute_names: z.array(z.string()).nullable().optional(), constraint_use: z.boolean().optional(), constraint_name: z.string().nullable().optional()}); export const metaNode = z.object({id_prefixes: z.array(z.string()), attributes: z.array(metaAttribute).nullable().optional()}); export const metaQualifier = z.object({qualifier_type_id: z.string(), applicable_values: z.array(z.string()).optional()}); export const metaEdge = z.object({subject: z.string(), predicate: z.string(), object: z.string(), knowledge_types: z.array(z.string()).nullable().optional(), attributes: z.array(metaAttribute).nullable().optional(), qualifiers: z.array(metaQualifier).nullable().optional(), association: z.string().optional()}); export const metaKnowledgeGraph = z.object({nodes: z.record(z.string(), metaNode), edges: z.array(metaEdge)}); 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 }; /** * Meta knowledge graph representation of this TRAPI web service. * * GET /meta_knowledge_graph */ export const getApiMetaKnowledgeGraph = async (args: GetApiMetaKnowledgeGraphArgs, options?: GetApiMetaKnowledgeGraphOptions) => { const {baseUrl = "https://arax.transltr.io/api/arax/v1.4", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.format != null) query.append("format", String(args.format)) const url = `${baseUrl.replace(/\/+$/, '')}/meta_knowledge_graph${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(metaKnowledgeGraph, await res.json()) };