import {z} from 'zod' export type CURIE = string; export type Attribute = {attribute_type_id: string, original_attribute_name?: (string | null) | undefined, value: unknown, value_type_id?: (CURIE | null) | undefined, attribute_source?: (string | null) | undefined, value_url?: (string | null) | undefined, description?: (string | null) | undefined, attributes?: (Array | null) | undefined}; export type NodeBinding = {id: CURIE, query_id?: CURIE | undefined, attributes?: (Array | null) | undefined} | {[key: string]: unknown}; export type EdgeBinding = {id: string, attributes?: (Array | null) | undefined} | {[key: string]: unknown}; export type Analysis = {resource_id: string, score?: (number | null) | undefined, edge_bindings: {[key: string]: Array}, support_graphs?: (Array | null) | undefined, scoring_method?: (string | null) | undefined, attributes?: (Array | null) | undefined} | {[key: string]: unknown}; export type Result = {node_bindings: {[key: string]: Array}, analyses: Array, id?: (string | null) | undefined, description?: (string | null) | undefined, essence?: (string | null) | undefined, essence_category?: (string | null) | undefined, row_data?: (Array | null) | undefined, score?: (number | null) | undefined, score_name?: (string | null) | undefined, score_direction?: (string | null) | undefined, confidence?: (number | null) | undefined, result_group?: (number | null) | undefined, result_group_similarity_score?: (number | null) | undefined, resource_id?: (string | null) | undefined} | {[key: string]: unknown}; export type BiolinkEntity = string; export type AttributeConstraint = {id: CURIE, name: string, not?: boolean | undefined, operator: '==' | '>' | '<' | 'matches' | '===', value: unknown, unit_id?: (string | null) | undefined, unit_name?: (string | null) | undefined}; export type QNode = {ids?: (Array | null) | undefined, categories?: (Array | null) | undefined, set_id?: (string | null) | undefined, is_set?: boolean | undefined, set_interpretation?: ('BATCH' | 'ALL' | 'MANY' | null) | undefined, constraints?: Array | undefined, option_group_id?: (string | null) | undefined} | {[key: string]: unknown}; export type BiolinkPredicate = string; export type Qualifier = {qualifier_type_id: string, qualifier_value: string}; export type QualifierConstraint = {qualifier_set: Array}; export type QEdge = {knowledge_type?: (string | null) | undefined, predicates?: (Array | null) | undefined, subject: string, object: string, attribute_constraints?: Array | undefined, qualifier_constraints?: Array | undefined, exclude?: boolean | undefined, option_group_id?: (string | null) | undefined} | {[key: string]: unknown}; export type QueryGraph = {nodes: {[key: string]: QNode}, edges: {[key: string]: QEdge}} | {[key: string]: unknown}; export type Node = {name?: (string | null) | undefined, categories?: (Array | null) | undefined, attributes?: (Array | null) | undefined}; export type ResourceRoleEnum = 'primary_knowledge_source' | 'aggregator_knowledge_source' | 'supporting_data_source'; export type RetrievalSource = {resource_id: string, resource_role: ResourceRoleEnum, upstream_resource_ids?: Array | undefined, source_record_urls?: Array | undefined} | {[key: string]: unknown}; export type Edge = {predicate: BiolinkPredicate, subject: CURIE, object: CURIE, attributes?: (Array | null) | undefined, qualifiers?: (Array | null) | undefined, sources: Array}; export type KnowledgeGraph = {nodes: {[key: string]: Node}, edges: {[key: string]: Edge}} | {[key: string]: unknown}; export type AuxiliaryGraph = {edges: Array, attributes?: (Array | null) | undefined} | {[key: string]: unknown}; export type Message = {results?: (Array | null) | undefined, query_graph?: (QueryGraph | null) | undefined, knowledge_graph?: (KnowledgeGraph | null) | undefined, auxiliary_graphs?: {[key: string]: AuxiliaryGraph} | undefined}; export type LogLevel = 'ERROR' | 'WARNING' | 'INFO' | 'DEBUG'; export type OperationLookup = {id: 'lookup', parameters: unknown}; export type AsyncQuery = {callback: string, message: Message, log_level?: (LogLevel | null) | undefined, workflow?: Array | undefined, submitter?: (string | null) | undefined, bypass_cache?: boolean | undefined} | {[key: string]: unknown}; export type CreateApiAsyncqueryArgs = {body: AsyncQuery}; export type CreateApiAsyncqueryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const asyncQueryResponse = z.object({status: z.string().nullable().optional(), description: z.string().nullable().optional(), job_id: z.string()}).and(z.record(z.string(), z.unknown())); 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 }; /** * Initiate a query with a callback to receive the response * * POST /asyncquery */ export const createApiAsyncquery = async (args: CreateApiAsyncqueryArgs, options?: CreateApiAsyncqueryOptions) => { const {baseUrl = "https://kg2cploverdb.transltr.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/asyncquery${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(asyncQueryResponse, await res.json()) };