import {z} from 'zod' export type SeparateVcfMemberSpec = {vcf_file_path: string, family_role: string, affected_status?: (string | null) | undefined, case_label?: (string | null) | undefined, sex?: (string | null) | undefined, staging_session_id?: (string | null) | undefined, clinical_profile?: ({[key: string]: unknown} | null) | undefined}; export type CreateFamilyFromSeparateVcfsRequest = {family_name?: (string | null) | undefined, family_notes?: (string | null) | undefined, inheritance_hypothesis?: (string | null) | undefined, members: Array, filtering_preset?: string | undefined}; export type CreateApiApiV1FamiliesFromSeparateVcfsArgs = {body: CreateFamilyFromSeparateVcfsRequest}; export type CreateApiApiV1FamiliesFromSeparateVcfsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const familyPublicResponse = z.object({id: z.string(), created_by: z.string(), organization_id: z.string(), name: z.string().nullable().optional(), notes: z.string().nullable().optional(), inheritance_hypothesis: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const familyFromJointVcfResponse = z.object({family: familyPublicResponse, session_ids: z.array(z.string())}); 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 }; /** * Create family + sessions from individual single-sample VCFs (atomic) * * POST /api/v1/families/from-separate-vcfs */ export const createApiApiV1FamiliesFromSeparateVcfs = async (args: CreateApiApiV1FamiliesFromSeparateVcfsArgs, options: CreateApiApiV1FamiliesFromSeparateVcfsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/families/from-separate-vcfs${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(familyFromJointVcfResponse, await res.json()) };