import {z} from 'zod' export type UpdateCustomerTaxInfoV3Dto = {vatId?: string | undefined, ein?: string | undefined, isTaxExempt?: boolean | undefined}; export type PatchApiV3CustomersIdTaxInfoArgs = {id: string, body: UpdateCustomerTaxInfoV3Dto}; export type PatchApiV3CustomersIdTaxInfoOptions = {baseUrl?: string | undefined, customHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const addressV3Dto = z.object({externalId: z.string(), city: z.string(), state: z.string(), country: z.string().optional(), postalCode: z.string(), line1: z.string(), line2: z.string(), addressee: z.string().optional()}); export const externalIdSourceType = z.enum(["QUICKBOOKS", "NETSUITE", "RILLET", "SAGE_INTACCT", "SALESFORCE", "HUBSPOT", "AVALARA", "ANROK", "STRIPE"]); export const externalIdV3Dto = z.object({type: externalIdSourceType, id: z.string()}); export const customerTaxInfoV3Dto = z.object({vatId: z.string().optional(), ein: z.string().optional(), isTaxExempt: z.boolean()}); export const integratorsApiError = z.object({code: z.number(), message: z.string(), details: z.object({}).optional()}); export const patchApiV3CustomersIdTaxInfoResponse = z.object({payload: z.object({id: z.string(), name: z.string(), parentCustomerId: z.string(), defaultCurrency: z.string(), primaryBillingContactName: z.string(), primaryBillingContactEmail: z.string(), secondaryBillingContacts: z.array(z.string()), billingAddress: addressV3Dto.optional(), shippingAddress: addressV3Dto.optional(), externalIds: z.array(externalIdV3Dto).optional(), lastUpdatedAt: z.string(), customFields: z.array(z.string()).optional(), autoCharge: z.boolean().optional(), dunningDisabled: z.boolean().optional(), customerTaxInfo: customerTaxInfoV3Dto.optional()}), success: z.boolean(), message: z.string(), error: integratorsApiError}); 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 customer tax info * * PATCH /v3/customers/{id}/tax-info */ export const patchApiV3CustomersIdTaxInfo = async (args: PatchApiV3CustomersIdTaxInfoArgs, options?: PatchApiV3CustomersIdTaxInfoOptions) => { const {baseUrl = "https://integrators.prod.api.tabsplatform.com", fetch: fetchFn = fetch, customHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (customHeader !== undefined) headers.set("Authorization", customHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v3/customers/${encodeURIComponent(String(args.id))}/tax-info${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(patchApiV3CustomersIdTaxInfoResponse, await res.json()) };