import {z} from 'zod' export type GetApiClinicConfigDocumentLayoutOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceLayoutDto = z.object({showPatientPhoto: z.boolean().optional(), showPaymentDetails: z.boolean().optional(), showNotes: z.boolean().optional(), showProviders: z.boolean().optional(), footerText: z.string().nullable().optional(), defaultFooterText: z.string().nullable().optional()}); export const estimateLayoutDto = z.object({showPatientPhoto: z.boolean().optional(), footerText: z.string().nullable().optional()}); export const letterheadLayoutDto = z.object({showLogo: z.boolean().optional(), logoId: z.string().nullable().optional(), footerText: z.string().nullable().optional()}); export const documentLayoutSettingsDto = z.object({invoice: invoiceLayoutDto.optional(), estimate: estimateLayoutDto.optional(), letterhead: letterheadLayoutDto.nullable().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 /clinic-config/document-layout — which optional blocks the clinic's invoice, * receipt and treatment-plan PDFs carry (ITE-1334), defaults resolved. 404 while the * `DocumentLayout` flag is off. * * GET /clinic-config/document-layout */ export const getApiClinicConfigDocumentLayout = async (options?: GetApiClinicConfigDocumentLayoutOptions) => { 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(/\/+$/, '')}/clinic-config/document-layout${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(documentLayoutSettingsDto, await res.json()) };