import {z} from 'zod' export type InvoiceSettingsUpdate = {preset?: ('tracking' | 'document' | null) | undefined, document_required?: (boolean | null) | undefined, initial_state?: ('draft' | 'open' | null) | undefined, tax_fields?: ('hidden' | 'optional' | 'required' | null) | undefined, default_payment_terms_days?: (number | null) | undefined, number_prefix?: (string | null) | undefined, series?: (string | null) | undefined, issuer_display_name?: (string | null) | undefined, footer_note?: (string | null) | undefined, payment_details?: (string | null) | undefined, accent_color?: (string | null) | undefined, template?: ({[key: string]: unknown} | null) | undefined}; export type PatchApiApiInvoicesSettingsArgs = {'X-Workspace-Id'?: (string | null) | undefined, body: InvoiceSettingsUpdate}; export type PatchApiApiInvoicesSettingsOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceSettingsRead = z.object({preset: z.enum(["tracking", "document"]), document_required: z.boolean(), initial_state: z.enum(["draft", "open"]), tax_fields: z.enum(["hidden", "optional", "required"]), default_payment_terms_days: z.number().int(), number_prefix: z.string().nullable(), series: z.string().nullable(), next_number: z.number().int(), logo_id: z.string().nullable().optional(), issuer_display_name: z.string().nullable(), footer_note: z.string().nullable(), payment_details: z.string().nullable(), accent_color: z.string().nullable(), template: z.record(z.string(), z.unknown()).nullable()}); 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 }; /** * Write Settings * * PATCH /api/invoices/settings */ export const patchApiApiInvoicesSettings = async (args: PatchApiApiInvoicesSettingsArgs, options: PatchApiApiInvoicesSettingsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Workspace-Id'] != null) headers.set("X-Workspace-Id", String(args['X-Workspace-Id'])) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/invoices/settings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(invoiceSettingsRead, await res.json()) };