import {z} from 'zod' export type GetApiApiV1InvoicesArgs = {filter?: (Array<{key?: ('search' | 'id' | 'invoice' | 'date' | 'status' | 'customerName' | 'customerNumber' | 'country' | 'projectId' | 'projectName' | 'projectAbbr' | 'paymentMethod' | 'paymentStatus' | 'debit' | 'balance' | 'currency' | 'shippingMethod' | 'amountNet' | 'salesOrder' | 'dunningLevel' | 'updatedAt') | undefined, op?: ('equals' | 'notEquals' | 'in' | 'notIn' | 'exactlyIn' | 'allIn' | 'allNotIn' | 'contains' | 'notContains' | 'startsWith' | 'endsWith' | 'greaterThan' | 'lessThan' | 'is' | 'lessThanOrEquals' | 'greaterThanOrEquals') | undefined, value?: (string | Array) | undefined}>) | undefined, page?: {number: string, size: string} | undefined, order?: (Array<{field?: ('id' | 'invoice' | 'date' | 'customerName' | 'customerNumber' | 'country' | 'project' | 'paymentMethod' | 'debit' | 'currency' | 'paymentStatus' | 'balance' | 'status' | 'shippingMethod' | 'amountNet' | 'dunningDate') | undefined, dir?: ('asc' | 'desc') | undefined}>) | undefined}; export type GetApiApiV1InvoicesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1InvoicesResponse = z.object({data: z.array(z.object({id: z.string().optional(), number: z.string().nullable().optional(), date: z.string().optional(), status: z.enum(["created", "released", "sent", "canceled", "completed", "partialCancellation", "unknown"]).optional(), address: z.object({name: z.string(), country: z.string(), customerNumber: z.string()}).optional(), project: z.object({id: z.string(), name: z.string(), abbreviation: z.string()}).nullable().optional(), payment: z.object({method: z.string(), status: z.enum(["pending", "paid", "partlyPaid", "canceled", "partialCancellation", "badDebt", "debited", "unknown"]), debit: z.object({amount: z.string().optional(), currency: z.enum(["EUR", "USD", "JPY", "BGN", "CZK", "DKK", "GBP", "HUF", "PLN", "RON", "SEK", "CHF", "ISK", "NOK", "HRK", "RUB", "TRY", "AUD", "BRL", "CAD", "CNY", "HKD", "IDR", "ILS", "INR", "KRW", "MXN", "MYR", "NZD", "PHP", "SGD", "THB", "ZAR", "ARS", "CLP", "SAR", "AED", "QAR"]).optional()}), balance: z.object({amount: z.string().optional(), currency: z.enum(["EUR", "USD", "JPY", "BGN", "CZK", "DKK", "GBP", "HUF", "PLN", "RON", "SEK", "CHF", "ISK", "NOK", "HRK", "RUB", "TRY", "AUD", "BRL", "CAD", "CNY", "HKD", "IDR", "ILS", "INR", "KRW", "MXN", "MYR", "NZD", "PHP", "SGD", "THB", "ZAR", "ARS", "CLP", "SAR", "AED", "QAR"]).optional()})}).optional(), additionalFields: z.array(z.object({key: z.enum(["amountNet", "shippingMethod", "orderNumber", "internet", "contactPerson", "distribution", "email"]).optional(), value: z.string().optional()})).optional(), tags: z.array(z.object({id: z.string().optional(), name: z.string().optional(), color: z.string().optional()})).optional(), dunningSettings: z.object({level: z.enum(["noReminder", "paymentReminder", "reminder1", "reminder2", "reminder3", "debtCollection", "lossOfReceivables"]), date: z.string(), comment: z.string(), sent: z.boolean()}).optional(), updatedBy: z.object({id: z.string()}).optional(), updatedAt: z.string().optional()})).optional(), extra: z.object({totalCount: z.number().int(), page: z.object({number: z.number().int(), size: z.number().int()})}).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 }; /** * List invoices * * GET /api/v1/invoices */ export const getApiApiV1Invoices = async (args: GetApiApiV1InvoicesArgs, options?: GetApiApiV1InvoicesOptions) => { const {baseUrl = "https://xentral.xentral.biz", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.xentral.minimal+json") if (args.filter != null) query.append("filter", args.filter.join(',')) if (args.page != null) query.append("page", JSON.stringify(args.page)) if (args.order != null) query.append("order", args.order.join(',')) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/invoices${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(getApiApiV1InvoicesResponse, await res.json()) };