import {z} from 'zod' export type GetApiDrV1AppointmentArgs = {Authorization: string, patient_id?: string | undefined, doctor_id?: string | undefined, clinic_id?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, page_no?: number | undefined}; export type GetApiDrV1AppointmentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiDrV1AppointmentResponse = z.object({appointments: z.array(z.object({appointment_id: z.string().optional(), created_at: z.number().int().optional(), doctor_id: z.string().optional(), patient_id: z.string().optional(), clinic_id: z.string().optional(), channel: z.string().optional(), status: z.enum(["BK", "OG", "CM", "CMNP", "CN", "CND", "CNS"]).optional(), mode: z.enum(["tele", "in_clinic"]).optional(), start_time: z.number().int().optional(), end_time: z.number().int().optional(), prescription_id: z.string().optional(), prescription_url: z.string().optional(), partner_appointment_id: z.string().optional(), partner_clinic_id: z.string().optional(), partner_doctor_id: z.string().optional(), partner_patient_id: z.string().optional()})).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 }; /** * Get Appointments * * GET /dr/v1/appointment */ export const getApiDrV1Appointment = async (args: GetApiDrV1AppointmentArgs, options?: GetApiDrV1AppointmentOptions) => { const {baseUrl = "https://api.eka.care", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.patient_id != null) query.append("patient_id", String(args.patient_id)) if (args.doctor_id != null) query.append("doctor_id", String(args.doctor_id)) if (args.clinic_id != null) query.append("clinic_id", String(args.clinic_id)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.page_no != null) query.append("page_no", String(args.page_no)) const url = `${baseUrl.replace(/\/+$/, '')}/dr/v1/appointment${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(getApiDrV1AppointmentResponse, await res.json()) };