import {z} from 'zod' export type GetApiV1AppointmentsAppointmentIdFormsArgs = {appointment_id: string}; export type GetApiV1AppointmentsAppointmentIdFormsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1AppointmentsAppointmentIdFormsResponse = z.array(z.object({appointment_id: z.string().optional(), form_id: z.string().optional(), service_name: z.unknown(), service_displayname: z.unknown(), name: z.string().optional(), is_filled: z.boolean().optional(), is_submitted: z.boolean().optional(), lastfilled_date: z.string().optional(), form_type: z.number().int().optional(), lastfilled_by: z.string().optional(), latest_version: z.unknown(), form_history: z.array(z.object({version_date: z.string().optional(), version_id: z.string().optional(), created_by: z.string().optional(), form_name: z.string().optional(), form_id: z.string().optional()})).optional(), is_viewonly: z.boolean().optional(), expiry_date: z.unknown(), is_editable: z.boolean().optional(), is_expired: z.boolean().optional(), owner_id: z.unknown(), display_order: z.number().int().optional(), is_service_form: z.boolean().optional(), source_appointment_date: z.string().optional(), permissions: z.object({can_view: z.boolean().optional(), can_edit: z.boolean().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 }; /** * Retrieve the list of forms associated with an appointment * * GET /v1/appointments/{appointment_id}/forms */ export const getApiV1AppointmentsAppointmentIdForms = async (args: GetApiV1AppointmentsAppointmentIdFormsArgs, options?: GetApiV1AppointmentsAppointmentIdFormsOptions) => { const {baseUrl = "https://api.zenoti.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.appointment_id != null) query.append("appointment_id", String(args.appointment_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/appointments/${encodeURIComponent(String(args.appointment_id))}/forms${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(getApiV1AppointmentsAppointmentIdFormsResponse, await res.json()) };