import {z} from 'zod' export type GetApiApiV1PurchaseOrdersArgs = {filter?: (Array<{key?: ('id' | 'status' | 'date' | 'supplier' | 'documentNumber' | 'project' | 'createdAt' | 'updatedAt' | 'salesOrderConfirmationReference' | 'suppliersOfferReference') | 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' | 'date' | 'project' | 'status' | 'documentNumber') | undefined, dir?: ('asc' | 'desc') | undefined}>) | undefined}; export type GetApiApiV1PurchaseOrdersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1PurchaseOrdersResponse = z.object({data: z.array(z.object({id: z.string().optional(), date: z.string().optional(), project: z.object({id: z.string()}).optional(), status: z.string().optional(), documentNumber: z.string().optional(), supplier: z.object({id: z.string().optional(), number: z.string().optional(), customerNumberAtSupplier: z.string().optional(), honorific: z.string().optional(), name: z.string().optional(), department: z.string().optional(), subDepartment: z.string().optional(), extraAddressLine: z.string().optional(), street: z.string().optional(), zipCode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), contactPerson: z.string().optional(), telephone: z.string().optional(), telefax: z.string().optional(), email: z.string().optional()}).optional(), requestForQuotation: z.object({id: z.string()}).nullable().optional(), bodyIntroduction: z.string().optional(), bodyOutroduction: z.string().optional(), salesOrderConfirmationReference: z.string().optional(), isConfirmationRequested: z.boolean().optional(), suppliersOfferReference: z.string().optional(), internalDesignation: z.string().optional(), language: z.object({iso2: z.string().optional()}).optional(), isConfirmed: z.boolean().optional(), confirmedVia: z.string().optional(), differingDesignation: z.boolean().optional(), financials: z.object({paymentMethod: z.object({id: z.string()}).nullable().optional(), total: 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(), paymentTerms: z.object({paymentTargetDiscount: z.number().optional(), paymentTargetDays: z.number().optional(), paymentTargetDiscountDays: z.number().optional(), paymentTargetDiscountAmount: z.number().optional(), isPaymentTargetDiscountApplied: z.boolean().optional()}).optional(), costCenter: z.string().optional(), tax: z.object({taxation: z.string().optional(), vatId: z.string().optional(), taxDisplay: z.string().optional()}).optional()}).optional(), delivery: z.object({deliveryTerms: z.object({id: z.string()}).nullable().optional(), hasDifferentDeliveryAddress: z.boolean().optional(), deliveryAddress: z.object({honorific: z.string().optional(), name: z.string().optional(), department: z.string().optional(), subDepartment: z.string().optional(), extraAddressLine: z.string().optional(), street: z.string().optional(), zipCode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), contactPerson: z.string().optional()}).optional(), dates: z.object({desiredDeliveryDate: z.string().nullable().optional(), confirmedDeliveryDate: z.string().nullable().optional()}).optional()}).optional(), formatting: z.object({hideLetterHead: z.boolean().optional(), hidePrices: z.boolean().optional(), hideProductNumbers: z.boolean().optional(), showLongProductNumbers: z.boolean().optional(), hideProductDescriptions: z.boolean().optional(), showOwnProductNumberInColumn: z.boolean().optional()}).optional(), internalComment: z.string().optional(), liabilitiesInfo: 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 purchase orders * * GET /api/v1/purchaseOrders */ export const getApiApiV1PurchaseOrders = async (args: GetApiApiV1PurchaseOrdersArgs, options?: GetApiApiV1PurchaseOrdersOptions) => { 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 (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/purchaseOrders${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(getApiApiV1PurchaseOrdersResponse, await res.json()) };