import {z} from 'zod' export type BillingAddressId = number; export type PatchApiBillingAddressesPublicBillingAddressIdArgs = {billingAddressId: BillingAddressId, body: {address1?: string | undefined, address2?: (string | null) | undefined, city?: string | undefined, country?: string | undefined, postal_code?: string | undefined, state?: string | undefined, erp_code?: string | undefined}}; export type PatchApiBillingAddressesPublicBillingAddressIdOptions = {baseUrl?: string | undefined, appId?: string | undefined, fetch?: typeof fetch | undefined}; export const billingAddress = z.object({id: z.number().int().optional(), address1: z.string().max(250).optional(), address2: z.string().max(250).nullable().optional(), city: z.string().max(100).optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().max(3).optional(), erp_code: z.string().max(50).optional()}); export const salesPerson = z.object({uuid: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), erp_code: z.string().optional()}); export const address = z.object({id: z.number().int().optional(), address1: z.string().max(250).optional(), address2: z.string().max(250).nullable().optional(), city: z.string().max(100).optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().max(3).optional(), erp_code: z.string().max(50).optional()}); export const account = z.object({billing_addresses: z.array(billingAddress).optional(), country_code: z.string().nullable().optional(), credit_cards_enabled: z.boolean().optional(), metadata: z.object({}).nullable().optional(), name: z.string().optional(), created: z.string().optional(), credit_line: z.number().nullable().optional(), erp_code: z.string().nullable().optional(), id: z.number().int().optional(), notes: z.string().nullable().optional(), payment_terms: z.string().nullable().optional(), payment_terms_period: z.number().int().nullable().optional(), phone: z.string().nullable().optional(), phone_ext: z.string().nullable().optional(), purchase_orders_enabled: z.boolean().optional(), salesperson: salesPerson.optional(), sold_to_address: address.optional(), tax_exempt: z.boolean().optional(), tax_rate: z.number().nullable().optional(), type: z.string().optional(), url: z.string().nullable().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 }; /** * Update a Billing Address * * PATCH /billing_addresses/public/{billingAddressId} */ export const patchApiBillingAddressesPublicBillingAddressId = async (args: PatchApiBillingAddressesPublicBillingAddressIdArgs, options?: PatchApiBillingAddressesPublicBillingAddressIdOptions) => { const {baseUrl = "https://api.paperlessparts.com/v1", fetch: fetchFn = fetch, appId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (appId !== undefined) headers.set("Authorization", appId) const url = `${baseUrl.replace(/\/+$/, '')}/billing_addresses/public/${encodeURIComponent(String(args.billingAddressId))}${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(account, await res.json()) };