import {z} from 'zod' export type GetApiTemplatesDischargeVariablesAppointmentIdArgs = {appointmentId: string}; export type GetApiTemplatesDischargeVariablesAppointmentIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTemplatesDischargeVariablesAppointmentIdResponse = z.record(z.string(), z.string()); 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 }; /** * The patient-record discharge variables for an appointment (ITE-1237): microchip, colour, * age, weight in kg and lbs, and sex with neuter/spay status, as the printed discharge fills * them (DischargePatientVariables). The on-screen discharge preview * merges these into its template context, so it no longer shows the raw tokens. A value the * record lacks is an empty string. 404 for an unknown appointment, including another * tenant's. Open to any authenticated tenant user, like GET /patients/{id}: patient * demographics are not a sensitive read under docs/permissions.md (Model A). * * GET /templates/discharge-variables/{appointmentId} */ export const getApiTemplatesDischargeVariablesAppointmentId = async (args: GetApiTemplatesDischargeVariablesAppointmentIdArgs, options?: GetApiTemplatesDischargeVariablesAppointmentIdOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/templates/discharge-variables/${encodeURIComponent(String(args.appointmentId))}${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(getApiTemplatesDischargeVariablesAppointmentIdResponse, await res.json()) };