import {z} from 'zod' export type PepReferralRequest = {patientId: string, appointmentId?: (string | null) | undefined, requestId?: string | undefined, previewHash?: (string | null) | undefined, consentConfirmed?: boolean | undefined, allowPossibleDuplicate?: boolean | undefined, includeDiagnosis?: boolean | undefined, phoneOverride?: (string | null) | undefined, clinicalNotes?: (string | null) | undefined, urgency?: (string | null) | undefined, preferredDate?: (string | null) | undefined, preferredTime?: (string | null) | undefined}; export type CreateApiPepReferralsPreviewArgs = {body: PepReferralRequest}; export type CreateApiPepReferralsPreviewOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pepPractice = z.object({practice_name: z.string().nullable().optional(), referring_user: z.string().nullable().optional()}); export const pepClient = z.object({client_first_name: z.string().nullable().optional(), client_last_name: z.string().nullable().optional(), client_phone: z.string(), client_email: z.string().nullable().optional(), client_postcode: z.string().nullable().optional(), client_address_line_1: z.string().nullable().optional(), client_address_line_2: z.string().nullable().optional(), client_city: z.string().nullable().optional()}); export const pepPatient = z.object({pet_name: z.string(), pet_species: z.string().nullable().optional(), pet_breed: z.string().nullable().optional(), pet_age_years: z.union([z.number(), z.string()]).nullable().optional(), pet_weight_kg: z.union([z.number(), z.string()]).nullable().optional(), pet_sex: z.string().nullable().optional(), pet_microchip: z.string().nullable().optional()}); export const pepNotes = z.object({clinical_notes: z.string().nullable().optional(), clinical_diagnosis: z.string().nullable().optional(), urgency: z.string().nullable().optional(), preferred_date: z.string().nullable().optional(), preferred_time: z.string().nullable().optional()}); export const pepReferralPayload = z.object({practice: pepPractice.nullable().optional(), client: pepClient, patient: pepPatient, notes: pepNotes.nullable().optional()}); export const pepPreviewDto = z.object({patientName: z.string(), clientName: z.string(), phone: z.string(), payload: pepReferralPayload.nullable(), validationError: z.string().nullable(), suggestionReason: z.string().nullable(), payloadHash: z.string().nullable()}); 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 }; /** * POST /pep/referrals/preview */ export const createApiPepReferralsPreview = async (args: CreateApiPepReferralsPreviewArgs, options?: CreateApiPepReferralsPreviewOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/pep/referrals/preview${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(pepPreviewDto, await res.json()) };