import {z} from 'zod' export type CreateApiDrV1PatientArgs = {auth: string, body: {designation?: ('Mr.' | 'Ms.' | 'Mrs.' | 'Miss.' | 'Kumar.' | 'Shri.' | 'Smt.' | 'Dr.' | 'Master.' | 'Baby.' | 'Mohd.' | 'B/O') | undefined, first_name: string, partner_patient_id?: string | undefined, middle_name?: string | undefined, last_name?: string | undefined, mobile?: string | undefined, gender: 'M' | 'F' | 'O', address?: ({city?: string | undefined, pincode?: number | undefined}) | undefined, dob: string} | {designation?: ('Mr.' | 'Ms.' | 'Mrs.' | 'Miss.' | 'Kumar.' | 'Shri.' | 'Smt.' | 'Dr.' | 'Master.' | 'Baby.' | 'Mohd.' | 'B/O') | undefined, first_name: string, partner_patient_id?: string | undefined, middle_name?: string | undefined, last_name?: string | undefined, mobile?: string | undefined, gender: 'M' | 'F' | 'O', address?: ({city?: string | undefined, pincode?: number | undefined}) | undefined, age: number}}; export type CreateApiDrV1PatientOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiDrV1PatientResponse = z.object({patient_id: z.string().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 }; /** * Add Patient to Directory * * POST /dr/v1/patient */ export const createApiDrV1Patient = async (args: CreateApiDrV1PatientArgs, options?: CreateApiDrV1PatientOptions) => { const {baseUrl = "https://api.eka.care", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.auth != null) headers.set("auth", String(args.auth)) const url = `${baseUrl.replace(/\/+$/, '')}/dr/v1/patient${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(createApiDrV1PatientResponse, await res.json()) };