import {z} from 'zod' export type ResolveRequest = {query: string, request_id: string}; export type CreateApiPublicV1GenesResolveArgs = {body: ResolveRequest}; export type CreateApiPublicV1GenesResolveOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const geneIdentity = z.object({gene_id: z.string(), symbol: z.string().min(1).max(128), name: z.string().min(1).max(2000), locus_type: z.string().min(1).max(200), status: z.enum(["Approved", "Entry Withdrawn", "Merged/Split"]), aliases: z.array(z.string().min(1).max(200)).optional(), previous_symbols: z.array(z.string().min(1).max(200)).optional(), ensembl_ids: z.array(z.string()).optional(), ncbi_gene_ids: z.array(z.string()).optional(), source_id: z.string().min(1).max(200), replacement_gene_ids: z.array(z.string()).optional()}); export const candidate = z.object({identity: geneIdentity, match_types: z.array(z.enum(["approved_symbol", "previous_symbol", "alias", "stable_id", "withdrawn_symbol"])), matched_terms: z.array(z.string().min(1).max(200))}); export const resolveResponse = z.object({schema_version: z.literal("1.0").optional(), request_id: z.string(), identity_snapshot_id: z.string(), status: z.enum(["exact", "alias", "ambiguous", "not_found", "withdrawn"]), normalized_query: z.string().min(1).max(200), candidates: z.array(candidate), candidate_total: z.number().int().gte(0), complete: z.literal(true).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 }; /** * Resolve Gene * * POST /public/v1/genes/resolve */ export const createApiPublicV1GenesResolve = async (args: CreateApiPublicV1GenesResolveArgs, options: CreateApiPublicV1GenesResolveOptions) => { const {baseUrl, 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(/\/+$/, '')}/public/v1/genes/resolve${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(resolveResponse, await res.json()) };