import {z} from 'zod' export type GetApiPreprocessingVepCacheSearchArgs = {chromosome?: string | undefined, position?: number | undefined, ref?: string | undefined, alt?: string | undefined, gene?: string | undefined, limit?: number | undefined}; export type GetApiPreprocessingVepCacheSearchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const vepCacheConsequenceBreakdown = z.object({consequence: z.string(), impact: z.string(), count: z.number().int()}); export const vepCacheSearchResult = z.object({mode: z.string(), query_time_ms: z.number().int().optional(), status: z.string().nullable().optional(), positive_hits: z.array(z.record(z.string(), z.unknown())).optional(), negative_hits: z.array(z.record(z.string(), z.unknown())).optional(), gene: z.string().nullable().optional(), total_variants: z.number().int().optional(), consequence_breakdown: z.array(vepCacheConsequenceBreakdown).optional(), sample_variants: z.array(z.record(z.string(), z.unknown())).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 }; /** * Search Vep Cache * * GET /preprocessing/vep-cache/search */ export const getApiPreprocessingVepCacheSearch = async (args: GetApiPreprocessingVepCacheSearchArgs, options: GetApiPreprocessingVepCacheSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.chromosome != null) query.append("chromosome", String(args.chromosome)) if (args.position != null) query.append("position", String(args.position)) if (args.ref != null) query.append("ref", String(args.ref)) if (args.alt != null) query.append("alt", String(args.alt)) if (args.gene != null) query.append("gene", String(args.gene)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/preprocessing/vep-cache/search${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(vepCacheSearchResult, await res.json()) };