import {z} from 'zod' export type GetApiApiV2CustomersArgs = {filter?: (Array<{key?: ('id' | 'uuid' | 'number' | 'customerType' | 'name' | 'deviatingCustomerNumber' | 'firstname' | 'lastname' | 'title' | 'salutation' | 'birthdate' | 'department' | 'subDepartment' | 'projectId' | 'phone' | 'fax' | 'email' | 'website' | 'salesManager' | 'accountManager' | 'isLead' | 'contactPerson' | 'street' | 'zip' | 'city' | 'state' | 'addressType') | undefined, op?: ('equals' | 'notEquals' | 'lessThan' | 'greaterThan' | 'in' | 'notIn') | undefined, value?: (string | Array) | undefined}>) | undefined, page?: {number: string, size: string} | undefined, cursor?: ({nextCursor?: string | undefined, size?: string | undefined}) | undefined, order?: (Array<{field?: 'id' | undefined, dir?: ('asc' | 'desc') | undefined}>) | undefined}; export type GetApiApiV2CustomersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV2CustomersResponse = z.object({data: z.array(z.object({id: z.string(), uuid: z.string().nullable().optional(), number: z.string(), deviatingCustomerNumber: z.string(), customerType: z.enum(["company", "person"]).nullable(), name: z.string(), firstname: z.string(), lastname: z.string(), title: z.string().nullable().optional(), salutation: z.enum(["company", "mr", "mrs", "other"]).nullable(), birthdate: z.string().nullable().optional(), department: z.string().nullable(), subDepartment: z.string().nullable(), projects: z.array(z.object({id: z.string()})), contactDetails: z.object({phone: z.string().nullable().optional(), fax: z.string().nullable().optional(), email: z.string().nullable().optional(), website: z.string().nullable().optional(), additionalData: z.array(z.object({label: z.string(), value: z.string().nullable()}).nullable()).optional()}), accountManager: z.object({id: z.string()}).nullable(), salesManager: z.object({id: z.string()}).nullable(), isLead: z.boolean(), contactPerson: z.string().nullable(), documentDelivery: z.object({offer: z.object({email: z.string().nullable().optional(), cc: z.string().nullable().optional()}), salesOrder: z.object({email: z.string().nullable().optional(), cc: z.string().nullable().optional()}), invoice: z.object({email: z.string().nullable().optional(), cc: z.string().nullable().optional()}), creditNote: z.object({email: z.string().nullable().optional(), cc: z.string().nullable().optional()}), deliveryNote: z.object({email: z.string().nullable().optional(), cc: z.string().nullable().optional()}), purchaseOrder: z.object({email: z.string().nullable().optional(), cc: z.string().nullable().optional()}), dunning: z.object({email: z.string().nullable().optional(), cc: z.string().nullable().optional()}), autoCreateDocuments: z.enum(["both", "delivery_note", "invoice"]).optional(), invoiceAlwaysPaper: z.boolean().optional(), amountInvoicePrints: z.number().int().nullable().optional(), preferFax: z.boolean().optional()})})), extra: z.union([z.object({totalCount: z.number().int(), page: z.object({number: z.number().int(), size: z.number().int()})}), z.object({totalCount: z.number().int().nullable(), cursor: z.object({nextCursor: z.string(), 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 customers * * GET /api/v2/customers */ export const getApiApiV2Customers = async (args: GetApiApiV2CustomersArgs, options?: GetApiApiV2CustomersOptions) => { 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.cursor != null) query.append("cursor", JSON.stringify(args.cursor)) if (args.order != null) query.append("order", args.order.join(',')) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/customers${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(getApiApiV2CustomersResponse, await res.json()) };