import {z} from 'zod' export type Party = {id?: string | undefined, name: string, business_id?: string | undefined, vat_number?: string | undefined, address_line1?: string | undefined, postal_code?: string | undefined, city?: string | undefined, country_code?: string | undefined, email?: string | undefined, einvoice_address?: string | undefined, einvoice_operator?: string | undefined, default_payment_terms_days?: number | undefined, default_vat_code?: string | undefined, default_vat_rate_basis_points?: number | undefined, default_invoice_language?: string | undefined}; export type InvoiceLine = {description: string, quantity: string, unit_price_cents: number, vat_rate_basis_points?: (number | null) | undefined, vat_code?: string | undefined, net_cents: number, vat_cents: number, gross_cents: number}; export type InvoiceDraftInput = {customer_id?: string | undefined, draft_id?: string | undefined, customer: Party, lines: Array, issue_date?: string | undefined, delivery_date?: string | undefined, due_date?: string | undefined, seller_iban?: string | undefined, seller_vat_id?: string | undefined, terms_days?: number | undefined, note?: string | undefined, language?: string | undefined, delivery_channel?: ('maventa_einvoice' | 'email_pdf' | 'pdf_download') | undefined, email_subject?: string | undefined, email_message?: string | undefined, net_cents?: number | undefined, vat_cents?: number | undefined, gross_cents?: number | undefined}; export type CreateApiInvoiceRecurringArgs = {body: {customer_id?: string | undefined, template_id?: string | undefined, name: string, cadence_months: number, next_issue_date: string, draft: InvoiceDraftInput}}; export type CreateApiInvoiceRecurringOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiInvoiceRecurringResponse = z.record(z.string(), z.unknown()); 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 }; /** * Create or update a recurring invoice template * * POST /invoice-recurring */ export const createApiInvoiceRecurring = async (args: CreateApiInvoiceRecurringArgs, options?: CreateApiInvoiceRecurringOptions) => { const {baseUrl = "https://api.app.lastaccountingcompany.com/portal", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoice-recurring${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(createApiInvoiceRecurringResponse, await res.json()) };