import {z} from 'zod' export type WidgetClientInfoDto = {name: string, email: string, phone: string, referralSource?: (string | null) | undefined}; export type WidgetPatientInfoDto = {existingPatientId?: (string | null) | undefined, name?: (string | null) | undefined, species?: (string | null) | undefined, breed?: (string | null) | undefined}; export type CreateWidgetAppointmentRequest = {sessionId: string, clinicId: string, slotId: string, appointmentTypeId?: (string | null) | undefined, clientInfo: WidgetClientInfoDto, patientInfo: WidgetPatientInfoDto, reason?: (string | null) | undefined, notes?: (string | null) | undefined}; export type CreateApiPublicWidgetAppointmentsArgs = {body: CreateWidgetAppointmentRequest}; export type CreateApiPublicWidgetAppointmentsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const widgetAppointmentResponse = z.object({appointmentId: z.string().optional(), clientId: z.string().optional(), patientId: z.string().optional(), scheduledTime: z.string().optional(), providerName: z.string().nullable().optional(), locationName: z.string().nullable().optional(), confirmationNumber: 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 }; /** * Create an appointment booking (anonymous with client info) * * POST /public/widget/appointments */ export const createApiPublicWidgetAppointments = async (args: CreateApiPublicWidgetAppointmentsArgs, options?: CreateApiPublicWidgetAppointmentsOptions) => { 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(/\/+$/, '')}/public/widget/appointments${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(widgetAppointmentResponse, await res.json()) };