import {z} from 'zod' export type GetApiApiV2SettingsTextTemplatesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV2SettingsTextTemplatesResponse = z.object({data: z.object({offer: z.object({header: z.string(), footer: z.string(), disable_stationary: z.boolean()}), order: z.object({header: z.string(), footer: z.string(), disable_stationary: z.boolean()}), invoice: z.object({header: z.string(), footer: z.string(), disable_stationary: z.boolean()}), delivery_note: z.object({header: z.string(), footer: z.string(), disable_stationary: z.boolean(), eu_delivery_note: z.string().optional(), export_delivery_note: z.string().optional()}), credit_note: z.object({header: z.string(), footer: z.string(), disable_stationary: z.boolean()}), purchase_order: z.object({header: z.string(), footer: z.string()}), work_report: z.object({header: z.string(), footer: z.string(), disable_stationary: z.boolean()}), commission_credit_note: z.object({header: z.string(), footer: z.string()}), proforma_invoice: z.object({header: z.string(), footer: z.string(), disable_stationary: z.boolean()}), misc: z.object({travel_expenses_without_letterhead: z.boolean(), footer_order_offer: z.string(), footer_order_offer_enabled: z.boolean(), footer_order_order: z.string(), footer_order_order_enabled: z.boolean(), footer_order_invoice: z.string(), footer_order_invoice_enabled: z.boolean(), footer_order_credit_note: z.string(), footer_order_credit_note_enabled: z.boolean(), footer_order_delivery_note: z.string(), footer_order_delivery_note_enabled: z.boolean(), footer_order_purchase_order: z.string(), footer_order_purchase_order_enabled: z.boolean(), footer_order_proforma_invoice: z.string(), footer_order_proforma_invoice_enabled: z.boolean()})})}); 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 }; /** * List text templates * * GET /api/v2/settings/text-templates */ export const getApiApiV2SettingsTextTemplates = async (options?: GetApiApiV2SettingsTextTemplatesOptions) => { const {baseUrl = "https://xentral.xentral.biz", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/settings/text-templates${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(getApiApiV2SettingsTextTemplatesResponse, await res.json()) };