import {z} from 'zod' export type CustomerDetailsV202603 = {id?: string | undefined, type?: ('CONSUMER' | 'BUSINESS') | undefined, tax_ids?: Array<{type: string, value: string}> | undefined, address?: ({address_line_1: string, address_line_2?: string | undefined, address_city: string, address_province: string, address_postal_code: string, address_country: string, address_type: string}) | undefined, ip?: ({value: string, resolution?: ('strict' | 'zero' | 'approximate' | 'best_effort') | undefined}) | undefined}; export type Address = {address_line_1: string, address_line_2?: string | undefined, address_city: string, address_province: string, address_postal_code: string, address_country: string}; export type LineItem = {reference_line_item_id?: string | undefined, reference_product_id?: string | undefined, product_category?: string | undefined, fallback_product_category?: string | undefined, amount: number, quantity: number}; export type OrderDetails = {customer_currency_code: string, tax_included_in_amount: boolean, automatic_tax?: ('auto' | 'disabled') | undefined, line_items: Array}; export type Metadata = {example_key?: string | undefined}; export type CalculationRequestV202603 = {customer: CustomerDetailsV202603, origin_address: Address, order_details: OrderDetails, metadata?: Metadata | undefined, default_customer_type?: ('CONSUMER' | 'BUSINESS') | undefined}; export type CustomerDetailsV2025 = {id?: string | undefined, type?: ('CONSUMER' | 'BUSINESS') | undefined, tax_ids?: Array<{type: string, value: string}> | undefined, address: {address_line_1: string, address_line_2?: string | undefined, address_city: string, address_province: string, address_postal_code: string, address_country: string, address_type: string}}; export type CalculationRequestV2025 = {customer: CustomerDetailsV2025, origin_address: Address, order_details: OrderDetails, metadata?: Metadata | undefined}; export type CustomerDetails = {id?: string | undefined, address: {address_line_1: string, address_line_2?: string | undefined, address_city: string, address_province: string, address_postal_code: string, address_country: string, address_type: string}}; export type CalculationRequest = {customer: CustomerDetails, origin_address: Address, order_details: OrderDetails, metadata?: Metadata | undefined}; export type CreateApiTaxCalculationsArgs = {'X-API-Version': '2026-03-01' | '2026-01-01' | '2025-05-12' | '2024-09-01', body: CalculationRequestV202603 | CalculationRequestV2025 | CalculationRequestV2025 | CalculationRequest}; export type CreateApiTaxCalculationsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionLineItem = z.object({product: z.object({reference_product_name: z.string().optional(), reference_line_item_id: z.string().optional(), reference_product_id: z.string().optional(), product_tax_code: z.string().optional()}).optional(), tax_jurisdictions: z.array(z.object({tax_rate: z.number().optional(), tax_due_decimal: z.number().optional(), rate_type: z.string().optional(), fee_amount: z.number().optional(), tax_authority_name: z.string().optional(), tax_authority_type: z.string().optional(), tax_type: z.string().optional()}).nullable()).optional(), quantity: z.number().optional(), tax_amount: z.number().optional(), amount_excluding_tax: z.number().optional(), amount_including_tax: z.number().optional()}); export const metadata = z.object({example_key: z.string().optional()}); export const calculationResponse = z.object({id: z.string().optional(), object: z.string().optional(), customer_currency_code: z.string().optional(), customer: z.object({type: z.enum(["CONSUMER", "BUSINESS"]).optional()}).optional(), automatic_tax: z.enum(["auto", "disabled"]).optional(), line_items: z.array(transactionLineItem).optional(), tax_included_in_amount: z.boolean().optional(), total_tax_amount: z.number().optional(), total_amount_excluding_tax: z.number().optional(), total_amount_including_tax: z.number().optional(), expires_at: z.number().optional(), testmode: z.boolean().optional(), address_resolution_status: z.enum(["EXACT", "POSTAL_FALLBACK_1", "POSTAL_ONLY"]).optional(), address_used: z.object({address_line_1: z.string(), address_line_2: z.string().optional(), address_city: z.string(), address_province: z.string(), address_postal_code: z.string(), address_country: z.string()}).optional(), metadata: metadata.optional(), location_source: z.enum(["address", "ip"]).optional(), resolution_precision: z.enum(["STREET", "POSTAL_PLUS", "POSTAL", "PROVINCE", "COUNTRY", "APPROXIMATED"]).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 }; /** * Get tax information for a given product and address * * POST /tax/calculations */ export const createApiTaxCalculations = async (args: CreateApiTaxCalculationsArgs, options?: CreateApiTaxCalculationsOptions) => { const {baseUrl = "https://api.numeralhq.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-Version'] != null) headers.set("X-API-Version", String(args['X-API-Version'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/tax/calculations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(calculationResponse, await res.json()) };