import {z} from 'zod' export type TemplateSectionDto = {id?: string | undefined, type?: string | undefined, content?: string | undefined, order?: (number | unknown) | undefined, columns?: (Array | null) | undefined, widthPercent?: (number | unknown | null) | undefined}; export type RenderTemplatePreviewRequestDto = {sections?: Array | undefined, format?: (string | null) | undefined, type?: (string | null) | undefined, category?: (string | null) | undefined, emailSubject?: (string | null) | undefined, emailBody?: (string | null) | undefined, smsBody?: (string | null) | undefined}; export type CreateApiTemplatesRenderPreviewArgs = {body: RenderTemplatePreviewRequestDto}; export type CreateApiTemplatesRenderPreviewOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const templatePreviewResultDto = z.object({pages: z.array(z.string()).optional(), pageCount: z.union([z.number().int(), z.string()]).optional(), widthMm: z.union([z.number(), z.string()]).optional(), heightMm: z.union([z.number(), z.string()]).optional(), dpi: z.union([z.number().int(), z.string()]).optional(), emailSubject: z.string().nullable().optional(), emailBody: z.string().nullable().optional(), smsBody: z.string().nullable().optional(), unknownVariables: z.array(z.string()).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 }; /** * Renders (possibly unsaved) editor sections through the real production * pipeline — sample data, regional compliance blocks, QuestPDF — and * returns one PNG per page. More than one page on a fixed-size label * format means the content overflows the label. * * POST /templates/render-preview */ export const createApiTemplatesRenderPreview = async (args: CreateApiTemplatesRenderPreviewArgs, options?: CreateApiTemplatesRenderPreviewOptions) => { 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(/\/+$/, '')}/templates/render-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(templatePreviewResultDto, await res.json()) };